Creating new variable with each <<for>> loop step | Using variable inside another variable's name

Twine Version:
Sugarcube: 2.36.1

I’m creating loot roll system. Some chests with the loot have less items, some have more.
So, I want to create a loop that checks how many item rolls left. On output I want to get the same number of different variables.
For example, if the chest contains 5 items, I want it to return me 5 variables $clothingloot1, $clothingloot2, $clothingloot3, $clothingloot4, $clothingloot5. Each of these variables should store information that was given inside the loop

So I need to change the variable inside the loop with each step and save it for later use.

That’s the code that I have atm, but it doesn’t work:

<<for _g to 0; _g lt $rewardscount; _g++>>
<<set $clothingloot[_g] to $lootrange1.pluck()>>

Your code looks like it works, but not the way you expect it to.

This implies that $clothingloot is an array. So when the loop is done, you will get the following:
$clothingloot = [item1, item2, item3, ... lastitem]

Instead of calling $clothingloot1 youcan just call $clothingloot[0] (an array starts at 0 instead of 1).

1 Like

Ok, I got the idea. $clothingloot[_g] interacts with the $clothingloot array, and [_g] in this case refers to the place of an object inside the array.
Using this information I created this:

<<set $lootrange to []>>

<<for _g to 0; _g lt $rewardscount; _g++>>
<<set $lootrange[_g] to $commonloot.pluck()>>

So on the output I get an array with all the items that were randomly picked from my items list.

My goal was to create new variable each step. However, this solutions is also ok. Thanks :slight_smile:

1 Like

I’ve just found it’s possible to create variables using other variables.
In the example below I created temporary variable _t and stored my desired dynamic variable’s name in it "$clothingloot"+_x
Then I used a weird <<print>> technique. It doesn’t print anything, but it’s needed to settle the right name of the variable.
As a result, it creates a new variable with each loop step. $clothingloot1, $clothingloot2, etc.
Each variable is a randomly picked item with it’s own values (from items list $commonloot).

<<for _x to 1; _x lte $rewardscount; _x++>>
<<set _t to "$clothingloot"+_x>>
<<print "<<set "+_t+" to $commonloot.pluck()>>">>

An easier way to do this is to use the setVar() method, like this:

<<for _x to 1; _x lte $rewardscount; _x++>>
    <<set State.setVar("$clothingloot"+_x, $commonloot.pluck())>>

That said, I don’t know what exactly you’re doing with those variables, but in general I’d recommend using an array instead, like Manon suggested.

1 Like

You should never need to use a <<print>> macro call to execute a String Representation of <<set>> macro, as SugarCube includes other ways to access variables dynamically.

So in your use-case you want to dynamically create a Story variable with a name of $clothingloot combined with a Number between 1 and $rewardscount - 1

@svlin has already explained how to do that using the State.setVar() function.

I will demonstrate how to achieve the same outcome using the State.variables object combined with Bracket Notation.

<<for _x to 1; _x lte $rewardscount; _x++>>
    <<set State.variables["clothingloot" + _x] to $commonloot.pluck() >>
1 Like

I’ll try it tomorrow and update the topic, thanks :slight_smile:

Hey. I have a question. That’s right, it creates a new variable each time. But I’m still confused with how to use this variable dynamically.
For example, I want to show a list of names from the $allactions array using <<for>>. It creates all the variables that I need, but I don’t know how to use them. Below is one of my attempts, but it doesn’t work:

\<<for _x to 1; _x lte 8; _x++>>
\<<set State.setVar("$action"+_x, $allactions.shift())>>
\<<print $action[_x].name>>

(P.S. for others. I don’t need any suggestions how to show a list of names in a better way, I know that, I just need to answer my current question)

maybe you can also help me with my latest question?

Try this:

<<print State.variables["action"+_x]>>

State.variables is an object containing the current variables in the game. To access the value of any variable, you can use State.variables["variable name here"].

1 Like

Magic… :sweat_smile:

Can you please explain a bit how to use it with widgets?
actioncard is a widget

<<for _x to 1; _x lte 8; _x++>>
<<set State.setVar("$action"+_x, $allactions.shift())>>
<<actioncard State.variables["$action"+_x]>>

Or how to store its values in another variable?

<<for _x to 1; _x lte 8; _x++>>
<<set State.setVar("$action"+_x, $allactions.shift())>>
<<set _t to State.variables["$action"+_x]>>

Or how to use it when it contains an array:

<<set $action1 to {power:40, color:"red", type:"attack", name:"Punch"}>>
<<for _x to 1; _x lte 8; _x++>>
<<print State.variables["$action"+_x].name>>

All of my examples don’t work :cry:

It needs to be State.variables["action"+_x] without the $.

1 Like

:rofl: gonna test it, one moment

thanks, it works just fine now :innocent: I feel a bit stupid :sweat_smile: