Search code examples
gdscriptgodot4enum-flags

GDScript : How to display a property's usage value as a list of flags


I'm working on a plugin to expand Godot's inspector, and whenever i print a list of properties, I get something like

{ "name": "col", "class_name": &"", "type": 4, 
"hint": 2, "hint_string": "Red,Green,Blue", 
"usage": 4102 }

I wanted something easier to read, so I tried making a function that would convert the usage value into the corresponding list of flags:

enum ExampleFlags {FLAG1=1, FLAG2=2, FLAG3=4096}

func get_usage_flags(prop : Dictionary):
    var usage : int = prop.usage
    var flags = []
    
    for flag in ExampleFlags.values():
        if usage & flag:
            flags.append(ExampleFlags.find_key(flag))
    return " | ".join(flags)

This works well enough, but when I substitute my ExampleFlags for the global PropertyUsageFlags, I get the following error :

res://Scene.gd:14 - Parse Error: The native enum "PropertyUsageFlags" does not behave like Dictionary and does not have methods of its own.

I could make a copy of that enum by hand, but that's a big list of values, and I'd need to manually update it each time godot changes those values in an update.

Is there another way ?


Solution

  • This is not a solution, but it is an answer: There is no API for this at the time of writing.

    To be more specific, there are APIs to handle build-in enum and constants in ClassDB, but they do not work for PropertyUsageFlags, because it is an un-scoped enum (they are exposed via @GlobalScope, but that is not a class).

    See also: