GodotECS / godex

Godex is a Godot Engine ECS library.
MIT License
1.22k stars 67 forks source link

Add dynamic set and get. #261

Closed AndreaCatania closed 3 years ago

AndreaCatania commented 3 years ago

This allow to have components (and shared components) with variable properties, which is useful to expose arrays, or structure with dynamic properites.

To use this featues it's necessary to implement the functions:

Here an example:

/// This Component accepts a color property on if the paramter `enable_color` is true.
struct TestDynamicSetGetComponent {
    COMPONENT(TestDynamicSetGetComponent, DenseVectorStorage);

    static void _bind_methods() {
        ECS_BIND_PROPERTY(TestDynamicSetGetComponent, PropertyInfo(Variant::BOOL, "enable_color"), enable_color);
    }

    void _get_property_list(List<PropertyInfo> *r_list) {
        if (enable_color) {
            r_list->push_back(PropertyInfo(Variant::COLOR, "color"));
        }
    }

    bool _set(const StringName &p_name, const Variant &p_value) {
        if (enable_color) {
            if (p_name == SNAME("color")) {
                meta[SNAME("color")] = p_value;
                return true;
            }
        }
        return false;
    }

    bool _get(const StringName &p_name, Variant &r_value) const {
        if (enable_color) {
            if (p_name == SNAME("color")) {
                if (meta.has(SNAME("color"))) {
                    r_value = meta[SNAME("color")];
                } else {
                    // Default color.
                    r_value = Color(1.0, 0.0, 0.0);
                }
                return true;
            }
        }
        return false;
    }

    bool enable_color = false;
    Dictionary meta;
};

https://user-images.githubusercontent.com/8342599/131245318-a4f055d2-93a8-4105-9d20-728c81996daf.mp4


Fixes: https://github.com/GodotECS/godex/issues/139

AndreaCatania commented 3 years ago

Implements: https://github.com/GodotECS/godex/discussions/236

AndreaCatania commented 3 years ago

Fixes: https://github.com/GodotECS/godex/issues/139