Why am i printing empty [] when clearly i have stuff in my inputmap

Godot Version

Gotdot 4.2.2

Question


Part of my code:
func _ready():
set_process_unhandled_key_input(false)
set_action_name()
call_deferred(“set_text_for_key”)

func set_text_for_key() → void:
if InputMap.has_action(action_name):
var action_events = InputMap.action_get_events(action_name)
print(“Action events for”, action_name, “:”, action_events)

action_name does not seem to be defined. Could you show your whole script? Also, what is the exact printing output?

1 Like

Oh yeah mb class_name Key_Rebind
extends Control

@onready var label = $HBoxContainer/Label as Label
@onready var button = $HBoxContainer/Button as Button

@export var action_name:String = “move_right”

func _ready():
set_process_unhandled_key_input(false)
set_action_name()
call_deferred(“set_text_for_key”)

func set_action_name() → void:
label.text = “Unassigned”

match action_name:
	"move_left":
		label.text = "Move Left"
	"move_right":
		label.text = "Move Right"
	"jump":
		label.text = "Jump"
	"cube transform":
		label.text = "Cube Transformation"
	"triangle transform":
		label.text = "Triangle Transformation"
	"reset":
		label.text = "Reset"

func set_text_for_key() → void:
if InputMap.has_action(action_name):
var action_events = InputMap.action_get_events(action_name)
print(“Action events for”, action_name, “:”, action_events)

func _on_button_toggled(toggled_on):
if toggled_on:
button.text = “Press any key…”
set_process_unhandled_key_input(toggled_on)

	for i in get_tree().get_nodes_in_group("hotkey_button"):
		if i.action_name != self.action_name:
			i.button.toggle_mode = false
			i.set_process_unhandled_key_input(false)
else:
	for i in get_tree().get_nodes_in_group("hotkey_button"):
		if i.action_name != self.action_name:
			i.button.toggle_mode = true
			i.set_process_unhandled_key_input(false)
	set_text_for_key()

func _unhandled_key_input(event):
rebind_action_key(event)
button.button_pressed = false

func rebind_action_key(event) → void:
InputMap.action_erase_events(action_name)
InputMap.action_add_event(action_name, event)
set_process_unhandled_input(false)
set_text_for_key()
set_action_name()

The exact printing output is






I changed my print output to print(“Action events for”, action_name, “:”, action_events) which prints a mess but if I change it back to print(action_events) then it would print the above

The only possible issue that I see right now is that the action_name variable is a @export variable which means that you may unintentionally have changed its initial value in the editor.

2 Likes

Thank you! I found out why it was because of another script.

1 Like

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