I am trying to create a basic game engine in C++ and I want an Engine
object to be able to loop through all the GameObject
children to run their update methods, to do this I want to use a vector of all children within the Engine
class.
For example:
This is similar to what I have been trying to do:
Parent Engine Class
class Engine {
public:
Engine() {};
std::vector<GameObject> GameObjects; //Vector to store all GameObjects
void AddNewObject(GameObject& obj) {
GameObjects.push_back(obj); //Add new object to the vector array
}
void Run() {
for (int i = 0; i < GameObjects.size(); i++) {
GameObjects[i].Update(); //Run the update method of each GameObject
}
}
}
GameObject Child Class
class GameObject : public Engine {
public:
GameObject() {}
void Update() {
//Do stuff
}
}
Main code loop
int main(void) {
Engine engine;
GameObject object;
engine.AddNewObject(object); //Add object
engine.run();
}
Any help would be greatly appreciated, Thanks.