Advertisement

Moving from scene to scene without loosing data (like inventory stuff)?

Started by November 09, 2018 02:38 PM
9 comments, last by Rutin 6 years ago

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.


void life()
{
  while (!succeed())
    try_again();

  die_happily();
}

 

Not a Game Design question. Moving to a more appropriate forum.

-- Tom Sloper -- sloperama.com

Advertisement

Are you not able to just make an inventory object and pass it by reference scene to scene in your independent logic section for each one?

This is your engine and you'll know how it works more than anyone else here. ;) You either pass an inventory object, or create manager class that handles all the stuff you need for the entire game regardless of scene and reference from that for everything.

Programmer and 3D Artist

33 minutes ago, Rutin said:

Are you not able to just make an inventory object and pass it by reference scene to scene in your independent logic section for each one?

LOL yes this is perfect, but I would like to make things more generic just for learning purposes. I mean, how does Unity3D succeeds that? I have years to use unity, but I remember that i had that problem and unity would had a way of dealing with this.

33 minutes ago, Rutin said:

or create manager class that handles all the stuff you need for the entire game regardless of scene and reference from that for everything.

Hmm that seems really interesting. If i understood correctly, this is going to be like a general purpose script runner that the Client can use to add scripts in there that are going to be independent from scene or sprite objects? In other words, these scripts are going to be alive until the entire application shuts down? Actually this is the solution of my problem lol! I just learned something new after a long time! 

Thank you!


void life()
{
  while (!succeed())
    try_again();

  die_happily();
}

 

1 hour ago, babaliaris said:

 I have years to use unity, but I remember that i had that problem and unity would had a way of dealing with this.

In Unity it's done with a game manager object that is explicitly set to NOT be destroyed between scene loads, pretty much what @Rutin is describing, just the Unity way of doing it.. ;)

5 hours ago, babaliaris said:

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.

Why not construct the Scene object with a reference to the "Game" object, that I suppose contains the LoadScene() method? And in that Game-class you can have a state manager, inventory manager, etc?

Advertisement
15 hours ago, TobiasK said:

Why not construct the Scene object with a reference to the "Game" object, that I suppose contains the LoadScene() method? And in that Game-class you can have a state manager, inventory manager, etc?

The scene contains a reference to the game object (I call it Core).


void life()
{
  while (!succeed())
    try_again();

  die_happily();
}

 

11 minutes ago, babaliaris said:

The scene contains a reference to the game object (I call it Core).

Well, problem solved then :) It makes a lot more sense design-wise to have global state there aswell, so just create state and inventory management classes and stick them there.

24 minutes ago, TobiasK said:

Well, problem solved then :) It makes a lot more sense design-wise to have global state there aswell, so just create state and inventory management classes and stick them there.

Yes the problem solved!!!


void life()
{
  while (!succeed())
    try_again();

  die_happily();
}

 

I'm glad. :) Best of luck on the rest of your project.

Programmer and 3D Artist

This topic is closed to new replies.

Advertisement