-
-
Notifications
You must be signed in to change notification settings - Fork 21.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a simpler way to get string values from enums #31896
Comments
Generally speaking I'd prefer something like I quickly created a GDScript equivalent: func find_key(dictionary, value):
var index = dictionary.values().find(value)
return dictionary.keys()[index] |
Maybe this is already known, but just want to add my bit: Im currently trying to use Enum-Values for my resources/nodes, as export variables (to have them as options for easy setup and consistency throughout the code) But with the current concept, if i save the variables to a file, i would just save the int of the enums (position in the enum-"dict"). So when i change the order of any enum, I would destroy the compatabilty of saves. So here its either: build a workaround solution or use strings directly instead of enums. I personally would prefer if it would be able to just have the direct information of string <-> enum (but as optional possibility). Akin to java's handling of enums. Edit: This also affects "export (ENUM)" fields of resources, as they also only save the int-value and not the enum per se (or a string to which the enum may be directly linked) |
Is this about retrieving value keys for Enums, like this? |
Also check out godotengine/godot-proposals#255 since it addresses a similar problem. |
Superseded godotengine/godot-proposals#255 - or consider creating a new proposal if that one doesn't fit completely, as proposals are now moved to that repository :) |
Godot version:
Godot 3.1
Issue description:
Currently, getting a String from a GDScript
enum
value is hard, and requires either a separate mapping dictionary/array. Ideally, there should be a way to get the key of any enum (script or otherwise), for easy debugging and potential use in UIs.Some ideas:
Dictionary.reverse()
which turns{a: b, c: d}
into{b: a, d: c}
, likely erroring on duplicates. This would remove the need for a separate mapping.{"enum_key": 4}
into{"enum_key": 4, 4: "enum_key"}
. This would break backwards compatibility, and turns enums into a bigger pile of hacks.Variant::Enum
which contains both anint
and aStringName
. It would be implicitly casted to both strings and ints, so printing an enum value would automatically print its name. Adding more types toVariant
is not cheap though.The text was updated successfully, but these errors were encountered: