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

368 Views Asked by At

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 ?

2

There are 2 best solutions below

0
Theraot On BEST ANSWER

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:

0
lilou On

Since there doesn't seem to be a better option for now (thanks Theraot for the answer), I went with hard coded enums. I'll add my version here (made for Godot 4.1.1) :

enum PropertyFlagsUsage {
    STORAGE = 2,
    EDITOR = 4,
    INTERNAL = 8,
    CHECKABLE = 16,
    CHECKED = 32,
    GROUP = 64,
    CATEGORY = 128,
    SUBGROUP = 256,
    CLASS_IS_BITFIELD = 512,
    NO_INSTANCE_STATE = 1024,
    RESTART_IF_CHANGED = 2048,
    SCRIPT_VARIABLE = 4096,
    STORE_IF_NULL = 8192,
    UPDATE_ALL_IF_MODIFIED = 16384,
    SCRIPT_DEFAULT_VALUE = 32768,
    CLASS_IS_ENUM = 65536,
    NIL_IS_VARIANT = 131072,
    ARRAY = 262144,
    ALWAYS_DUPLICATE = 524288,
    NEVER_DUPLICATE = 1048576,
    HIGH_END_GFX = 2097152,
    NODE_PATH_FROM_SCENE_ROOT = 4194304,
    RESOURCE_NOT_PERSISTENT = 8388608,
    KEYING_INCREMENTS = 16777216,
    DEFERRED_SET_RESOURCE = 33554432,
    EDITOR_INSTANTIATE_OBJECT = 67108864,
    EDITOR_BASIC_SETTING = 134217728,
    READ_ONLY = 268435456,
    SECRET = 536870912,
}

enum PropertyFlagsUsageCombo {
    NONE = 0,
    DEFAULT = 6,
    NO_EDITOR = 2,
}

enum PropertyHintValues {
    NONE = 0,
    RANGE = 1,
    ENUM = 2,
    ENUM_SUGGESTION = 3,
    EXP_EASING = 4,
    LINK = 5,
    FLAGS = 6,
    LAYERS_2D_RENDER = 7,
    LAYERS_2D_PHYSICS = 8,
    LAYERS_2D_NAVIGATION = 9,
    LAYERS_3D_RENDER = 10,
    LAYERS_3D_PHYSICS = 11,
    LAYERS_3D_NAVIGATION = 12,
    FILE = 13,
    DIR = 14,
    GLOBAL_FILE = 15,
    GLOBAL_DIR = 16,
    RESOURCE_TYPE = 17,
    MULTILINE_TEXT = 18,
    EXPRESSION = 19,
    PLACEHOLDER_TEXT = 20,
    COLOR_NO_ALPHA = 21,
    OBJECT_ID = 22,
    TYPE_STRING = 23,
    NODE_PATH_TO_EDITED_NODE = 24,
    OBJECT_TOO_BIG = 25,
    NODE_PATH_VALID_TYPES = 26,
    SAVE_FILE = 27,
    GLOBAL_SAVE_FILE = 28,
    INT_IS_OBJECTID = 29,
    INT_IS_POINTER = 30,
    ARRAY_TYPE = 31,
    LOCALE_ID = 32,
    LOCALIZABLE_STRING = 33,
    NODE_TYPE = 34,
    HIDE_QUATERNION_EDIT = 35,
    PASSWORD = 36,
    LAYERS_AVOIDANCE = 37,
    MAX = 38
}

enum PropertyTypes {
    NIL = 0,
    BOOL = 1,
    INT = 2,
    FLOAT = 3,
    STRING = 4,
    VECTOR2 = 5,
    VECTOR2I = 6,
    RECT2 = 7,
    RECT2I = 8,
    VECTOR3 = 9,
    VECTOR3I = 10,
    TRANSFORM2D = 11,
    VECTOR4 = 12,
    VECTOR4I = 13,
    PLANE = 14,
    QUATERNION = 15,
    AABB = 16,
    BASIS = 17,
    TRANSFORM3D = 18,
    PROJECTION = 19,
    COLOR = 20,
    STRING_NAME = 21,
    NODE_PATH = 22,
    RID = 23,
    OBJECT = 24,
    CALLABLE = 25,
    SIGNAL = 26,
    DICTIONARY = 27,
    ARRAY = 28,
    PACKED_BYTE_ARRAY = 29,
    PACKED_INT32_ARRAY = 30,
    PACKED_INT64_ARRAY = 31,
    PACKED_FLOAT32_ARRAY = 32,
    PACKED_FLOAT64_ARRAY = 33,
    PACKED_STRING_ARRAY = 34,
    PACKED_VECTOR2_ARRAY = 35,
    PACKED_VECTOR3_ARRAY = 36,
    PACKED_COLOR_ARRAY = 37,
    MAX = 38
}

static func get_type(type : int) -> String:
    if type >= 0 && type < PropertyTypes.MAX:
        return PropertyTypes.find_key(type)
    return "INVALID_TYPE"

static func get_hint(hint : int) -> String :
    if hint >= 0 && hint < PropertyHintValues.MAX:
        return PropertyHintValues.find_key(hint)
    return "INVALID_HINT"


static func get_usage_flags(usage : int) -> String:
    var flags = []
    
    for key in PropertyFlagsUsageCombo.values():
        if usage == key:
            return PropertyFlagsUsageCombo.find_key(key)
    
    for key in PropertyFlagsUsage.values():
        if usage & key:
            flags.append(PropertyFlagsUsage.find_key(key))
    return " | ".join(flags)