Hi there,

I would like to get information in _input() method about the action. Usually we have

func _input(event):
	if input.is_action("my_action_name"):
			#do things!

But I would like to request as a following:

func _input(event):
	if event is InputEventAction: #this test ever seems to result false !
		# I can get the name of the Action
		match event.action:
			"my_action_name":
				#do things
			"my_another_action_name":
				#do other actions

That means, I would like to get, if any, the action string associated to the input event.

Thanks in advance.

This discussion was caught in the moderation queue since you have not confirmed your account yet.
Upon creating your account you should have received an account verification email. The confirmation email may have been incorrectly flagged as spam, so please also check your spam filter. Without confirming your account, future posts may also be caught in the moderation queue. You can resend a confirmation email when you log into your account if you cannot find the first verification email.
If you need any help, please let us know! You can find ways to contact forum staff on the Contact page. Thanks! :smile:

Say for an example if you want to get a key mapped to an action to report to the player what key to press(maybe your game has input remapping supported in options so you can't hard code what to print to the player) you would use the InputMap to do so.

interact_event_binding = InputMap.get_action_list("use_interact") #get_action_list doesn't get a list of actions but a list of keys mapped to the specified action as an array

InfoLabel.set_text("Press " + interact_event_binding[0].as_text() + " to use") #note that we are accessing interact_event_binding as an array and picking the first element in the array here, first being index 0

In response to the info given to the player by the label player presses key triggering our "use_interact" action:

func _input(event: InputEvent) -> void:
    if event.is_action_pressed("use_interact"):
                #Do something in response to the action triggered by the key reported in above example
		InfoLabel.set_visible(false)

Not sure if that is exactly what you are trying to do but maybe a useful example of what can be done. As for

@AnarcoPhysic said:

func _input(event):
	if event is InputEventAction: #this test ever seems to result false !
		# I can get the name of the Action
		match event.action:
			"my_action_name":
				#do things
			"my_another_action_name":
				#do other actions

perhaps try

if event is InputEvent:
	print("an input event was triggered")
	print("the triggered event is " + event.as_text())

instead? edit: no that still gives us the mapped key...

if event is InputEvent:
	print("an input event was triggered")
	if InputMap.event_is_action(event, "use_interact"):
		print("the triggered event is action use_interact")

works to check if event is a specific action though

edit 2: oh wow is match a mess to use. And apparently slower than multiple if statements.

I just tried, it doesn't look like you can do that.

Does using the action property not work? It should be a string with the name of the action of the input event in the project settings input map, if I am reading the documentation correctly, which I think is what you are looking for. You could try print(event.action) and I think it should print the name of the action (if the InputEvent is bound to one, not sure what happens if its not).

Think I tried that but I haven't used match for myself before so maybe i just tried it wrong.

Some points to say:

  1. Thanks you all for your valuable help ! (@DaveTheCoder)
  2. event.as _text() doesn't help too much. It seems to be equivalent to event.scan_code returning string stead of integer. But in terms of logical and readability, works likely, in my opinion. (@Megalomaniak)
  3. I don't found nothing about InfoLabel object. Would it be a instance of a textual object?

I want to create a create a code to work over input oriented by the action declare in inputmap. I will try a better example.

func _input(event):
	#Does something trigger _input with a InputEventAction instance as parameter!? If not, why to make InputEventAction as descendent of InputEvent?
 if event is InputEventAction:
		#Now I now  I can get the property action name, identifying the input
		match event.action:
		"ui_up":
			move_and_collide(Vecto2.UP)
		"ui_down":
			move_and_collide( Vecto2.DOWN)
		 "ui_right":
			move_and_collide( Vecto2.RIGHT) 
		"ui_left":
			move_and_collide( Vecto2.LEFT)
		 _:
				pass

I think that using this approximation for handling inputs, I will have a good maintainability for futures changes in my project to this point.

Any suggestion is appreciable and welcome. :-)

Its just a label node with a changed name. Or rather in the script it is a variable with the get_node function pointed to the label node.

6 months later

Hi, sorry for necroposting, but this is the top Google result for this question and I thought it would be useful to have a clear answer.

You can use the InputMap to get a list of all defined actions, and to check if an InputEvent matches a defined action. This allows you to determine the action name inside _input:

func _input(event):
	var action = null
	for _action in InputMap.get_actions():
		if InputMap.event_is_action(event, _action):
			action = _action

	match action:
		"my_action_name":
			#do things
		"my_another_action_name":
			#do other actions

EDIT: The above will only work for taps, not continuous presses. You can use a similar technique using polling to handle both cases:

func _physics_process(delta):
	for action in InputMap.get_actions():
		if Input.is_action_just_pressed(action): # handle taps
			match action:
				'jump':
					# do a jump
				'shoot':
					# shoot your gun
		if Input.is_action_pressed(action): # handle continuous presses
			match action:
				'walk_left':
					# walk left
				'walk_right':
					# walk right
a year later