Functions and variables inside <<for>> loop

I’m trying to randomly distribute stat points when on equipment roll.
I have 4 main parameters, and I want to randomly distribute 16 stat points between them. I also don’t want to repeat my code 16 times on my own. I think it’s possible to use <<for>> looping in this case, I just don’t know how to do it right.

I have that version, but It works wrong. “range is not a function”. So I need a function, or what?

<<set $range(0,$statpoints)>>

<<for _i to 0; _i lt $range; _i++>>
<<set _dice to random(1,4)>>
<<if _dice eq 1>>
<<set $clothingloot.sub++>>
<<elseif _dice eq 2>>
<<set $>>
<<elseif _dice eq 3>>
<<set $>>
<<elseif _dice eq 4>>
<<set $clothingloot.att++>>

<<set $range(0,$statpoints)>>
SugarCube thinks this is a function. And since $range is not a function, it’s giving you an error.`

What are you trying to do with this? get a random number between 0 and the $statpoitns value?
If the latter:
<<set $range to random(0,$statpoints)>>

1 Like

0 is 0, $statpoints is a variable with the number of stat points. My idea was to make an array from 0 to $statpoints, which is eq to 16 in this case. Because I need all 16 steps, 16points=16 steps (0,1,2,3,4,5…15,16).

Maybe I just don’t know how to make an array from 0 to variable’s value…

An array is set with these brackets [] instead of parentheses.

But you could just do:
<<for _i to 0; _i lt $statpoints ; _i++>>
There no real need for an array in this case. This will loop until _i reaches the value of $statpoints.

1 Like

lol, it just works. I think I finally got how looping works :sweat_smile: Thanks :smiley:

1 Like