Parser Error On '@GlobalScope' Enum Access

Godot Version

4.3

Question

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?

Thanks.

if Key not appears in autocomplete dialog, then you addressing it wrong way or form wrong place.

GlobalEnum’s not accessible by default like Keys.keys()[32].
it will print you error about “can’t be used on its own”

You can access it as const name for things that related to its enum.

or if you need exact keys, you can create your own enum with these values:
enum UsedKeys {SPACE=KEY_SPACE, PERCENT=KEY_PERCENT, # and so on...}

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())
1 Like

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.