How optimize memory if game switch scene

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

I design layout for rpg game like this:


When player switch to level 2 by change_scene, I wonder godot released memory which used to load level 1 (tileset, player …)?

:bust_in_silhouette: Reply From: Miguelino

If you used the get_tree().change_scene("*insert the new scene nodes location on project's folder*") action, in theory the entire engine moved to that scene and the other one is gone and released.

You can confirm this usually by the fact that you have to reload the previous scene from scratch if you intend to load that scene again.

Memory is not saved from that previous scene when you change scene, unless you stored it in some sort of singleton.

if godot still has the player instance in memory, i have considered putting the whole levels in the main node, showing the tileset when necessary… of course it’s quite cumbersome, luckily i don’t need to, thanks you answered.

diesel | 2023-03-02 05:40

The player instance is not saved.

Let’s say you nested each level more or less like this:
main node for the level > assets like tilsets, etc + an instance of player node

whenever the scene is loaded, it loads that tree, and when it’s changed that tree is deleted in favor of the new scene you changed into, and this new scene will load everything from their own tree of nodes.

In shot, player is not kept on RAM in-between levels/scenes, so you don’t have to worry about it.

Miguelino | 2023-03-03 22:36