How to properly stach a scene for later use

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Vegevan
:warning: Old Version Published before Godot 3 was released.

Hi,

I’m working on a little RPG with Godot Engine, and I’m confronted to the following problem :

There is a safe place, a village where NPC move and everything is active. Somewhere, I want a wanted cell (tilemap based game) to instance a fight in a turn per turn style.

As the two scenes are working and well implemented; I want to switch to the fight scene while the village scene is kept in memory, un-updated for later use. After the fight, I want the game to switch back the village scene destroying the fight scene.

More schematic :
Village scene → walk on bush → fight → back to village scene

I actually did this keeping the scene in a var in a autoloaded script, but as I’m switching back to village, I’ve many errors; mainly with area2D strangly, like following screenshot :

Have you ever did something like this ? How can I avoid thoses 30 errors that re-appear at each fight ?

:bust_in_silhouette: Reply From: Hinsbart

One thing you could do is, instead of switching scenes, just put the battle scene on top of your village scene.
In practise, you would stop processing & input handling on your village scene, hide it and instance the battle scene.
Then after the battle is over and its scene is free’d, show and resume processing on the village.

Thanks for the reply !
Hum, it is an interesting solution. Though, two new problems appear :

  • If I extend this thinking or both scene, might I feel a performance problem in your opinion ? ( I think not but two scene can be a lot)

  • Have you a proper way to stop processing the village scene (which might be each and very map of the game) while fighting ? It would be nice that fighting scene could stop the village scene instead of the village scenes stopping itself ^^

Vegevan | 2016-09-03 10:57

:bust_in_silhouette: Reply From: Zylann

You can have both scenes in the tree, but have one paused and hidden, and the other visible and active. Then you switch between the two when you want it. The pause system in Godot could be handy here, however it’s only available as a global state, so you will have to stop processing stuff yourself (otherwise the village would still be updated while you fight).

Another way is to save the state of the village in a singleton or a file, change the active scene with get_tree().change_scene(new_scene), play the fight, and then when you want to go back to the village, you load it and you initialize it based on the saved state in the singleton.

Thanks for the reply, actually your solution is what I’m doing, the problem with the autoload stach is that re-entering the scene leads to many errors as the screenshot shows. I’m looking for a solution to avoid having 30 errors at each “back-to-village” process.
Anyway, I hadn’t heard about change_scene so I already thank you for this :slight_smile:

Vegevan | 2016-09-03 10:53

I actually proposed two solutions. Which one are you talking about?
The first one doesn’t uses autoload at all.

If you go for my second proposal, you shouldn’t store a node from the scene into a singleton because when you change it, nodes will be freed and the singleton will contain bad references. Instead you should store like a simple “savegame” (where the player was, for example, or more if you need) and restore this state after you loaded the scene back.

Zylann | 2016-09-03 13:33