Skip to content

"Limiting the Range of a Number": SugarCube (v2.18)#

Summary#

This example demonstrates how to limit a numeric variable to a value between a set range, this process is commonly known as clamping. It uses the Math.clamp() function in SugarCube to achieve the desired result.

Example#

Download

Twee Code#

:: StoryTitle
Limiting the range of a number in SugarCube


:: Start
Initialize the numeric variable to a value with the range you want.
eg. between ''1'' and ''10'' inclusive.
(note: You don't need to use the //Math.clamp()// function at this point.)\

<<set $valueToClamp to 5>>
''Current value'': $valueToClamp

Increase the number to a value that is ''within'' the desired range.
eg. Add 1 to the current value.\

<<set $valueToClamp to Math.clamp($valueToClamp + 1, 1, 10)>>
''New value'': $valueToClamp

Try to increase the number to a value that is ''outside'' the desired range.
eg. Add 100 to the current value.\

<<set $valueToClamp to Math.clamp($valueToClamp + 100, 1, 10)>>
''New value'': $valueToClamp

Decrease the number to a value that is ''within'' the desired range.
eg. Minus 5 from the current value.\

<<set $valueToClamp to Math.clamp($valueToClamp - 5, 1, 10)>>
''New value'': $valueToClamp

Try to decrease the number to a value that is ''outside'' the desired range.
eg. Minus 100 from the current value.\

<<set $valueToClamp to Math.clamp($valueToClamp - 100, 1, 10)>>
''New value'': $valueToClamp

Twee Download