How to save data from a node not in tree ?

Godot Version 4.2

Hello all !

I’m making an RPG in which my player character node has all its stats contained in a component.

This character is not always in the tree, for example if I’m on a map selection or if the character has been swapped for another.

I can’t find the best way to save its stats for when I load the game or when it gets back onto the tree.

I explored a singleton for storing its stats but I find bad that there’s two way to store stats, one in a ressource that is loaded in a StatManager for npcs and now a singleton for my characters.

I wanted all characters (ai and mine) to use the StatManager component that I made, but I didn’t know I had to save it to disk everytime and I don’t want to cause wear by saving to disk each time a stat change.

It’s some kind of pokemon clone in which you can have only one monster at a time and you need to do battle to gain stats which will determine your next evolution.

  • I have a monster class that I want to use for the AI which contains variable for the sprite, initialization of stats and flag for determining if it’s an ally, player or a foe.
  • I have a player pet script that extends the monster class in which I have all player only related function like gaining a level, gain xp etc.
  • I have a stats component node that contains a StatsManager ressource. That ressource contains the stats of the monster that possess it.
  • The monster node // my player character node is a Character Body 2D containing an animated sprite and a stats manager components (with its stats as a ressource).

When on the overworld my monster is not showing and is not in the tree.

Once a battle is engaged then my monster is added to a new scene with the ennemy and its stats should be loaded from the ressource.

However once the battle is done, we go back to the overworld and my monster needs to save its stats for the next battle or for when I load the game back.

This is the part in which I’m in trouble because I don’t know the best way to do it and using singleton for my pet and ressource for the ennemy seems messy.

If you have any idea on how to tackle this, that’d be greatly appreciated ! →

try AutoLoad/SIngletons/Global Variable to save your character stats when moving in-between scenes
when the scene is ready , you load it from that

1 Like

Isn’t it a bad practice to use Singleton to store temporary data ?

if you change_to_scene, whole scene tree got deleted except the autoloads, the only way for you to keep it is by storing it to autoload/global variable, or really you want to save your temporary data into a save data?
if you dont like autoloads, try actually move scene/level while keeping the character alive/not queue_freed(), that’s without using the change_to_scene() method

Singleton seems the easiest choice in this case, even thought it doesn’t feel right to use, thanks a lot for your ideas

Why would it be wrong? Just curious.

I was told to limit Singleton as it can lead to spaghetti code, I thought there were always better solution, seems it’s not always the case.
I’m still learning !
Thanks a lot all