How to do not delete previous scene as move next scene

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By HorseGorilla

I’m making a rogue-like game. like Binding of Isaac. As far as I know, Isaac creates a scene for each room, and when you go to the next room, you go to the next scene, and when you go to the previous room, you go to the previous scene.

Is there a way to leave the previous scene’s resources as is and go to the next scene, or have the next scene go to the previous scene in the same way?

https://drive.google.com/file/d/102V36ElafcryGIhAssd2RlzvdgVNd66t/view?usp=sharing

For example, the room looks like this. Should I put all the rooms in the Autoload variable?

Sorry to my horrible English.

:bust_in_silhouette: Reply From: AlexTheRegent

if you change your scenes with change_scene methods then you can’t save previous scene easily. But you can implement your own scene switching algorithm:

  1. Make Main node that will act as scenes container.
  2. Load your scene using load, instance and add_child methods to Main node.
  3. When next scene is required, you can remove your scene from Main node using remove_child method and then repeat step 2 to get new scene going. This way you still have reference to previous room and can do anything you want with it. But always remember to queue_free nodes when you don’t need them anymore, otherwise, if you lose reference to it, it will result into memory leak.

Note that this is just one of the many ways of implementing scene switching algorithms and optimal solution depends on your game.
Loading rooms into Singleton class (autoloading variable) can be very bad solution. I suggest you to avoid this approach.

Thank you! I was trying to do it with a singleton, but I canceled it and did it differently.

HorseGorilla | 2021-07-22 17:11

Hi, if you still remember how you managed to keep the previous scene’s data could you explain me a bit as I am looking for a way to achieve the same thing?