rttrorg / rttr

C++ Reflection Library
https://www.rttr.org
MIT License
3.2k stars 444 forks source link

Casting an object instance to be the type stored in rttr::type #368

Open killereks opened 11 months ago

killereks commented 11 months ago

I have this piece of code, I need to automatically be able to modify values of their components. My components all derive from Component class, e.g. MeshComponent : Component, TransformComponent : Component.

I found that when casting component to (MeshComponent*) and then doing set_value works, but how would I automatically convert this to the correct type? I have tried using var.convert(type); but that just returns 0, is it because type is not a pointer?

for (Component* component : selectedEntity->GetAllComponents()) {
        const rttr::type type = rttr::type::get_by_name(component->GetName());

        rttr::variant var = component;

        for (rttr::property& prop : type.get_properties()) {
            if (prop.get_type() == rttr::type::get<int>()) {
                int value = prop.get_value(var).to_int();
                ImGui::InputInt(prop.get_name().c_str(), &value);
                bool success = prop.set_value(var, value);
            }
        }
}