Coding Help with Widget

Twine Version: 2.36.1
Hello I am having issues creating a widget that easily updates a variable and an image (on the sidebar). So far, here is the widget I have:

<<widget energy>><<nobr>>
<<set $energy += $args[0]>>
<<if $energy gte 50>>
	<<replace "#energy">><img src="image.png"><</replace>>
<<else>>
	<<replace "#energy">><img src="image.png"><</replace>>
<</if>>
<</nobr>><</widget>>

It’s working, and I like being able to use the widget to easily add to the variable(+ updating the sidebar image) by doing <<energy +10>> or -10, etc., but I’m not sure how to implement having a $maxEnergy variable into the code, to cap the energy variable between 0 and 100. I’m sure the solution is simple but I’m very new!

Here is a different bit of code that I’m also using, but I’m not sure if it would work in the widget.

<<set $energy to Math.clamp($energy + 10, 0, $maxEnergy)>>

Thank you in advance.

Yeah, take that last line and replace the $energy + 10 with $energy + $args[0] and then you should be able to put it right into your widget instead of the <<set $energy += $args[0]>>.

Also, if your $maxEnergy is always going to be 100, you could just write 100 and not use a $maxEnergy variable at all…

Thanks! And yea I plan on having the $maxEnergy be increased later on.

1 Like