Save system just using a global script

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

Hi, i’m fairly new to coding etc. I have made a few projects but never mastered the save system - i know there is lots of documentation and things about but i cant seem to find what I’m looking for (or if its even a thing)

I’m making a simple RPG/ dungeon crawler game. I have a global script with every player stat in it. From HP, XP, MP to how many potions and items the player has to skills learnt.

is there a way to literally save that entire script to disk, and load it back up when the game is launched so the player can just carry on where they left off?

Sorry if this is long winded, or i’ve missed something glaringly obvious, but any help would be appreciated

:bust_in_silhouette: Reply From: Sween123

If it’s just a simple game, you can do a save system within one script, but not recommended. As the number objects you want to save increases and when you need to save infos that are not just simple attributes, a save system within one script can be quit inconcenient.
For saving game infos, we use json.
So, you can save your game infos into a dictionary, and then save the dictionary into a file. For example, you have a dictionary of attributes for a character, you simply save this dictionary into your file using json.
(Simply put, the idea is to put text inside a file, and when load, we change things according to what is inside the file)
(Godot provides functions like to_json() and parse_json())

I don’t think there’s a way to save the entire script. Scripts are what contains the code to function the game, not a save file that contains game infos.

As the game gets more complex, you may want to know more about the save system and how to save more complicated infos. Yesterday I had answered a question on save system, it might help. See This.

:bust_in_silhouette: Reply From: njamster

All your scripts are already saved to disk, otherwise you couldn’t run them. :wink: What you want to do is saving variable values during runtime. That is called “Serializing”.

I’d recommend you take a look at this tutorial.