Why does the player keep walking?

Godot Version

v4.6.stable.official [89cea1439]

Question

Why does the player keep walking when I press pause? There is a stop variable, but the player does not stop.

player_script.gd


var can_move: bool = true

func _physics_process(delta: float) -> void:
	if !can_move:
		return

	else:
		move_and_slide()

func _unhandled_input(event: InputEvent) -> void:
	direction.x = int(Input.get_axis("left", "right"))
	velocity.x = direction.x * SPEED

----------------------------------------------------------------


pause_menu.gd


func _unhandled_input(event: InputEvent) -> void:
	if event.is_action_pressed("pause"):
		pause.visible = not pause.visible
		player.can_move = not player.can_move
		con_button.grab_focus()






How do you get the reference to the player in the pause_menu script? How is the player variable initialized?

I use “@export

If you don’t have any errors and the change of pause.visibile works, I would double check if the player reference is set correctly in the inspector. You could also try print(player.get_path()) in the pause function and check if the path matches the player’s position in your scene tree.

The engine shows everything correctly - /root/Player

However, I noticed that if you change the player’s can_move to false, he continues to move. The variable simply does not apply.

Player is the topmost node in your game scene? It doesn’t have a parent node?

You mean even if you change the initial value in the player script? Are you chaning that variable anywhere else?

Yes, false and true do not affect the player, he continues anyway. It is only in the player’s script.

Of course Player is the top node in the player scene, it just wasn’t clear to me that (apparently) you were running this scene isolated (and not as part of some game/level scene).


If it’s only about the can_move variable, I would start by making sure nothing is overwriting its value. I would try adding a setter function with print_stack():

var can_move: bool = true:
	set(value):
		print_stack()
		can_move = value

Then you can see from where this variable is getting changed.

1 Like

Thank you very much, the problem is finally solved. :+1:

1 Like