Setting a variable inside another variable (sugarcube 2.30)

I want this in my room #50

<<set $number50Room = $room>>

the 50 is a variable though ($myLocation)

I enter room 100, I then need a line in that room that says

<<set $number100Room = $room>>

I can’t hard code this in each room, I need this done using variables.

I’ve tried …
<<set $+“number”+$myLocation+“Room” = $room>>
<<set $number+$myLocation+Room = $room>>
and a bunch of ideas similar to that, but none work.

I’m sure someone has a simple answer…thanks.

It sounds like you should be using an array.

<<set $room[$var] to $roomvalue>>

You will have to establish the array near the start of your game (maybe in StoryInit) so the size is already known. Like <<set $room to [0, 0, 0, 0, … , 0]>>, however many spaces you plan to use.

I may have got the exact format wrong, as I’m not at home.

1 Like

Since the $ is really just a shortcut for State.variables, sirslice can do something more inline with their original idea using the actual variable object. Like this:

<<set State.variables["number" + $myLocation + "Room"] = $room>>

Assuming $myLocation equals 100, that will create a $number100Room variable.

I’m not going to say this is good coding practice or even a good idea. I’m just saying this should work.

Yes that works. Thank you again.