+2 votes

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.

in Engine by (106 points)

2 Answers

+10 votes
Best answer

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

num = clamp(num, 0, 3)
by (4,179 points)
selected by

Nice it worked too!

Why am i getting this error when i use clamp()?
–3 votes

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
by (177 points)

Thanks it worked!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.