How to set a maximum and a minimun value of a int/float?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By MaximoTG98

Hi,
So let’s say a have a variable called number,and two buttons adding and subtracting “1” to that value.If i keep spaming the button the number variable will keep rising without a limit,but i just want it to stop until it is 0 (in case of subtracting) or 3 (in case of adding).
i tried to use the clamp() function,but it returns a value instead limiting the value.
Have a good day.

:bust_in_silhouette: Reply From: ZacB

When you press the button “+” :

if your_variable + your_increment <= 3:
	your_variable += your_increment

When you press the button “-” :

if your_variable - your_decrement >= 0:
	your_variable -= your_decrement

Thanks it worked!

MaximoTG98 | 2018-08-21 15:57

:bust_in_silhouette: Reply From: Dlean Jeans

I think you might have made use of clamp() the wrong way. The correct way:

num = clamp(num, 0, 3)

Nice it worked too!

MaximoTG98 | 2018-08-21 16:02

1 Like