Getting the int value from string representation of a global enum

Godot Version

v4.4.1.stable.official [49a5bc7b6]

Question

So how do I get the actual int value from the string representation of a global enum like JOY_BUTTON_A?

Say var btn_idx_str = "JOY_BUTTON_A", none of these worked:

Ask ClassDB

Of course will fail as ClassDB does not have a “enum class” or “global class” in its registry…

var btn_idx := ClassDB.class_get_integer_constant("JoyButton", btn_idx_str) as JoyButton

GPT dreamt of this, which I knew was too good to be true:

var btn_idx := ClassDB.class_get_integer_constant("@GlobalScope", btn_idx_str) as JoyButton

Eval

Expression is known for being cripple…

var btn_idx_expr := Expression.new()
btn_idx_expr.parse(btn_idx_str)
var btn_idx := btn_idx_expr.execute() as JoyButton

Giving it a context did not help:

# init `btn_idx_expr` ...
var btn_idx := btn_idx_expr.execute([], self) as JoyButton

Bare getter

No such a thing in self.get

var btn_idx := get(btn_idx_str) as JoyButton

As far as I can tell it’s not possible to do what you need. @GlobalScope is not registered as a class in ClassDB

You could copy&paste the enum in a script and use that one instead to get the value.

I’m not sure I understand what you want to do…

Are you in a situation where all you have is the String representation of the Enum entry?

Because just using JOY_BUTTON_A will give you the int value


You can’t really access global enums with strings like you can with normal enums, using Enum.get()

There’s also the values in the documentation for JoyButton

Hey thanks for the reply. Yeah it looks like only thing I can do is to map all possible string literals to values, sadly.

Hey thanks for confirming that. Okay, guess I can still live with a manual copy paste.

Cheer curiosity:

Is it for mapping the player’s saved controls scheme or something?

I’m writing a gamepad emulator lol:

I want to fire InputEventJoypadButton, and to avoid hard-coding an event for every button I came up with the idea to type the enumeration into the button’s text property. But what an idea.

I’m writing a gamepad emulator

Neat !

to avoid hard-coding an event for every button I came up with

It would be way more elegant indeed, sad that Global enums are weird special guys, I’m guessing it’s because they are declared in the C++ code and are not gdscript objects

Well good luck! It’s a cool project, I never thought of doing something like that with Godot.

Sorry there was no easy solution…

Thanks! At least the try gave us some (nerdy) fun thinking about what the script can’t do.

I don’t know anything about this and have probably misunderstood your question, but just taking a guess. If the buttons are stored in an enum and have button indexes, wouldn’t the indexes correspond with the enum index? In that case you could just not append each index into a non-global enum/array/dictionary and get the ints from there? As it sounds like the problem is that global enums dont have this functionality.

In that case you can just do @export var button: JoyButton and you’ll get a list in the inspector of all the entries in that enum.

If I interpreted correctly, I think you meant this:

And yes! It’s a really smart way of encoding the value in the the index. However my concern is: if we can represent an enum in its symbolic form, we don’t want to rely on its integeral form in any way (incl. where implicitly bound to array indices), because the latter is subject to change (in future engine versions).

mrcdk you are the Godot master.

In the end the Godotiest way wins!