Wednesday 16 August 2017

Game Engine 16/08/17 - Component registration

While designing the new SceneModule (handles the creation and destruction of GameObjects & Components) I wanted to create a Component Factory that would allow me to create Components using their name string and register them.

So I created a static std::map containing the functions necessary to Component creation. Then I would register them using a static function named RegisterComponent. Sadly, I had to register the components at run-time and it wasn't really useful while creating new component types. So I created a static bool in my component types that when initialized would call the RegisterComponent function. This wasn't very clean still so I created two macros:
  • ENGINE_REGISTER_COMPONENT_DECL: declares the static bool inside the class;
  • ENGINE_REGISTER_COMPONENT_DEFN (type, typeName): defines the static bool, registering the Component of 'type' with the 'typeName' chosen.
Now, when I want to create a new Component, I call the SceneModule CreateComponent function, specifying the parent GameObject and the type string. This function calls the corresponding create function stored on the static std::map. This way I can even create new components using only the console.

No comments:

Post a Comment