Attributes within attributes

ReaperFace
27 Jul 2016, 23:44Okay, so here's my issue. I've got a bunch of objects, named player1, player2, player3, etc.
Each of those player objects have their own individual attributes - health, mood, strength, etc.
Then, I've got some scripts which are intended to act differently depending on which player object interacts with it, depending on their attributes.
Now, I'm assuming I could just do this with a bunch of 'if's, which would work, but I'm hoping to streamline this a little bit, if only so I can change stuff later a bit easier. To this end, I've created another object, object1 (probably not necessary, but hey, it should work, right?), which has a an attribute - objectstore, which is just a string. This effectively exists to store which player object is being affected by the script at that time - e.g. if player2 is being affected, it takes the value "player2".
Okay, so say I wanted to do something with the health attribute of player2. So that can be expressed as:
player2.health
That's easy enough. But what I want to do is express the same thing using the objectstore attribute of object1. So if that attribute is "player2", I want to just replace it as such:
(object1.objectstore).health
Now, that's my problem. I'm not really sure how to express this, syntax-wise. Here's a general list of the things I've tried:
object1.objectstore.health
(object1.objectstore).health
{object1.objectstore}.health
[object1.objectstore].health
Of course, none of these appear to work. returning errors and such.
So, I'm assuming that either this a more complex issue than I think it is, or there's something really simple I'm overlooking. Any ideas?
hegemonkhan
28 Jul 2016, 06:05in quest, you can have multiple 'attachment/connectors/layers', so this for example works fine:
orc.current_life = orc.current_life - player.weapon.damage
// orc's new (reduced) life amount: 500 - 50 = 450
player.current_life = player.current_life - (orc.damage - player.armor.defense)
// player's new (reduced) life amount: 999 - (100 - 25) = 999 - 75 = 924
<object name="orc">
<attr name="current_life" type="int">500</attr>
<attr name="damage" type="int">100</attr>
</object>
<object name="player">
<attr name="current_life" type="int">999</attr>
<object name="weapon">
<attr name="damage" type="int">50</attr>
</object>
<object name="armor">
<attr name="defense" type="int">25</attr>
</object>
</object>
oh, if you want the 'objectstore' to be a String Attribute... you'd do somethng like this, if I understadn you:
(this is really bad design, there's much better methods to use! You really don't want to be doing this however! I don't quite understand what you want though. If you could explain more about what you want to do, then I can help you with it better)
if (object1.objectstore = "player1" and object1.objectstore2 = "health") {
player1.health = object1.health
} else if (object1.objectstore = "player2" and object1.objectstore2 = "health") {
player2.health = object1.health
} else if (object1.objectstore = "player1" and object1.objectstore2 = "strength") {
player1.strength = object1.strength
}
<object name="object1">
<attr name="objectstore" type="string">unknown</attr> // player1/player2/player3/etc
<attr name="objectstore2" type="string">unknown</attr> // health/strength/mood/etc
<attr name="health" type="int">100</attr>
<attr name="strength" type="int">500</attr>
</object>
But, I'm still a bit unclear on exactly what you want to do and/or why you want to do this stuff... sorry for not understanding!
You might want to take a look into:
using 'game.pov' vs 'specific Player Object references (eg: player1, player2, player3)'
Functions/Commands (as they've got Parameters you can use)
Objects + Delegates (Delegates basically allows you to use Parameters for Object's Script/Verb Attributes)
there's also the 'this' keyword/keycommand, which can be useful in scripting as it gets/uses/returns/outputs the Script's parent Object.
Also, there's of course List/Dictionary Attributes too. Though, if you're a code/quest noobie, these can be a bit difficult to understand, as they're a bit more advanced than the basic Attributes.
HK edit:
oh... I think I see now what you wanted.... for this to work:
(object1.objectstore).health
you need to use the 'set' Script/Function (as you found out, the dot/period syntax/notation doesn't work for this stuff):
http://docs.textadventures.co.uk/quest/scripts/set.html
set (OBJECT, "ATTRIBUTE_NAME", ATTRIBUTE_VALUE)
so it would be (if using/having 'objectstore' as a String Attribute):
(you need the 'GetObject' Script/Function to convert the string name into an actual reference of the Object)
( http://docs.textadventures.co.uk/quest/functions/getobject.html )
// object1.objectstore = player1.name // or "player1"
set (GetObject (object1.objectstore), "health", 100)
// player1.health = 100
// object1.objectstore = player2.name // or "player2"
set (GetObject (object1.objectstore), "health", 250)
// player2.health = 250
<object name="object1">
<attr name="objectstore" type="string">unknown</attr> // player1/player2/player3
</object>
<object name="player1">
</object>
<object name="player2">
</object>
so it would be (if using/having 'objectstore' as an Object Attribute):
// object1.objectstore = player1
set (object1.objectstore, "health", 100)
// player1.health = 100
// object1.objectstore = player2
set (object1.objectstore, "health", 250)
// player2.health = 250
<object name="object1">
<attr name="objectstore" type="object">unknown</attr> // player1/player2/player3
</object>
<object name="unknown">
</object>
<object name="player1">
</object>
<object name="player2">
</object>
Object references as Object Values of Object Attributes do NOT have the double quotes, for example using the built-in 'parent' Object Attribute: player.parent = room, notice how the VALUE (room) doesn't have the double quotes
there's a few exceptions of VALUES without double quotes, in NOT being an object reference: 'true/false' as these are reserved for Boolean Attributes' VALUES: orc.dead = false, orc.dead = true, for one example of an exception, I think there's a few others as well besides the ''true/false' of Boolean Attirbutes. Oops,I forgot the most obvious: any numerical Value obvious is not an Object reference (-100, -1, 0, 1, 100, -9.234, 0.0, 3.876)
Any VALUE with double quotes is a String.
some examples:
1. player.right_hand = sword // this is an Object Attribute (which quest determines by it having) an Object Value (no double quotes and it's not 'true/false')
// ERROR! There's no existing 'sword' Object!
2. player.right_hand = "sword" // this is a String Attribute (which quest determines by it having) a String Value (double quotes)
// NO error
3. player.right_hand = sword
<object name="sword">
</object>
// NO error
4. player.strength = 100
player.strength = player.strength + 50
// NO error: player.strength = 150
5. player.strength = "100"
player.strength = player.strength + 50
// ERROR: humans/computers can't do addition of "100" (String) and 50 (Integer), illogically possible, lol.
6. player.strength = "100"
player.strength = player.strength + "50"
// NO error: humans/computers can do concatenation (literally putting together/next-to-each each) of "100" (String) and "50" (String), making: "10050" (String)
7. player.strength = "mama"
player.strength = player.strength + "50"
// NO error: humans/computers can do concatenation (literally putting together/next-to-each each) of "mama" and "50", making: "mama50"
8. player.strength = "100"
player.strength = player.strength + "mama"
// NO error: humans/computers can do concatenation (literally putting together/next-to-each each) of "mama" and "50", making: "100mama"
The Pixie
28 Jul 2016, 07:18I think you are making it overly complicated. I am not sure exactly what you want to do, but I would guess the best way is to have local variable, and use the attributes of that.
current_player = player2
current_player.health = current_player.health - 5
current_player.strength = 10
Or parameter in a function.
DoStuffWithPlayer(player2)
-function
-parameters: current_player
current_player.health = current_player.health - 5
current_player.strength = 10

ReaperFace
28 Jul 2016, 12:58@hegemonkhan
Ah, I see what I was doing wrong now. I felt like what I was doing was a little ... weird, but I wasn't sure why until now. Thanks for explaining it so clearly! Still got a lot to learn! :)
@The Pixie
Parameters - I think that's the piece of the puzzle I've been missing! Certainly a lot easier than messing with attributes.
Thanks all for the help. :)
Jay Nabonne
29 Jul 2016, 12:19You can also make the original code work (with object1.objectstore.health) simply by storing player2 (as an object reference) rather than "player2" (as a string).