ListContains functions creates and unknown variable error

initnupt
12 Nov 2016, 17:35

Hello! I'm totally new to quest and I've been able to figure a lot of stuff out with the forums and tutorials but I'm running into an issue.

My game script starts by creating an empty string list called PeopleList and then adding some items to it using this code:

PeopleList = NewStringList()
list add (PeopleList, "Name1")
list add (PeopleList, "Name2")

I then use this code to ask the player for a name and I want to check the name against PeopleList and return one response if the name is on the list and another if it's not on the list:

TextFX_Typewriter (">> Who are you?", 100)
      get input {
        if (ListContains (PeopleList, result) ) {
          TextFX_Typewriter ("Lovely to see you again, " + result + ". Let's begin.", 100)
        }
        else {
          TextFX_Typewriter ("I'm sorry, your name isn't on the list, I'm afraid I can't let you in.", 100)
        }

When I run this code and enter a name, I get the following:
"Error running script: Error compiling expression 'ListContains (PeopleList, result) ': Unknown object or variable 'PeopleList'"

Can anyone explain what's going on here?

Thanks!


The Pixie
12 Nov 2016, 21:33

I think the problem is PeopleList goes out of scope. As you have defined it, it is a variable that is local to that bit of code, and Quest forgets it when it finishes that script. Occasionally it will even forget it within a script, for example with the ShowMenu function. The solution is to use attributes instead, and the easiest way to do that is to prepend game. to every `PeopleList, so for example:

game.PeopleList = NewStringList()

hegemonkhan
12 Nov 2016, 22:28

as Pixie said:

use Attribute VARIABLES, as they're global/permanent (so long as the Object containing it, exists or still exists, of course):

NAME_OF_OBJECT.NAME_OF_ATTRIBUTE
// or:
NAME_OF_OBJECT.NAME_OF_ATTRIBUTE = VALUE_OR_EXPRESSION

so, some examples:

// in-code scripting:

game.sex_list = split ("male; female", ";")
// or:
game.sex_list = NewStringList ()
list add (game.sex_list, "male")
list add (game.sex_list, "female")

// aslx/xml 'creation' tag blocks:

(the 'game' Game Object, already exists, as it must exist, but you can add new Attributes to it)

<game name="xxx">
  <attr name="sex_list" type="simplestringlist">male;female</attr>
</game>

// ----------------------

// in-code scripting:

player.spell_list = split ("fireball; icespike; earthquake; windslash", ";")
// or:
player.spell_list = NewStringList ()
list add (player.spell_list, "fireball")
list add (game.spell_list, "icespike")
list add (game.spell_list, "earthquake")
list add (game.spell_list, "windslash")

// aslx/xml 'creation' tag blocks:

(there must be a designated Player Object, either the default 'player' Player Object or your own Player Object, and that Player Object must be within a Room Object, quest requires it)

<object name="room">
</object>

<object name="player">
  <attr name='parent" type="object">room</attr>
  <attr name="spell_list" type="simplestringlist">fireball;icespike;earthquake;windslash</attr>
</object>

// -------------

and you can do the same with your own custom Objects and giving them Attributes (List Attributes for this topic, of course)

se here for more guides/help/examples if you haven't already found them:

http://textadventures.co.uk/forum/samples/topic/5559/attributes-and-if-script-guide-by-hk
http://textadventures.co.uk/forum/samples/topic/5137/list-and-dictionary-extensive-guide-by-hk
http://textadventures.co.uk/forum/samples/topic/5138/explore-and-travel-code-sample-by-hk
http://textadventures.co.uk/forum/quest/topic/3348/noobie-hks-help-me-thread#22483
http://textadventures.co.uk/forum/quest/topic/3348/noobie-hks-help-me-thread#22486


ask if you need any help, explanation, or whatever


hegemonkhan
12 Nov 2016, 22:38

P.S.

in-code, the List Attribute's Values (the list's items) must be enclosed in double quotes, unless you're using an Objectlist Attribute (which unfortunately for whatever reason can't be done in the GUI/Editor or was left out of the GUI/Editor, but it can still be done in-code). An Objectlist Attribute's Values (its items), are object references, so that Object must actually exist or still exist.

or, unless you're using a String VARIABLE, or unless you've got a Function which returns a string value. I think the 'list add' can use/have a VARIABLE/Function in it, but the 'split' can't have a VARIABLE/Function in it