How to use an attribute to refer to a separate object?

Let's say player.favoritefood is "Icecream". Is there a way to use player.favoritefood to refer to an object called "Icecream"?

e.g. if Icecream.temp is "cold", how can I say that player.favoritefoodtemp is also "cold"?


K.V.

Is there a way to use player.favoritefood to refer to an object called "Icecream"?

player.favoritefood = Icecream

if Icecream.temp is "cold", how can I say that player.favoritefoodtemp is also "cold"?

If the Icecream object has the attribute temp set to the string "cold", and player.favoritefood is pointing to the Icecream object, then player.favoritefood.temp will automatically return the string "cold".


Io

What KV said. You can set the attribute to ObjectType, and then just set it equal to the object Icecream.

You can also, as KV showed, 'nest' attributes in each other. So you can do something like, and pardon my psuedocode:

if player.favoritefood.temp<32:
print("The food is cold!")
else if player.favoritefood.temp<150:
print("The food is lukewarm!")
else:
print("The food is hot!")

You can go much more than this if you really need to - Object.attribute1.attr2.attr3.attr4 etc - but for what you describe, player.favoritefood.temp should do it.


Oops, I see what I was doing wrong. Thanks for the help guys!


anything in double quotes is a String Value:

player.alias = "HK"

game.introduction = "Welcome to my game, I hope you enjoy it"

example_object.example_string_attribute = "1"


any numeric expression (number as integer, or numbers and the dot/period as decimal/fractional/double/float-floating-point), withOUT the double quotes, is an amount (an integer or double/float/floating-point/decimal/fractional):

player.strength = 100
player.damage = 63.8
orc.damage = 23.98572345321


some words withOUT the double quotes are special/reserved key-values/key-words:

such as 'true/false' for a Boolean's values:
orc.dead = true
orc.dead = false
player.poisoned = true
player.poisoned = false


anything else (not an amount:integer/double, and not a special/reserved word) wtihOUT the double quotes, are Object's names/references/pointers:

create ("unarmed") // creating an 'unarmed' Object
unarmed.damage = 1

create ("katana") // creating a 'katana' Object
katana.damage = 50

player.weapon = unarmed
player.weapon = katana

player.damage = player.weapon.damage // if you want another Attribute to hold this value

player.weapon = unarmed
player.damage = player.weapon.damage
// player.damage = 1

player.weapon = katana
player.damage = player.weapon.damage
// player.damage = 50

// the below examples don't use another Attribute to hold the value:

orc.life = 100

player.weapon = unarmed
orc.life = orc.life - player.weapon.damage
// orc.life = [100] - [1] = 99

player.weapon = katana
orc.life = orc.life - player.weapon.damage
// orc.life = [100] - [50] = 50


it takes awhile to remember or get used to the difference between double quotes and no double quotes:

player.weapon = "katana" // A String Attribute and Value

VS

create ("katana")
player.weapon = katana // An Object (reference/pointer) Attribute and Value


if (player.weapon = "katana") {
  orc.life = orc.life - 50
  msg ("Weapon: katana")
  // displays:
  // Weapon: katana
} else if (player.weapon = "unarmed") {
  orc.life = orc.life - 1
  msg ("Weapon: unarmed")
  // displays:
  // Weapon: unarmed
}

VS

create ("unarmed_object")
unarmed_object.alias = "unarmed"
unarmed_object.damage = 1

create ("katana_object")
katana_object.alias = "katana"
katana_object.damage = 50

---------------------------------

if (player.weapon = katana_object) {
  msg ("Weapon: " + katana_object.name + " (" + katana_object.damage + ")")
  msg ("Weapon: " + katana_object.alias + " (" + katana_object.damage + ")")
  // displays:
  // Weapon: katana_object (50)
  // Weapon: katana (50)
} else if (player.weapon = unarmed_object) {
  msg ("Weapon: " + unarmed_object.name + " (" + unarmed_object.damage + ")")
  msg ("Weapon: " + unarmed_object.alias + " (" + unarmed_object.damage + ")")
  // displays:
  // Weapon: unarmed_object (1)
  // Weapon: unarmed (1)
}

orc.life = orc.life - player.weapon.damage

also, a very useful trick (especially when using Clones):

(you can also create an Object List Attribute and add the Object reference/pointers to it, for being able to get at multiple Objects/Cloned-Objects)

example_object.object_attribute = Create ("unarmed_object")
example_object.object_attribute.alias = "unarmed"
example_object.object_attribute.damage = 1

orc.life = orc.life - example_object.object_attribute.damage
msg ("Weapon: " + example_object.object_attribute.alias)

example_object.object_attribute = Create ("katana_object")
example_object.object_attribute.alias = "katana"
example_object.object_attribute.damage = 50

orc.life = orc.life - example_object.object_attribute.damage
msg ("Weapon: " + example_object.object_attribute.alias)

---------

example_object.object_attribute = CloneObject (unarmed_object)
example_object.object_attribute.alias = "unarmed"
example_object.object_attribute.damage = 1

orc.life = orc.life - example_object.object_attribute.damage
msg ("Weapon: " + example_object.object_attribute.alias)

example_object.object_attribute = CloneObject (katana_object)
example_object.object_attribute.alias = "katana"
example_object.object_attribute.damage = 50

orc.life = orc.life - example_object.object_attribute.damage
msg ("Weapon: " + example_object.object_attribute.alias)

This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums