How do i save in godot

Godot Version

4.0

Question

i recently wanted to make my game save so i watched a few tutorials and wrote the code from this video.

the code i wrote is


func _ready():
	save_game()
func save():
	var save_dict = {
		"score" : 1000
	}
	return save_dict
	
func save_game():
	var save_game = FileAccess.open("user://savegame.save", FileAccess.WRITE)
	
	var json_string = JSON.stringify(save())
	
	save_game.store_line(json_string)

func load_data():
	if not FileAccess.file_exists("user://savegame.save"):
		return
	
	var save_game = FileAccess.open("user://savegame.save", FileAccess.READ)
	
	while save_game.get_position() < save_game.get_length():
		var json_string = save_game.get_line()
		var json = JSON.new()
		var parse_result = json.parse(json_string)
		var node_data = json.get_data()

i get some of it but im also pretty confused. my goal from this is to make a few saved variables and then those would load whenever you load the game.


var achievements = [false,false,false,false,false,false,false,false,false,false]
var enemy_multiplier = 1
var level = 1
var max_level = 1
var weapons: Array[int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
var dinos_spawned = 0

this is an examples of some of the variables i want to save.

so did this tutorial do it right? and if so, how am i supposed to access the data. so i put all the variables in a dictionary but how do i access that and make it so that whenever i load the game all the stats will change, maybe im not thinking right and idk what im saying but this is just kinda confusing, any help is appreciated even if not a clear answer

i’m using this, no need headache of making the savedata load and save again.

3 Likes

Your node_data variable should have all the data from the save() dictionary. Use like so

player_score = node_data["score"]

Last things you need to save a real variable in the save() function, assuming “player_score” is your variable again:

func save():
	var save_dict = {
		"score" : player_score
	}
	return save_dict
1 Like

im trying this and its kind of confusing, would you be able to explain how i would go about saving a global variable and then loading it? the key_path stuf is a bit confusing too.

it basically just
to save:

SaveSystem.set_var("achievements",achievements)
SaveSystem.save()

to load:

var achievements=SaveSystem.get_var("achievements",[])
1 Like

It made saving very simple. Thank you. Also, I called the load function on the global script using func_ready(). and then it won’t load anything, but then when I call the same function on a button, it loads everything. Do you know how I could make it load upon starting up the game?
Global script:

func _ready():
	Engine.time_scale = 3
	load_all()

button script:

func _on_load_button_pressed():
	Global.load_all()

Dont combine another Global Values Autoload with this SaveSystem AutoLoad, because this SaveSystem basically already a Global Values where you can access anywhere just like the global values you already have, it wasted loading to another variables of AutoLoad for?

1 Like