Dynamically create class object?
I am looking for the way to create new class object at the run time. What I had in mind was if I have a class 'enemy' that can define enemy model, animation, position, rotation, scale, etc. How can I create another object that will define new enemy? I want to do this without restricting my program with predefinded class id and such.
By having a class with model, animation, position, rotation, scale, etc. Read in input, create object, tahdah.
Use a vector:
When you are ready to create a new one, call:
When drawing or updating enemies, you iterate over the entire vector.
std::vector<MyEnemy> enemies;
When you are ready to create a new one, call:
enemies.push_back(MyEnemy(model, animation, position, rotation, scale, etc...);
When drawing or updating enemies, you iterate over the entire vector.
The factory pattern might be of interest to you: http://en.wikipedia.org/wiki/Factory_method_pattern
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement