willnationsdev / log

A log of my ongoing and planned development efforts.
MIT License
0 stars 0 forks source link

Godot 3.2: Implement enum exports for GDScript #25

Open willnationsdev opened 6 years ago

willnationsdev commented 6 years ago

At this time, exporting enums in GDScript is very cumbersome. In order for the enums' names to be visible in the editor, you must specify a set of strings to be displayed for each value. This requires a hardcoded list of strings that must be updated as the enum changes over time. Either that, or a Dictionary mapping must be made and then used in a _get_property_list() override in a tool script (which, in turn, necessitates an implementation of _set() and _get(). This can amount to as much as 20 extra lines of code + converting it to a tool script JUST to make an enum value automatically updated in the Inspector.

This change would allow you to define enum exports based on an enum's identifier and using an optional string prefix to filter out string content from the keys, similar to the way the engine treats property groups.

enum Elements {
    ELEM_FIRE,
    ELEM_ICE
}
export(int, Elements, "ELEM_") var element = ELEM_FIRE

This would then display "Fire" and "Ice" as options within the editor as it would filter out the content. If you wished to change the order, you could just change the ordering within the enum.

willnationsdev commented 6 years ago

Turns out this was largely already done in 3.1! All you had to do was export the enum directly. I have submitted a PR to add the optional prefix filter portion.