Set variable expression?
Malphas
19 Mar 2023, 20:27Is there a way to set an expression that is a variable to another expression?
Maybe that is a bit unclear so let me just show you some code:
msg ("Variable?")
GetInput() {
player.tempvar = result
SetForegroundColour ("LightGreen")
msg ("What should {player.tempvar} be set to?")
GetInput() {
player.tempvarname = result
SetForegroundColour ("LightGreen")
msg ("{player.tempvar} has been set to {player.tempvarname}. (This does currently not work like it is supposed to.")
player.tempvar = player.tempvarname
MovePlayer (Cheddar set stuff)
}
}
So what I want to do is to make it so that player.tempvar is not player.tempvar but whatever I put in the first input box, is there a way to do this?
Thanks in advance!
mrangel
20 Mar 2023, 09:17
player.tempvar = player.tempvarname
That gets the value of player.tempvarname
(the second string entered), and sets player.tempvar
to it.
I assume what you're trying to do is use the first input as a variable name. This gets a little more complex.
You can't do this with local variables. But I can't think of any situation where it would be helpful to do that; because variables are only meaningful if they're in your code. If this is really what you want to do, it might make more sense to make a stringdictionary to hold your value… but that would really depend on why you want to do it.
But you can do this with attributes. Something like this would work:
msg ("Attribute?")
GetInput() {
player.tempattr = result
msg ("What should player.{player.tempattr} be set to?")
GetInput() {
player.tempvalue = result
msg ("player.{player.tempattr} will be set to {player.tempvalue}.")
// set takes three parameters:
// 1 - an object
// 2 - the name of an attribute
// 3 - the value to set that attribute to
set (player, player.tempattr, player.tempvalue)
}
}
Malphas
20 Mar 2023, 19:56Thank you, that worked like a charm.
Much appreciated!