Creating a stat assignment screen with buttons that draw from an available points pool, with min and max levels

Twine Version: 2.5.1.0
Sugarcube2

Full disclosure, I’m new to all this, and haven’t had much success following documentation and forums for this particular issue. This might be a simple thing, but I’m hoping someone can help or point me in the right direction.

I am trying to create a stat assignment screen which uses buttons. What I have been able to work out on my own is the buttons and counter displays for remaining points and the stat’s level, but I can’t figure out how to make the stat draw from the points remaining without the math freaking out.

Ideally, my goals are:

  • To limit the minimum and maximum points that can be assigned to a stat, i.e. 1 and 20.
  • Each time a point is added or subtracted, the stat level and the remaining points total update accordingly.
  • Each stat draws from the same pool of points.

The goal of this setup is allowing for a varied distribution of points by the reader, rather than random assignment. For example, using most points on strength and having only a few left for charisma, and an button interface to do it with.

The below code does most of this, but I can’t work out how to both set a maximum value for a skill and also limit the maximum to the number of points are left.
I could set the maximum to the $points variable, but before spending points that would allow for more than 20.

<<set $str to 0>>
<<set $points to 20>>

@@#add;<<button "+1">>\
	/<<set $str to Math.clamp($str + 1, 0, 20)>>
    <<replace "#str">><<print $str>><</replace>>
    <<set $points to Math.clamp($points - 1, 0, 20)>>
    <<replace "#points">><<print $points>><</replace>>/
<</button>>@@

Strength:<span id="str"><<print $str>></span>

@@#subtract;<<button "-1">>\
	/<<set $str to Math.clamp($str - 1, 0, 20)>>
    <<replace "#str">><<print $str>><</replace>>
    <<set $points to Math.clamp($points + 1, 0, 20)>>
    <<replace "#points">><<print $points>><</replace>>/
<</button>>@@

Points left:<span id="points"><<print $points>></span>

Apologies for rambling a bit, and if this is in the wrong place.
Any help would be greatly appreciated.
Cheers!

1 Like

You could simply use an <<if>> macro to check whether the stat is below the maximum and whether there are points left, and only increase the stat if appropriate:

<<set $str to 0>>
<<set $points to 20>>

@@#add;<<button "+1">>
	<<if $points > 0 and $str <= 20>>
		<<set $str++>>
		<<replace "#str">>$str<</replace>>
		<<set $points-->>
		<<replace "#points">>$points<</replace>>
	<</if>>
<</button>>@@

Strength: <span id="str">$str</span>

@@#subtract;<<button "-1">>
	<<if $str > 0>>
		<<set $str-->>
		<<replace "#str">>$str<</replace>>
		<<set $points++>>
		<<replace "#points">>$points<</replace>>
	<</if>>
<</button>>@@

Points left: <span id="points">$points</span>

(As a note, you don’t need the <<print>> macro if you just want to print a variable. Just writing $points will do.)

1 Like

That is perfect, thank you so much!

1 Like