I'm working on an entity component system but I'm having trouble with the editor. Components take the form of structs with this formula Component healthComponent; This works fine when you know what component you want but for the game editor I'm working in a dynamic way and need to be able to simple get any component of an entity by string for example auto component = GetComponent("Health", entityId); I know c++ is strongly typed so is there any work around for this?

  • 1
    \$\begingroup\$ There are ways to work around the type system (void*, abstract base classes), but they tend to introduce more pain than they actually solve. It is hard to answer your question without knowing more about your code and the objects' usage context. A way could be, to find out the type of the object first and use a special branch for each type afterward. But that depends on what you want to do with the data. \$\endgroup\$
    – wychmaster
    Commented May 3, 2020 at 10:38
  • 1
    \$\begingroup\$ How are your components stored? How are they used in systems? What exactly do you want to accomplish? Simulating reflection (for editors or serialization) in C++ is a tricky subject, and might not be worth the pain depending on your goals. \$\endgroup\$
    – user35344
    Commented May 3, 2020 at 10:54
  • \$\begingroup\$ Components are stored in ComponentManagers of the same type and I have a vector of those with a basetype.I can use the void* approach but it still requires me to cast it to the correct type to access the fields of the component. I already have serialization done using templates but now I need to apply the same logic to the editor. \$\endgroup\$ Commented May 3, 2020 at 23:42


You must log in to answer this question.

Browse other questions tagged .