What are flags? How do I manipulate them?

Godot Version

4.3.stable

Question

What are flags, specifically named in https://docs.godotengine.org/en/stable/classes/class_%40globalscope.html#enum-globalscope-propertyusageflags. I found this when looking up Object.get_property_list() has to return a list of dictionaries where the value for the “hint” key refers to this. I have found that flags is not a keyword in gdscript, so I am lost as to what this “PropertyUsageFlags” is or how to manipulate it.

like you said _get_property_list has a good outline on their usage. Flags are an idea based on binary, where each “flag” is represented by a single bit. We prefer to use bit-wise operations to manipulate flags. To combine flags use | like so

PROPERTY_USAGE_SCRIPT_VARIABLE | PROPERTY_USAGE_STORE_IF_NULL

Might help if you gave a more concrete example of what you are trying to do with properties, are you overriding a _get_property_list?

FIrst i am just trying to display a property, have it be editable as a slider, make it resetable, and make the editor value bound to a float variable. Basically @export_range but manually.

Then i will want to manipulate the exported parameters dinamically, based on their values, but that is for the future. Now I just want to undertsand how the editor works.

I agree that the documentation is great, i got pretty far with it. But the sudden introduction of Flags in the documentation where the type is supposed to be got me stuck. To my understanding PROPERTY_HINT_RANGE is just an integer value, right? I got the first part working with returning the following list

[
    {
        "name" : "my_var",
        "class_name" : &"",
        "type" : Variant.Type.TYPE_FLOAT,
        "hint" : PROPERTY_HINT_RANGE,
    },
]

Since this works, I assumed that hint is just a bitmask, and godot uses some special class/type to represent them, and PROPERTY_HINT_TYPE refers to an integer, useful in this context. However I found no documentation on Flags.

Ah, yeah “flags” are just integers, a different way to use the 32 bits within an integer.


For example the property flag PROPERTY_USAGE_DEFAULT contains two bits, that match two other properties

6 in binary is 110, bits for the 2’s and 4’s place are “set”.

We can see these properties with matching 2 and 4 values

  • 2 PROPERTY_USAGE_NO_EDITOR
  • 4 PROPERTY_USAGE_STORAGE

Meaning PROPERTY_USAGE_DEFAULT is equal to both of those flags combined.

1 Like

Thanks, I think I understand it. So then the documentation mentions at here that “usage is a combination of PropertyUsageFlags” then “combination” means I can just add different constants together to get the desired result? Or use bitwise operations to avoid the carry bit.

1 Like

Yes that bitwise operations like | is how to combine the flags

PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_STORAGE == PROPERTY_USAGE_DEFAULT
2 Likes