Is it possible to access InputMap actions in exported properties?

Godot Version

4.2

Question

For my project, I’m making “prompted interactable” scene, and I want to be able to choose which action from the Input Map will allow the user to interact with the scene. I’d like to use an exported property (@export) and have it grab the list of actions from InputMap and let me choose which of those to use. Is this possible? I don’t think it can be done with the simple @export or @export_enum but perhaps with some of the advanced exports?

I think so, if I have understood your problem. InputMap docs

Example: erase Enter Key and add Space Key.


func _ready():
	var ie_space = InputEventKey.new()
	ie_space.physical_keycode = KEY_SPACE

	var ie_enter = InputEventKey.new()
	ie_enter.physical_keycode = KEY_ENTER

	InputMap.action_erase_event("interact", ie_enter)
	InputMap.action_add_event("interact", ie_space)

func _physics_process(delta):
	if Input.is_action_just_pressed("interact"):
		print("interact")

Hi,

Thanks, but that’s not what I need. I’ve done a bit more research and I’ve used this to get the list of actions selected -

func _get_property_list():
	var actions = InputMap.get_actions()
	var hint_string = ""
	for action in actions:
		hint_string += action + ","
	var properties = []
	properties.append({
		"name": "prompt_action",
		"type": TYPE_STRING_NAME,
		"hint": PROPERTY_HINT_ENUM,
		"hint_string": hint_string
	})
	return properties

Good News - This is getting a list of actions that I can select!
Bad News - It’s giving me the list of actions for the EDITOR, not for my game! Any ideas on how I can get the input map for the GAME I’m editing, not the EDITOR?

They should be one and same. All the ui_ actions are in the game as well.

You can get the inputs from the game using ProjectSettings like this:

func _get_property_list():
	var actions = []
	for prop in ProjectSettings.get_property_list():
		var prop_name:String = prop.get("name", "")
		if prop_name.begins_with('input/'):
			prop_name = prop_name.replace('input/', '') 
			prop_name = prop_name.substr(0, prop_name.find("."))
			if not actions.has(prop_name):
				actions.append(prop_name)
	
	var hint_string = ",".join(actions)
	
	var properties = []
	properties.append({
		"name": "prompt_action",
		"type": TYPE_STRING_NAME,
		"hint": PROPERTY_HINT_ENUM,
		"hint_string": hint_string
	})
	return properties
2 Likes

Fantastic! I thought that this might be what was needed, but this got everything up and running! It still had all the UI parts, so I added a check to see if the prompt began with UI as well. Thank you!

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