Hello. First of all, I just want to say, thank you for taking the time to read this. I've been having a hard time figuring this particular thing out, and I have done some extensive searching with Google. Obviously the only reason I am posting about this, is because I simply can't find an answer. I'll try to make it brief.
I am trying to make a game in C++. This is what I'm trying to do.
I have a game loop.
Inside this game loop, I am calling the objects' member functions to update them.
1 2 3
|
player->update();
enemy[0].update();
enemy[1].update();
|
Now obviously I am using a loop to check through all of my enemy objects. I'm just trying to illustrate my point.
I am using a 2D array for my level. And certain numbers represent tiles, and certain numbers represent the enemies, and player, etc.
For instance, the number 2 is the player, and the multiple number 3s represent the enemies.
I have a loop that reads through this array, and dynamically creates these objects, so I am using them only when I need them, and deleting them when the player beats the level, and creating new ones when the next level is called, etc. Another benefit of having my objects represented in the array, is that I can use the array to initialize their starting x and y positions.
My code counts how many enemies are in the array, and uses that count to dynamically create storage for that amount of enemies.
Here is the problem:
1 2 3
|
player->update();
enemy[0].update();
enemy[1].update();
|
The compiler will give you an error if you try to run these object member functions UNLESS the objects already exist.
I even tried something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
bool playerExists;
bool enemyExists;
if (playerExists)
{
player->update();
}
if (enemyExists)
{
for (int i = 0; i < enemyCount; i++)
enemy[i].update();
}
|
I want to use dynamic objects to manage memory, but you can't run these member functions in the game loop unless they already exist.
So how do you set aside storage for an object, if you don't know in advance how many objects you're going to need (it's all based on how many objects are in the arrays for each level), and call their member functions?
I heard you can set objects with a NULL_PTR, but then, it still doesn't solve the problem.
Even if you create a single object at compile time, and set it to the null pointer, how do you then later modify it to dynamically set storage for more objects of that type?
Based on the searching I have done, there doesn't seem to be an answer.
I tried using vectors as well, and had some problems with them too. Since a vector is a dynamic array and can be resized.
I don't remember the specifics, but with one of my projects (and I dissected my own code to look for the error), I couldn't for the life of me figure out why using the vectors to create my objects were causing my program to not compile. I couldn't trace the error.
Just out of curiosity, if you have a vector of 3 enemy objects, and then you shrink that vector down to 2 enemy objects, what happens to the data in the third object? Does it still sit there in memory? What if you shrink it down to 2 objects, and then expand it back to 3 objects? Can it then use that data that was previously there?
I want to create these objects during runtime, when I need them, and still execute their update member functions every frame ONCE they are created.
The concept of dynamically creating objects is neat, but if they need to be defined first in order to use their functions later, doesn't that defeat the whole purpose of dynamically creating storage?
Thank you for taking the time to help me with this issue.