Question About Passing Variable Names

Please specify version and format if asking for help, or apply optional tags above:
Twine Version: 2.31.3
Story Format: Sugarcube 2.34.1

Dear All:

Sorry for the bother.

I am working on a ‘dungeon-crawler’ of a sort using Twine and Sugarcube (v. 2.31.3 and 2.34.1 respectively). I have a procedural generation system that works wonderfully (I think), and the game tracks a player through any of several preset mazes (set via the StoryInit file). As you go through the maze, you have the possibility of running into one of several enemies or encountering any of a number of locked containers - or falling down some very poorly maintained stairs.

During the game, all the information needed for a check is saved to an object. Let’s say that the object looks like this -

$engine_variables {
type: “”,
foe: “”,
dice: 0,
level: 0
}

I want each challenge in the game to be based on a character trait. So, let’s say you are trying to sneak past a goblin. If there is a sneak skill in the game, the current passage would record the engine_variables.type to “sneak.” In the passage that computes skill checks, the passage would then look for the $sneak variable (a numerical value between 0 to 10) and use it to help determine success or failure.

Right now, my code for this (after declaring variables in StoryInit ) looks something like -

<<set $engine_variables to {
type: “”,
foe: “”,
dice: 0,
level: 0
}>>
<<set $sneak to 3>>

(During the story - where the current value of engine_variables.type is “sneak”)

<<set _cur_v to $engine_variables.type>>
<<set _cur_b to “$”+_cur_v>>
<<set _value to parseInt(_cur_b)>>
_cur_b

This produces an error - namely [number NaN].

I need to be able to have Twine recognize $engine_variables.type as the name of a variable so that the program can use it to access the integer value of that variable. parseInt() and Number() do not seem to work for this, and I have looked through the internet and the IF forum. Any ideas?

It’s the line that you’re setting _cur_b that’s messing up. I’m not sure on the logic behind it, but I’m guessing $ + variable name is being stored as a string of that assignment (literally the variable name with a sigil slapped onto it and not the value). It works if you print the variable directly but not if you try to do any calculations on it.

I don’t know if that’s actually what’s going on, but I do know that setting the value like the following makes it work.

<<set _cur_b = variables()[_cur_v]>>

or

<<set _cur_b = State.variables[_cur_v]>>

Edit:
To explain what’s going on here, in case you’re unaware: State.variables is the object that holds all of the story variables. So $sneak is State.variables["sneak"]. Likewise, State.temorary holds the temporary variables.

variables() and temporary() are just getter functions that return their respective objects. It saves like 4 characters of typing so it’s not a huge convenience, but oh well.

1 Like

Thank you. I am not a coder, and it surprises me that I’ve gotten as far as I have. Still, I went through the Sugarcube documentation, and I’m not sure why State.variables[] didn’t click.

Thank you. This helps me immensely.

1 Like