Skip to content
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

Closed
bojidar-bg opened this issue Sep 2, 2019 · 5 comments
Closed

Add a simpler way to get string values from enums #31896

bojidar-bg opened this issue Sep 2, 2019 · 5 comments

Comments

@bojidar-bg
Copy link
Contributor

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:

  • Add 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.
  • Add reverse entries to the enum dictionary, turning {"enum_key": 4} into {"enum_key": 4, 4: "enum_key"}. This would break backwards compatibility, and turns enums into a bigger pile of hacks.
  • Add Variant::Enum which contains both an int and a StringName. It would be implicitly casted to both strings and ints, so printing an enum value would automatically print its name. Adding more types to Variant is not cheap though.
@TheFlamyy
Copy link
Contributor

Generally speaking I'd prefer something like Dictionary.find_key(value). Technically it could go hand in hand with a reverse method. It would have a general use case besides enums.

I quickly created a GDScript equivalent:

func find_key(dictionary, value):
	var index = dictionary.values().find(value)
	return dictionary.keys()[index]

@Reneator
Copy link

Reneator commented Dec 5, 2019

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)

@KoBeWi
Copy link
Member

KoBeWi commented May 25, 2020

Is this about retrieving value keys for Enums, like this?
var key = enum.keys()[enum.keys().find(value)]

@PLyczkowski
Copy link

Also check out godotengine/godot-proposals#255 since it addresses a similar problem.

@mhilbrunner
Copy link
Member

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants