How to get my own defined Actions only for Input Map by code?

Godot Version

4.3

Question

I am getting all Input Maps by code as follow:

func list_all_inputs():
	var actions = InputMap.get_actions()  # Get all action names
	for action in actions:
		print("Action: " + action)
		var events = InputMap.action_get_events(action)  # Get list of input events for the action
		for event in events:
			if event is InputEventKey:
				print("  - Key: " + str(event.as_text_physical_keycode()))
			elif event is InputEventMouseButton:
				print("  - Mouse Button: " + str(event.button_index))
			elif event is InputEventJoypadButton:
				print("  - Joypad Button: " + str(event.button_index))
			elif event is InputEventJoypadMotion:
				print("  - Joypad Motion: Axis: " + str(event.axis) + " Value: " + str(event.axis_value))
			else:
				print("  - Other event: " + str(event))

The code lists everything out including all the Built-in Actions such as ui_accept, etc., but I would like to retrieve only the Actions I defined (the “NOT” Built-in Actions). Is there a built-in method for that or must I do this manually for this case?

There is no method but you can still do something like this to filter your custom actions from the list:

# Define your custom actions
var custom_actions = ["move_left", "move_right", "jump", "shoot"]

# Function to get only custom actions
func get_custom_actions() -> Array:
    var actions = InputMap.get_actions()
    var filtered_actions = []
    
    for action in actions:
        if custom_actions.has(action):
            filtered_actions.append(action)
    
    return filtered_actions

You do have to manually make your custom ones into another array, but it shouldnt take too long i hope…

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.