Appending a string to an attribute name
scrimshaw04
09 Jul 2017, 13:56I'm trying to set up a system where the description of an object that the player holds changes depending on different circumstances. When the player looks at the object, I want one of a set of possible 'look at' descriptions to be selected randomly.
So, I've got a few script dictionaries, labelled something like 'dictionary_1', 'dictionary_2', 'dictionary_3' etc... What I want to do is set up a script that says when the player looks at the object, it displays a message from 'dictionary_x', where x is an integer. Is there a way I can tell Quest to run "dictionary_" + x?

Dcoder
09 Jul 2017, 14:29How about just use the text processor's randomizing command in your object's description:
{random:text 1:text 2:text 3}
hegemonkhan
09 Jul 2017, 14:34you can just use a Stringlist Attribute, no need for a Scriptdictionary Attribute, an example:
<object name="room>
</object>
<object name="player">
<attr name="parent" type="object">room</attr>
</object>
<object name='example_object_1">
<attr name="parent" type="object">room</attr>
<attr name="look_stringlist_attribute" type="stringlist">
<value>LOOK_DESCRIPTION_1</value>
<value>LOOK_DESCRIPTION_2</value>
<value>LOOK_DESCRIPTION_3</value>
</atrt>
</object>
<object name='example_object_2">
<attr name="parent" type="object">room</attr>
<attr name="look_stringlist_attribute" type="stringlist">
<value>LOOK_DESCRIPTION_4</value>
<value>LOOK_DESCRIPTION_5</value>
<value>LOOK_DESCRIPTION_6</value>
</atrt>
</object>
// you'd type in during game play:
// look at example_object_1
// and/or:
// look at example_object_2
<command name="look_at_command">
<pattern>look at #object_parameter#</pattern>
<script>
integer_variable = GetRandomInt (0, ListCount (object_parameter.look_stringlist_attribute) - 1)
string_variable = StringListItem (object_parameter.look_stringlist_attribute, integer_variable)
msg (string_variable)
</script>
<unresolved>wrong input: either the Object doesn't exist or it's just not in the same room as you</unresolved>
</command>
you can always try to concatenate with whatever the Function/whatever-scripting (see if it works or not), I know you definately can concatenate with:
'set' and 'do'
I think you can do concatenation in the list and dictionary Functions too...
The Pixie
09 Jul 2017, 14:50If I understand you right, you want to be able to pick a dictionary, given a number. I will assume these script dictionaries are all attributes of the game object, and you have a local variable, n
, which is the number of the dictionary, and s
, which is the name of the script (the key for the dictionary)
dict = GetAttribute(game, "dictionary_" + n)
script = ScriptDictionaryItem(dict, s)
invoke(script)
So the first line gets the dictionary, byadding n to the string to give its name. The second line gets the script from the dictionary. The third line runs it.
You may feel tempted to combine the first two lines, and then confused about why it does not work. GetAttribute can return any type and (I think) Quest will only decide what that type is when you assign it to something. If you try to use it directly in an expression, Quest will get confused (that is what happens with lists and dictionaries anyway, I guess the same for GetAttribute).
hegemonkhan
10 Jul 2017, 03:13I think Pixie meant to have 's' and not 'key', based on his post's description, so like this, an example:
dict = GetAttribute(game, "dictionary_" + n)
script = ScriptDictionaryItem(dict, s)
invoke(script)
// --------------
<game name="example_game">
<attr name="start" type="script">
n = GetRandomInt (1,2)
dict = GetAttribute (game, "dictionary_" + n)
list_variable = GetAttribute (game, "list_" + n)
list_index_integer_variable = GetRandomInt (0, ListCount (list_variable) - 1)
s = StringListItem (list_variable, list_index_integer_variable)
script = ScriptDictionaryItem (dict, s)
invoke (script)
</attr>
<attr name="list_1" type="simplestringlist">hi;bye</attr>
<attr name="dictionary_1" type="scriptdictionary">
<item key="hi">
msg ("hi")
</item>
<item key="bye">
msg ("bye")
</item>
</attr>
<attr name="list_2" type="simplestringlist">hola;adios</attr>
<attr name="dictionary_2" type="scriptdictionary">
<item key="hola">
msg ("hola")
</item>
<item key="adios">
msg ("adios")
</item>
</attr>
</game>
The Pixie
10 Jul 2017, 09:48I think Pixie meant to have 's' and not 'key', based on his post's description, so like this, an example:
Oops, yes, thanks for that. I have now updated the post.
scrimshaw04
10 Jul 2017, 14:17Got it working! Thanks so much for the help guys!