Input mapping interesting behaviour

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

I was having some Input detection problems and as usual i checked to see if i could find a solved question. Many people appear to have the same problem. Pressing a key, then while this said key is pressed, pressing another key and then releasing the originally pressed key. Most solved questions point out that the _input() function only detects the last event and that the _process() function should be used instead.

Well in my case i’m using _process() and defined in my project’s input map an action called “movement” and two more actions called “left” and “right”. The code looks something like this:

func _process(delta):
if(Input.is_action_pressed("movement")):
	direction.x = int(Input.is_action_pressed("right")) - int(Input.is_action_pressed("left"));
	#more validation code

It appears that if press the pattern i described before (Right then Left and release Right while keeping Left pressed and then release Left) and print out the values detected, the debug shows:
L: 0 R: 1

L: 1 R: 1

L: 0 R: 0
What fixed it in the end was to get rid of the “movement” action and thus the code to this:

func _process(delta):
if(Input.is_action_pressed("right") || Input.is_action_pressed("left")):
	direction.x = int(Input.is_action_pressed("right")) - int(Input.is_action_pressed("left"));
	#use direction for something

I don’t get why it worked. Is there something special about the way Godot handles input? Should i add the issue to the repository?

  • Movement was mapped to WASDQE. Left and right where mapped to A and D respectively.
:bust_in_silhouette: Reply From: flurick

This depends on what you have set the “movement” action to, if it is set to the same as “left” it would never get to run the code for pressing “right”. You can skip that first check and maybe even use Input.get_action_strength() instead.

func _process(delta):
  direction.x = Input.get_action_strength("ui_left") - Input.get_action_strength("ui_right")