Could someone help me with saving and loading?

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

So I followed Game Endeavor’s tutorial on YouTube and managed to create a save file, but I have one problem: They didn’t cover how to link the directory to the actual game. Example script:

extends Sprite

const SAVE_DIR = "user://saves/"

var path = SAVE_DIR + "playerInfo.dat"

func _on_Save_pressed():
    var defaultData = {
    "player" : {
	    "name" : "Bob",
	    "race" : "Human",
	    "gender" : "Male",
	    "hairC" : "Brown",
	    "hairS" : "Combed"
	    },
    "options" : {
	    "master" : 50,
	    "music" : 50,
	    "sfx" : 50
	    }
    }

    var dir = Directory.new()
    if !dir.dir_exists(SAVE_DIR):
	    dir.make_dir_recursive(SAVE_DIR)

    var file = File.new()
    var error = file.open_encrypted_with_pass(path, File.WRITE, "$3GAM$E_/SAvE!1")
    if error == OK:
	    file.store_var(defaultData)
	    file.close()

What I want is for the data in defaultData to actually have an effect on the game. Help would be appreciated.

:bust_in_silhouette: Reply From: lewis glasgow
func load():
var file = File.new()
if file.file_exists(save_path):
	file.open(save_path,File.READ)
	defaultData = file.get_var()
	file.close()

#the order that you save is the order that you load

That isn’t what I asked. I asked how to implement the save/load function in the actual game. For instance, how does the save function detect the “health” of the player?

Amateur.game.dev. | 2021-07-26 20:23

To implement these values, you need a class or function. In this function you can do something like setting player in his saved position or checkpoint. To do the you first have to extract or parse the JSON file. You can use Godot built-in JSON Parser function. It will extract and save all of your values in a dictionary ex. player_data .
Then (assume you are in the level script) you can write something like that,
$Player.position = player_data["position"]
It’ll set player position.

The best thing to implement Save And Load, is to have an Autoload script. Then it’ll set up music, SFX, level and player’s data.

(My English is very bad. I’ll be very happy if it helps you )

adib003 | 2021-10-12 19:05

:bust_in_silhouette: Reply From: MisterMano

The directory the file is saved is that user://, which is different depending on platform: Windows, Linux, Web. I know you can use ./ instead, like ./savefile.dat, to save wherever the game actually is, or ./saves/save1.dat, but I know it won’t work for Web and I don’t know if it’ll work for Android.

To load, you’ll have to do something like this:

var error = file.open_encrypted_with_pass(path, File.READ, "$3GAM$E_/SAvE!1") #Open it in READ mode
defaultData = file.get_var()
file.close()

Do note that anything that isn’t in the defaultData struct won’t be saved, so you’ll want to throw anything else you want to save inside it. Health, items, score, unlocked flags, current map, etc.