I am working on a small component of the game engine in C ++. Various components are added to the gameobject internal list to save some time. I do not make a member variable of these components, only the game object is a member variable, for example:
void Initialize()
{
MeshComponent* meshRenderer = new MeshComponent(mesh, material);
m_GameObject.AddComponent(meshRenderer);
}
The meshRenderer variable is added to the component list in AddComponent (), but is out of scope at the end of this function.
Later (in the update / drawing cycles of the game) this component is called and everything works, despite the fact that the object went out of scope during initialization.
I understand something is wrong in scope, is it safe to use or is there another approach I should take (without having to introduce member variables of each of my components)?
Thanks for the help!
source
share