Using variable values to create new variables

Hi, I’m fairly new to but generally getting the hang of things. I’ve been looking around and I haven’t found quite what I’m looking for, which is taking the string value of one variable and using it to create a new variable.

I’m trying to create an npc builder/randomizer which takes a name entered into a textbox or from a list, converts that name into a variable of the same name, and then passes the variable to a class constructor.
So if <<$_tempName =‘John’>>, I then want it to create a new variable called $John.
Additionally, is there a way to set if statements with undefined or null variables, i.e <<if $_tempName == undefined>>

Thank you in advance.

1 Like

Regarding undefined variables, you can use null and undefined according to the documentation.

As for the first question. Create a new game.

In your Start passage, create a list that will set $name to alice or john. Using a text box that allows any entry will be difficult because of the possibility that the player will enter whitespace and quotation marks…maybe someone else can solve that. Anyway, create two options using basic links instead.

Option 1: <<link "alice">><<set $name to "alice">><<goto passage2>><</link>>
Option 2: <<link "john">><<set $name to "john">><<goto passage2>><</link>>

Now create a passage called passage2. Using the “Stupid Print Trick”, which uses Sugarcube’s print macro, you can wrap one variable inside another.

// print the name that was chosen
<<print $name>>

// use the print trick to set the second variable ; despite using print this will be invisible
<<print "<<set $" + $name + " to 'thistext'>>">>

// add a link for the user to click
[[passage3]]

Then create a passage called passage3. Here, we’ll see the results.

Calling the $name variable will return the name that you set it to.

If you set your name to alice or john, calling $john or $alice will return “thistext.”

Using the Stupid Print Trick again will teturn “thistext” regardless of the name you set.

Name: <<print $name>>
John: <<print $john>>
Alice: <<print $alice>>
Anyone: <<print "$" + $name>>
1 Like

warning: the following code examples have not been tested.

There are two ways you can reference variables within SugarCube:

  1. using variable names that start with either a $ or a _ character.
    eg. Name: $name or Age: _age
  2. using the variables or temporary properties of the State API object.
    ex 1. Name: <<= State.variables.name>> or Age: <<= State.temporary.age>>
    ex 2. Name: <<= State.variables["name"]>> or Age: <<= State.temporary["age"]>>

The 1st example of using the State API uses Dot Notation to reference the two variables by name, the 2nd example uses Bracket Notation to reference the same variables.

You can use the Bracket Notation method to create the variables you want.

<<set $_tempName to 'John'>>
<<set State.variables[$_tempName] to 'some value'>>

The value of the John variable is $John
1 Like

Thank you very much, this is very helpful and easy to understand. It should be easier to implement my idea with this method.