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()>>
<</for>>
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()>>
<</for>>
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
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()>>">>
<</for>>
@LatonFeeds
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