Using a Variable result to call an Object (Solved)
Nakita Block
30 Aug 2018, 03:13I don't know a better way to title it because it's more complex than that. Since I don't have an actual code for this (I've been trying to see if it's possible before writing a bunch of stuff out) I'll just make something up like I did in the editor.
Let's say I have a couple object variables Room.Name1 and Room.Name2, which, according to this particular script I want to write, the number may possibly go to double digits.
So now let's say I'm going to have something that will need to read an integer to know which Room.Name to use. So is there some kind of way to use two variables together to call it? I'll give an example, although I know it doesn't work, but it will let you know what I'm trying to do.
temp1 = "Name"
temp2 = "2"
temp3 = temp1 + temp2
DoAnActionWith Room.temp3
So I know that doesn't work, but hopefully you'll understand what I'm trying to accomplish.
Thank you so much in advance!
I'm guessing there's a better way to utilize this type of process with a list or dictionary, but I just don't understand how to use those. The actual variables being called are static, but they need to be called according to the specific integer requested.
Nakita Block
30 Aug 2018, 03:26Actually let me explain it a little better. The reason I'm using Objects are because they will be certain types of buildings that will be making things. Like a sawmill that produces lumber, but there are also sawmill upgrades, so I have an object called "Sawmill"
And then I use Sawmill attributes Level1, Level2, etc so I'm guessing that eventually I'm going to need to have the program figure out what that level number is, or if I upgrade, it's going to need to know which attribute to go to next, and it will need to be called accordingly.
Because my knowledge of the language is still quite limited, that seems to be the fastest way because I can set the attribute to the number I need for that level. And since the Sawmill's level attribute won't be the same as, say, a Blacksmith, I can't use a universal leveling system. I'm really trying stay as far away from deep If/Else nests if possible, so I'm trying to keep these numbers static
hegemonkhan
30 Aug 2018, 05:42Take a look at the String (Manipulation) Functions:
http://docs.textadventures.co.uk/quest/functions/#string
there's also Concatenation, a quick simple example:
string_variable = "Hi"
msg (string_variable)
string_variable = string_variable + ", how are you?"
msg (string_variable)
string_variable = string_variable + " My name is HK"
msg (string_variable)
string_variable = string_variable + ", what is your name?"
msg (string_variable)
get input {
player.alias = result
// let's say you typed in: Nakita
// result = "Nakita"
// player.alias = "Nakita"
}
string_variable = string_variable + " Ah, nice to meet you, " + player.alias + "!"
msg (string_variable)
// output/results/display:
Hi
Hi, how are you?
Hi, how are you? My name is HK
Hi, how are you? My name is HK, what is your name?
Hi, how are you? My name is HK, what is your name? Ah, nice to meet you, Nakita!
then there's using List/Dictionary Attributes:
(Lists/Dictionaries are just containers of items of input-output functions. They can hold/store multiple items, and thus you can add/remove items, and iterate, via 'for' and 'foreach', through their items, doing whatever actions you wish for/upon them)
http://docs.textadventures.co.uk/quest/using_lists.html
http://docs.textadventures.co.uk/quest/using_dictionaries.html
(this is a bit old and has some mistakes in it... I have other posts floating around which are newer and probably do a better and/or more concise job of trying to explain Lists and Dictionaries):
http://textadventures.co.uk/forum/samples/topic/5137/list-and-dictionary-extensive-guide-by-hk
which is from within this link, which has more useful guides/resources you might want to look at as well:
http://textadventures.co.uk/forum/general/topic/ljjm32av4e2t9ot49k478g/help#710be61e-eae1-4af1-8363-520cc718ba1c
Lists:
Item 1:
input: key (as a string/integer): index number: 0
output: value: format string: WHATEVER (if a String List: then the output is a String, if an Object List, then the output is an Object reference/pointer)
Item 2:
input: key (as a string/integer): index number: 1
output: value: format string: WHATEVER (if a String List: then the output is a String, if an Object List, then the output is an Object reference/pointer)
Item 3:
input: key (as a string/integer): index number: 2
output: value: format string: WHATEVER (if a String List: then the output is a String, if an Object List, then the output is an Object reference/pointer)
etc etc etc
Dictionaries:
Item 1:
input: key (as a string): WHATEVER
output: value: format string: WHATEVER (if a String Dictionary: then the output is a String, if an Object Dictionary, then the output is an Object reference/pointer, if a Script Dictionary, then the output/s is/are Script/s)
Item 2:
input: key (as a string): WHATEVER
output: value: format string: WHATEVER (if a String Dictionary: then the output is a String, if an Object Dictionary, then the output is an Object reference/pointer, if a Script Dictionary, then the output/s is/are Script/s)
Item 3:
input: key (as a string): WHATEVER
output: value: format string: WHATEVER (if a String Dictionary: then the output is a String, if an Object Dictionary, then the output is an Object reference/pointer, if a Script Dictionary, then the output/s is/are Script/s)
etc etc etc
Lists:
VARIABLE = ListItem (LIST, INDEX_NUMBER_INPUT) // this Function returns the output of the given input (see below)
// VARIABLE = returned (string or object refernce/pointer) output
// quest will parse the Data Type when you use this function, so this is a dynamic function
msg (VARIABLE)
// or:
msg (StringListItem (LIST, INDEX_NUMBER_INPUT))
VARIABLE = StringListItem (STRINGLIST, INDEX_NUMBER_INPUT) // this Function returns the output of the given input (see below)
// VARIABLE = returned string output
msg (VARIABLE)
// or:
msg (StringListItem (STRINGLIST, INDEX_NUMBER_INPUT))
VARIABLE = ObjectListItem (OBJECTLIST, INDEX_NUMBER_INPUT) // this Function returns the output of the given input (see below)
// VARIABLE = returned object reference/pointer output
msg (VARIABLE)
// or:
msg (ObjectListItem (OBJECTLIST, INDEX_NUMBER_INPUT))
Dictionaries:
VARIABLE = DictionaryItem (DICTIONARY, STRING_INPUT) // this Function returns the output of the given input (see below)
// VARIABLE = returned (string or object reference/pointer or script) output
// quest will parse the Data Type when you use this function, so this is a dynamic function
if (TypeOf (VARIABLE) = "string" or TypeOf (VARIABLE) = "object") {
msg (VARIABLE)
} else if (TypeOf (VARIABLE) = "script") {
invoke (VARIABLE)
}
// or:
if (TypeOf (DICTIONARY) = "stringdictionary" or TypeOf (DICTIONARY) = "objectdictionary") {
msg (VARIABLE)
} else if (TypeOf (DICTIONARY) = "scriptdictionary") {
invoke (VARIABLE)
}
// or:
if (TypeOf (DICTIONARY) = "stringdictionary" or TypeOf (DICTIONARY) = "objectdictionary") {
msg (DictionaryItem (DICTIONARY, STRING_INPUT))
} else if (TypeOf (DICTIONARY) = "scriptdictionary") {
invoke (DictionaryItem (DICTIONARY, STRING_INPUT))
}
VARIABLE = StringDictionaryItem (STRINGDICTIONARY, STRING_INPUT) // this Function returns the output of the given input (see below)
// VARIABLE = returned string output
msg (VARIABLE)
// or:
msg (StringDictionaryItem (OBJECTDICTIONARY, STRING_INPUT))
VARIABLE = ObjectDictionaryItem (OBJECTDICTIONARY, STRING_INPUT) // this Function returns the output of the given input (see below)
// VARIABLE = returned object reference/pointer output
msg (VARIABLE)
// or:
msg (ObjectDictionaryItem (OBJECTDICTIONARY, STRING_INPUT))
VARIABLE = ScriptDictionaryItem (SCRIPTDICTIONARY, STRING_INPUT) // this Function returns the output of the given input (see below)
// VARIABLE = returned script(s) output
invoke (VARIABLE)
// or:
invoke (ScriptDictionaryItem (SCRIPTDICTIONARY, STRING_INPUT))
there's the 'foreach', 'for', and 'while' Functions, and/or looping and/or recursion methods, for doing iteration:
http://docs.textadventures.co.uk/quest/scripts/foreach.html
http://docs.textadventures.co.uk/quest/scripts/for.html
http://docs.textadventures.co.uk/quest/scripts/while.html
some quick simple examples:
object_variable_1 = create ("structures")
object_variable_2 = create ("lumbermill")
object_variable_2.parent = object_variable_1
object_variable_2 = create ("blacksmith")
object_variable_2.parent = object_variable_1
object_variable_1.objectlist_attribute = NewObjectList ()
list add (object_variable_1.objectlist_attribute, lumbermill)
list add (object_variable_1.objectlist_attribute, blacksmith)
object_variable_1.stringlist_attribute = NewStringList ()
list add (object_variable_1.stringlist_attribute, "damage")
list add (object_variable_1.stringlist_attribute, "defense")
// this is very inefficient to nest iterations like this (but it's just an example for you to see what can be done):
foreach (object_variable_3, object_variable_1.objectlist_attribute) {
foreach (string_variable, object_variable_1.stringlist_attribute) {
for (numbering_integer_variable, 1, 4) {
numbering_string_variable = ToString (numbering_integer_variable)
attribute_name_string_variable = string_variable + "_upgrade_" + numbering_string_variable
set (object_variable_1.name, attribute_name_string_variable, numbering_integer_variable + 1)
}
}
// this will create these Objects and their Attributes and their Values:
structures.objectlist_attribute = [lumbermill,blacksmith] // pseudo-code (can't use 'Split' for Object lists, nor Object Dictionaries)
structures.stringlist_attribute = Split ("damage;defense", ";")
// (also, the 'lumbermill' and 'blacksmith' Objects are contained within the 'structures' Object):
lumbermill.parent = structures
blacksmith.parent = structures
lumbermill.damage_upgrade_1 = 2
lumbermill.damage_upgrade_2 = 3
lumbermill.damage_upgrade_3 = 4
lumbermill.damage_upgrade_4 = 5
lumbermill.defense_upgrade_1 = 2
lumbermill.defense_upgrade_2 = 3
lumbermill.defense_upgrade_3 = 4
lumbermill.defense_upgrade_4 = 5
blacksmith.damage_upgrade_1 = 2
blacksmith.damage_upgrade_2 = 3
blacksmith.damage_upgrade_3 = 4
blacksmith.damage_upgrade_4 = 5
blacksmith.defense_upgrade_1 = 2
blacksmith.defense_upgrade_2 = 3
blacksmith.defense_upgrade_3 = 4
blacksmith.defense_upgrade_4 = 5
I'm not quite sure I understand exactly what you want done...
(sounds a bit like old warcraft games and other such games, with buildings and upgrades to units/resources/etc)
do you mean something like this?
(Object Name: lumber_mill_1)
Attribute Name: damage_1
Attribute Value: 2 // such as using as a modifier for your 'archer' units' damage
(Object Name: lumber_mill_1)
Attribute Name: damage_2
Attribute Value: 3 // such as using as a modifier for your 'archer' units' damage
(Object Name: lumber_mill_1)
Attribute Name: defense_1
Attribute Value: 2 // such as using as a modifier for your 'archer' units' armor_class
(Object Name: lumber_mill_1)
Attribute Name: defense_2
Attribute Value: 3 // such as using as a modifier for your 'archer' units' armor_class
(Object Name: lumber_mill_2)
Attribute Name: damage_1
Attribute Value: 4 // such as using as a modifier for your 'archer' units' damage
(Object Name: lumber_mill_2)
Attribute Name: damage_2
Attribute Value: 5 // such as using as a modifier for your 'archer' units' damage
(Object Name: lumber_mill_2)
Attribute Name: defense_1
Attribute Value: 4 // such as using as a modifier for your 'archer' units' armor_class
(Object Name: lumber_mill_2)
Attribute Name: defense_2
Attribute Value: 5 // such as using as a modifier for your 'archer' units' armor_class
and then having a way of handling it, which is what you need help with from us
I'm new to doing this type of game design... so I may not be able to provide the best design method for doing this stuff (mrangel or pixie or whoever can probably craft a better design method than me for this type of stuff) and also there's a lot of different design methods you can do, and it depends on exactly what game design you want, as well...
here's some examples..
<object name="units">
<structure_objectlist type="objectlist">
<value>archers</value>
<value>knights</value>
</structure_objectlist>
<object name="archers">
<attr name="damage" type="int">1</attr>
<attr name="defense" type="int">1</attr>
</object>
<object name="knights">
<attr name="damage" type="int">2</attr>
<attr name="defense" type="int">2</attr>
</object>
</object>
<object name="structures">
<structure_objectlist type="objectlist">
<value>lumbermill</value>
<value>blacksmith</value>
</structure_objectlist>
<object name="lumbermill">
<attr name="lumbermill_upgrade_level" type="int">0</attr>
<attr name="damage_upgrade_level" type="int">0</attr>
<attr name="defense_upgrade_level" type="int">0</attr>
<attr name="damage_upgrade_1" type="int">2</attr>
<attr name="damage_upgrade_2" type="int">3</attr>
<attr name="damage_upgrade_3" type="int">4</attr>
<attr name="damage_upgrade_4" type="int">5</attr>
<attr name="defense_upgrade_1" type="int">2</attr>
<attr name="defense_upgrade_2" type="int">3</attr>
<attr name="defense_upgrade_3" type="int">4</attr>
<attr name="defense_upgrade_4" type="int">5</attr>
</object>
<object name="blacksmith">
<attr name="blacksmith_upgrade_level" type="int">0</attr>
<attr name="damage_upgrade_level" type="int">0</attr>
<attr name="defense_upgrade_level" type="int">0</attr>
<attr name="damage_upgrade_1" type="int">2</attr>
<attr name="damage_upgrade_2" type="int">3</attr>
<attr name="damage_upgrade_3" type="int">4</attr>
<attr name="damage_upgrade_4" type="int">5</attr>
<attr name="defense_upgrade_1" type="int">2</attr>
<attr name="defense_upgrade_2" type="int">3</attr>
<attr name="defense_upgrade_3" type="int">4</attr>
<attr name="defense_upgrade_4" type="int">5</attr>
</object>
</object>
(err... too much work, and too hard, especially without knowing exactly what you want in your game and how you want it done)
(need more clear Objectives and will need some time as well... others might be able to help you better and faster than I can)

Dcoder
30 Aug 2018, 06:10An object can only have one (unchangeable) name. The value for each attribute of that object can change repeatedly, however.
So if you are going to script an action with your object, it doesn't matter if its attributes have changed -- it's still the same object. I don't think I really understood your question...
hegemonkhan
30 Aug 2018, 06:16just to add to Dcoder's post:
Objects can be given custom String Attributes too for their in-game "name", especially in using the built-in 'alias' String Attribute, as it has already built-in functionality for it, unlike if you create a custom String Attribute.
So, you can iterate increasing numbers onto (via catenation) these String Attributes for the 'in-game' name of your Objects
but, as Dcoder said, the Object's 'name' String Attribute is the ID for quest, so it must be unique and thus also it can't be changed

Dcoder
30 Aug 2018, 06:49Piggybacking off of HK...
Don't know if this helps, but say you have a Sawmill object, with a "level" integer attribute set to 1. You could put the following into the Sawmill's alias:
Sawmill Lvl{Sawmill.level}
It will say "Sawmill Lvl1". You will also have to put the above text into the Sawmill's Display Alias under the Object tab. When the "level" attribute changes, so will the alias.

Dcoder
30 Aug 2018, 07:58Maybe this is what you want...
game.ChosenRoom = GetObject(Room.temp3)
Room.temp3
(e.g., "Sawmill3") is a string attribute, but is also the alias (string name) of a room. Using the GetObject
function, game.ChosenRoom
has been assigned an object value that is the same as the room with the alias of "Sawmill3". Now you can just script whatever you want to game.ChosenRoom
.
The Pixie
30 Aug 2018, 09:12Okay, so not exactly sure what you are trying to do, but to give a building a level, just give it a level attribute, and set that to the number:
sawmill.level = 3
Getting a bit more complicated...
If the player can buy additional equipment to upgrade a building, you could handle that a number of ways, but one would be to have each upgrade as an object, and add the object to an upgrades list
sawmill.upgrades = NewObjectList()
list add (sawmill.upgrades, titanium saw)
list add (sawmill.upgrades, smoking area)
You could then give each upgrade attributes that give bonuses:
smoking area.happiness = 10
And add them up to get an overall bonus:
happiness = sawmill.happiness
foreach (obj, sawmill.upgrades) {
if (HasInt(obj, "happiness")) {
happiness = happiness + obj.happiness
}
}
mrangel
30 Aug 2018, 09:52So, your Sawmill object has attributes named "Level1", "Level2", "Level3" and so on.
What do those attributes contain?
In any case, your code would look something like:
temp1 = 2
temp2 = "Level" + temp2
temp3 = GetAttribute (Sawmill, temp2)
// do something with temp3
or possibly:
temp1 = "Sawmill"
temp2 = 3
temp3 = GetAttribute (temp1, "Level"+temp2)
// do something with temp3
It's really hard to understand what you actually want this to do. But this code will get the value of the attribute Sawmill.Level3
based on the first two variables set.
However, this is an inefficient way to do something that would be a lot easier to use lists for.
I could understand a little better if you had different objects named "Sawmill1", "Sawmill2", etc that behaved in different ways.
In that case, your code would be more like:
temp1 = "Sawmill"
temp2 = 3
object = GetObject (temp1 + temp2)
// do something with the objects
Nakita Block
30 Aug 2018, 10:26The Alias thing might be what I'm looking for, for this particular example. I'll have to check it after work when I have more time.
I wasn't trying to change the attribute name, I was trying to use the data from a variable to determine which attribute would be read.
Again this is just an example because I haven't made anything yet. I was just playing around with a blank script trying to get something to work. I created an Object and then I added Attributes like Speed1, Speed2, etc. But this example, which I'll just use Sawmill again:
Sawmill.Speed1 = 50
Sawmill.Speed2 = 100
Sawmill.Speed3 = 250
And we'll pretend the game has been played a while and we've upgraded the Sawmill to level 3.
SawmillLvl = 3
The day is over and we need to check to see what kind of work the Sawmill has done for us.
StrVariable = "Sawmill.Speed" + SawmillLvl //To create "Sawmill.Speed3"
Now I'm trying to use StrVariable to read the info from Sawmill.Speed3.
Instead of making IF SawmillLvl = 1 (Do this) ELSE IF SawmillLvl = 2 (Do this) because I plan on making a LOT of levels.
In short, I'm trying to "hack" the script into using Arrays according to how I'm used to seeing Arrays, e.g. Sawmill.Power(n)
After reading the wonderful replies you have offered it seems that it's a lot easier to create a List to do this instead, since the data that I add to the List will be static throughout the game. BUT if it's possible to utilize that kind of manipulation in the example above it could certainly prove useful in some other way.
Nakita Block
30 Aug 2018, 10:27Dcoder and mrangel, that's exactly it! GetObject (tempname)
And let me tell you.. When you're searching for something through the list of commands, but you don't really know what you're looking for, the words just start to run together and get more and more confusing.
In either case, I'm going to make a List instead, but I'm absolutely certain that GetObject will definitely come in handy at some point because of the way that I make games. Thank you guys ever so much for your input, because even those things that were listed that didn't go with what I Was looking for were great examples for other things that I'll use in the future. You guys are extremely helpful.
mrangel
30 Aug 2018, 10:38In short, I'm trying to "hack" the script into using Arrays according to how I'm used to seeing Arrays, e.g. Sawmill.Power(n)
If you already know how to use arrays, then you might as well create a list. Then you can refer to Sawmill.power[n]
like you'd expect (Note: that's equivalent to ListItem (Sawmill.power, n)
. The square-bracket notation is syntactic sugar for people who are used to arrays in other languages)
hegemonkhan
30 Aug 2018, 17:18arrays and lists are basically the same thing (I know there's probably some technical differences)
the term 'list' might be used, just because its more friendly to non-coders than the scary sounding term of 'array'
Data Type Conversions:
// conceptually only (due to order of operations of the code lines):
integer_variable = ToInt (string_variable)
string_variable = ToString (integer_variable)
double_variable = ToDouble (string_variable)
string_variable = ToString (double_variable)
string_variable = object_variable.name
object_variable = GetObject (string_variable)
'ToString' also works on: Strings, Object references/pointers, Scripts, Lists, and Dictionaries ( http://docs.textadventures.co.uk/quest/functions/tostring.html )
Data Type Checking:
IsXXX // if (IsXXX (XXX)) { SCRIPTING }
HasXXX // if (HasXXX (OBJECT_NAME,ATTRIBUTE_NAME)) { SCRIPTING }
GetObject // if (not GetObject (OBJECT_NAME) = null) { SCRIPTING }
TypeOf // if (TypeOf (VALUE) = "DATA_TYPE") { SCRIPTING } // or: if (TypeOf (OBJECT_NAME, ATTRIBUTE_NAME) = "DATA_TYPE") { SCRIPTING }
DoesInherit // if (DoesInherit (OBJECT_NAME, OBJECT_TYPE_NAME)) { SCRIPTING }
Attribute Value Checking:
GetXXX // if (GetXXX (OBJECT_NAME, ATTRIBUTE_NAME) = VALUE) { SCRIPTING }