save file system

godot 4.3

this is what i have tried ?

what i should do
class_name FileManager
extends Node

var SAVE_PATH = “user://savegame.data”
@onready var game_manager: Node = %GameManager # Reference to the Game Manager

func save() → Dictionary:
var save_dict = {
“total_coin”: Event.total_coin,
“level_data”: Event.level_data
}
return save_dict

func save_game() → void:
var save_file = FileAccess.open(SAVE_PATH, FileAccess.WRITE)
save_file.store_var(call(“save”))

func load_game() → void:
if not FileAccess.file_exists(SAVE_PATH):
save_game()
return

var save_file = FileAccess.open(SAVE_PATH, FileAccess.READ)
var saved_data = save_file.get_var()

Event.level_data = {} if saved_data["level_data"].is_empty() else saved_data["level_data"]

okay… and what is the problem you are encountering ?

can you please use preformatted text so that people can read your code without getting a headache ?

1 Like

Before anything else, format your code and close the files after use.

done what else

Current Code:

class_name FileManager
extends Node

var SAVE_PATH = "user://savegame.data"

@onready var game_manager: Node = %GameManager # Reference to the Game Manager

func _get_state() → Dictionary:
	var save_dict = {
	"total_coin": Event.total_coin,
	"level_data": Event.level_data,
	}
	return save_dict


func save_game() → void:
	var save_file = FileAccess.open(SAVE_PATH, FileAccess.WRITE)
	save_file.store_var(_get_state())
	save_file.close()


func load_game() → void:
	if not FileAccess.file_exists(SAVE_PATH):
		save_game()
		return
	var save_file = FileAccess.open(SAVE_PATH, FileAccess.READ)
	var saved_data = save_file.get_var()
	save_file.close()
	if saved_data.level_data.is_empty():
		Event.level_data = {}
	else:
		Event.level_data = saved_data.level_data

This code should work currectly, if it isn’t, please wrtie more detailes for us.

Also, I recommend using GProgress to manage game state.

steps to do it please and steps to make it at every level