Hello!
I'm creating a 2D game engine for a game I wan to make for the Game Off jam.
I'm trying to figure out the best way to keep data when loading a new scene. In order to make you understand exactly what I mean, I must explain to you briefly how my engine works.
A game is being constructed by Scenes. Each scene contains an std::vector(Sprite *), containing all the sprite objects of the scene. Each Sprite object has an std::vector(Brain *) , where each brain object is just a script, or you can name them components. When a new scene is getting loaded, the old scene get's destroyed which means that all the sprite objects are getting destroy including their brain objects.
This made me find a problem. If I have a brain script for the inventory system of the player, when loading a new scene, the brain object inside the player which describe's the inventory system will get destroyed, so I will loose all the data about items etc. One way of solving this is to mark sprite objects as scene_independed, which means that after these objects have been created, every time you call the LoadScene() , first add all the scene_independed sprites from the old scene to the new scene and then destroy the old scene. Something like this:
void LoadScene(Scene *new_scene)
{
if (this->scene != NULL)
{
//Move all scene_independed sprites to the new scene.
for (Sprite *sp : this->scene->getSprites())
{
if (sp->scene_independed)
{
//Move sp to the new scene.
new_scene->addSprite(sp)
//Pop sp from the old scene
//to prevent it from getting destroyed
//after calling delete this->scene.
this->scene->popSprite(sp)
}
}
//Delete the old scene.
delete this->scene;
}
this->scene = new_scene;
}
The reason I created this thread was to take Advises. This is the first time I'm doing something like that and i want to know if there are better solutions.