The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

+1 vote

I need to clamp a variable that the user can increment with input therefore I have been clamping it in the process function. Wondering if that is a good idea or if there is anyway else I could do it.

this is what I have so far:

func _process(delta):
print(sail_angle)
if Input.is_action_pressed("sail_right"):
    sail_angle += 0.1
if Input.is_action_pressed("sail_left"):
    sail_angle += -0.1
sail_angle = clamp(sail_angle, -70, 70)

I mean I could also clamp it after the input action:

func _process(delta):
print(sail_angle)
if Input.is_action_pressed("sail_right"):
    sail_angle += 0.1
    sail_angle = clamp(sail_angle, -70, 70)
if Input.is_action_pressed("sail_left"):
    sail_angle += -0.1
    sail_angle = clamp(sail_angle, -70, 70)

but then I am repeating code

in Engine by (24 points)

You probably want to do that in _physics_process though, otherwise your mechanics are going to be affected by drawing frame rate.

hmm possibly, but its related to input though, how would you propose i do it?

1 Answer

+1 vote
Best answer

Nothing wrong with the way you've done it in the first snippet at all. Like you say, the 2nd repeats code unnecessarily. Clamp isn't unduly expensive, all it is in essence is a max and a min together and using it in _process is just fine, won't cause you any problems at all.

by (2,159 points)
selected by

good to know, thanks!

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.