Problem with input spamming and _unhandled_input(event)

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

Hello,

Here is my problem:

_unhandled_input(event) is executing multiple times when spamming the PRIMARY_ACTION before _order_mover_3d_to_move() returns. _detected_mover_3d.is_moving evaluates to false multiple times after the first press of PRIMARY_ACTION. Is there some kind of special execution order in Godot that is causing this? How do I prevent this?

Thanks.

Here is my code:

# Start Node handling the input
func _unhandled_input(event: InputEvent) -> void:
	if event is InputEventJoypadMotion:
		return
	if _detected_mover_3d.is_moving:
		return
	if event.is_action_pressed(InputMappings.PRIMARY_ACTION % player_id):
		print("Primary pressed %s" % name)
		get_viewport().set_input_as_handled()
		_order_mover_3d_to_move()


func _order_mover_3d_to_move() -> void:
	_detected_mover_3d.movement_done.connect(_on_mover_3d_movement_done)
	_detected_mover_3d.move()
	print("is moving = %s" % _detected_mover_3d.is_moving)
# End Node handling the input

# Inside the _detected_mover_3d Node
var _is_moving: bool = false
var is_moving: bool: 
	get: return _is_moving


func move() -> void:
	_is_moving = true

Try checking the input in _process() instead, so you only get one input per frame.

(Also, it’s confusing having vars called “_is_moving” and “is_moving”.)

SteveSmith | 2022-12-24 12:02

I hadn’t considered polling the input. Thanks.

RudyFisher | 2022-12-24 21:04