Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing objects and object functions through variables
    primarykey
    data
    text
    <p>Background of the program: the user is able to input a <code>string</code> of two words only - a verb and a noun. I tokenize this string into a <code>vector</code> and compare <code>tokens[0]</code> with a <code>vector</code> of allowed verbs, and <code>tokens[1]</code> with a <code>vector</code> of allowed nouns.</p> <p>Now, I'm trying to find a way to allow only certain verbs to be performed on certain nouns. For example, writing "take book" will give a message (or whatever) saying it's allowed, but writing "take door" would not be. I have so far created a <code>class</code> Object with <code>bool</code> values for each possible verb (eg. within the <code>class Object</code>, I can create an <code>Object book</code> for which <code>m_take = true</code>, or <code>false</code> for an <code>Object door</code>). </p> <p>However, I'm having trouble associating these objects with the user input. For example, I would like to be able to do something like this: <br> 1) User inputs "verb noun", which go into the tokens vector as <code>tokens[0]</code> and <code>tokens[1]</code>.<br> 2) Program checks if the input contains acceptable words (individually).<br> 3) Considering <code>getstat()</code> to be the function to retreive the bool value of the possible action doable on an object, the program retrieves <code>tokens[1].getstat(tokens[0])</code> and, if <code>true</code>, executes <code>tokens[0].tokens[1]()</code> (eg. <code>book.take()</code>). This way I could have only one <code>if</code> cycle in my <code>main()</code>, which can be used by all legal verbs and nouns, without making an infinite list of <code>if</code>, <code>else if</code>, etc, considering every single option manually. <br></p> <p>Sorry if this is at all confusing. I know it is not possible to use a variable as an object name, but I'm sure there's a better way to do this than doing cycles within cycles of considering every single mix and match of verb and noun. I'm experimenting with like 3 each at the moment, but once I get this working I plan on expanding it and it would be a nightmare to keep track of every change if I have to hard-code every possible verb and noun multiple times within the source code. (Also, sorry for not posting the whole source - it's a really long file just now!) Thanks for any help/hint in the right direction! </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload