Is it good performance wise and generally to use a clamp function in _process()

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

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

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

skysphr | 2021-10-26 10:28

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

tofu | 2021-10-26 10:41

:bust_in_silhouette: Reply From: DaddyMonster

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.

good to know, thanks!

tofu | 2021-10-26 08:09