Debug console freezes while playing the scene

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By MohamedZindha
func _physics_process(delta):
var fwd_mps = transform.basis.xform_inv(linear_velocity).x
steer_target = Input.get_action_strength("turn_left") - Input.get_action_strength("turn_right")
steer_target *= STEER_LIMIT
if Input.is_action_pressed("turn_left"):
	if steer_value > -1:
		steer_value -= 0.25
		$steeringwheel.transform.basis = $steeringwheel.transform.basis.rotated($steeringwheel.transform.basis.y.normalized(),0.25)
if Input.is_action_pressed("turn_right"):
	if steer_value < 1:
		steer_value += 0.25
		$steeringwheel.transform.basis = $steeringwheel.transform.basis.rotated($steeringwheel.transform.basis.y.normalized(),-0.25)
if Input.is_action_just_released("turn_left"):
	while(steer_value != 0):
		steer_value += 0.25
		$steeringwheel.transform.basis = $steeringwheel.transform.basis.rotated($steeringwheel.transform.basis.y.normalized(),-0.25)
if Input.is_action_just_released("turn_right"):
	while(steer_value != 0):
		steer_value -= 0.25
		$steeringwheel.transform.basis = $steeringwheel.transform.basis.rotated($steeringwheel.transform.basis.y.normalized(),0.25)
if Input.is_action_pressed("accelerate"):
	# Increase engine force at low speeds to make the initial acceleration faster.
	var speed = linear_velocity.length()
	if speed < 5 and speed != 0:
		engine_force = clamp(engine_force_value * 5 / speed, 0, 100)
	else:
		engine_force = engine_force_value
else:
	engine_force = 0

if Input.is_action_pressed("reverse"):
	# Increase engine force at low speeds to make the initial acceleration faster.
	if fwd_mps >= -1:
		var speed = linear_velocity.length()
		if speed < 5 and speed != 0:
			engine_force = -clamp(engine_force_value * 5 / speed, 0, 100)
		else:
			engine_force = -engine_force_value
	else:
		brake = 1
else:
	brake = 0.0
	
steering = move_toward(steering, steer_target, STEER_SPEED * delta)

Hi I am new to Game development.I have been trying to understand the vehiclebody node and code a very simple game.The above code runs as expected when I play the scene but
when I change the values from 1 to 2 and 0.25 to 0.1 like the below code, the debug console runs at first but like after 3 seconds it freezes.I wonder if it has to do with my system’s memory or there is something I should know.if someone knows what’s going on please help me out. Thanks!

if Input.is_action_pressed("turn_left"):
	if steer_value > -2:
		steer_value -= 0.1
		$steeringwheel.transform.basis = $steeringwheel.transform.basis.rotated($steeringwheel.transform.basis.y.normalized(),0.1)
if Input.is_action_pressed("turn_right"):
	if steer_value < 2:
		steer_value += 0.1
		$steeringwheel.transform.basis = $steeringwheel.transform.basis.rotated($steeringwheel.transform.basis.y.normalized(),-0.1)
if Input.is_action_just_released("turn_left"):
	while(steer_value != 0):
		steer_value += 0.1
		$steeringwheel.transform.basis = $steeringwheel.transform.basis.rotated($steeringwheel.transform.basis.y.normalized(),-0.1)
if Input.is_action_just_released("turn_right"):
	while(steer_value != 0):
		steer_value -= 0.1
		$steeringwheel.transform.basis = $steeringwheel.transform.basis.rotated($steeringwheel.transform.basis.y.normalized(),0.1)
:bust_in_silhouette: Reply From: jgodfrey

Your problem is very likely with while loops like this:

while(steer_value != 0):
    steer_value -= 0.1

It’s never a good idea to make equality checks against floating point values as numeric errors in the values will make the logic fail in unpredictable ways. In the above case, I assume that, as your value is decremented, it’s never exactly zero, so you’re getting permanently stuck in that while loop - hanging the engine.

For the check above, this is MUCH safer…

while(steer_value > 0):
    steer_value -= 0.1

With that, the while loop will exit as soon as it reaches a value that 0 or less. So, even if the exact value of 0 is never reached, the loop will still exit as intended.

You’ll want to make similar changes to other while loop logic also.

Thanks! I will follow that.

MohamedZindha | 2023-03-15 13:42

Also know that the incremental changes you’re making inside that while loop won’t be visible in-game. That’s because once you enter the loop, NOTHING else can happen until you exit the loop. So, you’ll see the final result of the changes that happened in the loop once the loop exits, but nothing else.

If you’re trying to make the incremental changes be visible in-game, they need to be made using a mechanism that allows the engine to continue processing at the same time. That’s what the _process() and _physics_process() methods are for…

jgodfrey | 2023-03-15 14:54

that’s very helpful.thanks!

MohamedZindha | 2023-03-16 04:44