+2 votes

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.

in Engine by (104 points)

2 Answers

+8 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,101 points)
selected by

Nice it worked too!

Why am i getting this error when i use clamp()?
–1 vote

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 (175 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 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 webmaster@godotengine.org with your username.