#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Randomize stats?

So the player can plus or minus their stats up to 5 in each stat with 12 points total. That part works.

But I'd also like them to just hit a link to roll their character.

How do I have each stat be 0-5 and also have the total not go over 12?
```<<set \$stealth  to either (0,1,2,3,4,5)>>
<<set \$spells to either (0,1,2,3,4,5)>>
<<set \$strength  to either (0,1,2,3,4,5)>>
<<set \$smarts to either (0,1,2,3,4,5)>>
<<set \$style to either (0,1,2,3,4,5)>>
```

Twine 1.4 and Sugarcane

• I figured it out with a loop. Was able to make so the Randomize link set the random values above, but only so that they collectively added up to exactly 12.
• Here's how I did it

Start
```<<set \$points to 12>><<set \$stealth to 0>><<set \$spells to 0>><<set \$strength to 0>><<set \$smarts to 0>><<set \$style to 0>>
```

Stats
```Hi <<print \$name>>.

You have <<print \$points>> points to spend on your stats (Max 5 each):
<<if \$loop>><<loop>><<endif>>
<strong>
<tr>
<td><span class=stealth>Stealth</span></td>
<td><<print \$stealth>></td>
<td><<if \$points != 0>><<if \$stealth neq 5>> [[+1|Stats][\$stealth += 1,\$points -= 1]]<<endif>><<endif>> </td>
<td><<if \$stealth >0>>[[-1|Stats][\$stealth -= 1,\$points += 1]]<<endif>></td>
</tr>
<tr>
<td><span class=spells>Spells</span></td>
<td><<print \$spells>></td>
<td><<if \$points != 0>><<if \$spells neq 5>>[[+1|Stats][\$spells += 1,\$points -= 1]]<<endif>><<endif>></td>
<td><<if \$spells >0>>[[-1|Stats][\$spells -= 1,\$points += 1]]<<endif>></td>
</tr>
<tr>
<td><span class=strength>Strength</span></td>
<td><<print \$strength>></td>
<td><<if \$points != 0>><<if \$strength neq 5>> [[+1|Stats][\$strength += 1,\$points -= 1]]<<endif>><<endif>></td>
<td><<if \$strength >0>>[[-1|Stats][\$strength  -= 1,\$points += 1]]<<endif>></td>
</tr>
<tr>
<td><span class=smarts>Smarts</span></td>
<td><<print \$smarts>></td>
<td><<if \$points != 0>><<if \$smarts neq 5>> [[+1|Stats][\$smarts += 1,\$points -= 1]]<<endif>><<endif>></td>
<td><<if \$smarts >0>>[[-1|Stats][\$smarts -= 1,\$points += 1]]<<endif>></td>
</tr>
<tr>
<td><span class=style>Style</span></td>
<td><<print \$style>></td>
<td><<if \$points != 0>><<if \$style neq 5>> [[+1|Stats][\$style += 1,\$points -= 1]]<<endif>><<endif>></td>
<td><<if \$style >0>>[[-1|Stats][\$style -= 1,\$points += 1]]<<endif>></td>
</tr>
</table>
</strong>
[[Randomize your points here.|Stats][\$loop to 1;\$total to 0;\$points to 0]]

<<if \$points < 1>>[[Continue with these stats|Perks]]<<endif>>
```

Then the passage 'loop'
```<<nobr>>
<<if \$total != 12 and \$i < 10000000>>
<<set \$stealth to either (0,1,2,3,4,5)>>
<<set \$spells to either (0,1,2,3,4,5)>>
<<set \$strength  to either (0,1,2,3,4,5)>>
<<set \$smarts to either (0,1,2,3,4,5)>>
<<set \$style to either (0,1,2,3,4,5)>>
<<set \$total =  (\$stealth + \$spells + \$strength + \$smarts + \$style) >>
<<set \$i = \$i + 1>>
<<loop>>
<<else>>
<<set \$i = 0>>
<<endif>>
<<endnobr>>
```

The player is presented with the options to distribute 12 points with plus or minus signs, never going over 5 in each, and never spending more than 12. But then they can hit Randomize and the loop runs, distributing the 12 points evenly, which can then be further edited by the player with the plus/minus links.
• That loop looks super inefficient, and could possibly go through very many iterations before ending. I don't know how much Sugarcane can do in terms of Arrays and random selection, but you could instead run a loop which iterates 12 times. Each iteration adds 1 to a randomly selected variable and adds to a counter. Once it hits 12 on the counter, end the loop and you'll have added 12 total points to your variables.
• Try something like the following example, which consists of three passages.

```<<set \$stealth to 0>>
<<set \$spells to 0>>
<<set \$strength to 0>>
<<set \$smarts to 0>>
<<set \$style to 0>>
<<set \$pool to 12>>
```

2. The passage in which you startup the randomisation process.
```<<silently>><<display "Randomize Stats">><<endsilently>>

stealth: <<print \$stealth>>
spells: <<print \$spells>>
strength: <<print \$strength>>
smarts: <<print \$smarts>>
style: <<print \$style>>
```
note: <<silently>> is used to suppress any visual output generated by the Randomize Stats passage.

3. The Randomize Stats passage
```<<set \$stat to random(1,5)>>
<<if \$stat is 1 and \$stealth < 5>>
<<set \$stealth += 1>>
<<set \$pool -= 1>>
<<else if \$stat is 2 and \$spells < 5>>
<<set \$spells += 1>>
<<set \$pool -= 1>>
<<else if \$stat is 3 and \$strength < 5>>
<<set \$strength += 1>>
<<set \$pool -= 1>>
<<else if \$stat is 4 and \$smarts < 5>>
<<set \$smarts += 1>>
<<set \$pool -= 1>>
<<else if \$stat is 5 and \$style < 5>>
<<set \$style += 1>>
<<set \$pool -= 1>>
<<endif>>
<<if \$pool > 0>>
<<display "Randomize Stats">>
<<endif>>
```
• edited February 2017
in greyelf's passage 3 he erroneously wrote <<else if>> instead of <<elseif>> . Fix that and it works great!

It's cool to see the <<display>> macro being used as a psuedo if/for? loop.
• edited February 2017
in greyelf's passage 3 he erroneously wrote <<else if>> instead of <<elseif>> . Fix that and it works great!
@spaceranger123 Did you actually create a story project and test my examples?

The <<else if>>'s in the third example of my previous solution are correct syntax for the Sugarcane story format, which @Muze stared they are using, and the last example in the relevant Twine 1 documentation shows the usage of that syntax. The same documentation explains that you can also use the <<elseif>> syntax if you prefer.

I generally build (and run) a test-case story project for (almost) every one of my solutions before posting them on the forums, that does not mean that they never contain errors nor that errors aren't introduced during the transfer process.
• @greyelf Yes, in 2.0. Forgot this was 1.0 format.
• @spaceranger123 You also seem to be confusing SugarCube with Sugarcane. They are distinct story formats. There are better ways to do this sort of thing in SugarCube.