I am working on configurable input actions for my project, and when trying to access the Key enum from ‘@GlobalScope’, I am getting this error;
print(Key[“32”]) # I want it to return “KEY_SPACE”
^ Parser Error: Identifier not found: Key
I tried looking on how to reference GlobalScope (like how you can with other globals, or how you can reference a class with ClassDB) and nothing would work.
How do I reference @GlobalScope like other globals so I can use the Key (and other) enums?
Can’t access enums like that, the name KEY_SPACE isn’t a string and isn’t accessable as a string, at least I’m certain the C++ constant ones like Key arent.
If you want to get an input’s name you could use as_text_keycode on the event
func _input(event: InputEvent) -> void:
if event is InputEventKey:
print(event.as_text_keycode())
Thanks!
I was using them wrong; and after some messing about I was able to get what I needed working, which for anyone in the future reading this is;
const key_keycodes : PackedInt32Array = [4194304, ...] # all the keycodes under the 'Key' enum
const mouse_keycodes : PackedInt32Array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # all the button indices under the 'MouseButton' enum
func get_input_event(keycode : int) -> InputEvent:
if keycode in key_keycodes:
return (func(): var key = InputEventKey.new(); key.keycode = keycode; return key).call()
elif keycode in mouse_keycodes:
return (func(): var key = InputEventMouseButton.new(); key.button_index = keycode; return key).call()
return null
This worked for me, as I was trying to get the Input actions to allow for the user to set their keybinds, so using this I was able to set the actions under all the InputMap events.