+1 vote

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

in Engine by (21 points)

2 Answers

+1 vote
Best answer

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.
Anyway:
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.

by (765 points)
selected by
0 votes

All your scripts are already saved to disk, otherwise you couldn't run them. ;) 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.

by (10,265 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.