I dont know how to save and load my game

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

I recently started working on a video game, it is platform so it takes many levels, I use global variables to determine if a level is unlocked or not, I need a way to save the information about which levels are unlocked so that when one re-enters to the game the levels that I already have unlocked are still unlocked, I have tried tutorials and documentation, but I still cannot find a solution, I am using this code but whenever I use it it says “No saved data” so I have tried to follow all the steps it takes with print to see if something is wrong somewhere, but from what I have seen it just can’t find the file and I don’t know what to do anyway I don’t know much about programming, only when reading the information it tells me “no saved data”

recientemente comencé a trabajar en un videojuego, es de plataformas así que lleva muchos niveles, uso variables globales para determinar si un nivel esta desbloqueado o no, necesito una manera de guardar la información sobre cuales niveles están desbloqueados para que así cuando uno vuelva a entrar al juego los niveles que ya tenga desbloqueados sigan desbloqueados, he tratado con tutoriales y con la documentación, pero sigo sin lograr una solución, estoy usando este código pero siempre que lo uso me dice “No saved data” así que he tratado de seguir todos los pasos que da con print para ver si en alguna parte falla algo, pero por lo que he visto simplemente no encuentra el archivo y ya no se que hacer igual yo no se mucho de programación , solo que al momento de leer la información me dice “no saved data”

extends Node

const FILE_NAME = “C:/user://game-data.json”

var n1 = NivelCode.nivel1
var n2 = NivelCode.nivel2
var n3 = NivelCode.nivel3
var n4 = NivelCode.nivel4
var n5 = NivelCode.nivel5
var n6 = NivelCode.nivel6
var n7
var n8

func _physics_process(delta):
if Input.is_action_just_pressed(“Guardar”):

if Input.is_action_just_pressed("debug"):
	print("player guardado = ", player)
	print("Nivel 1 gloval var = ", NivelCode.nivel1)
	print("n1 = ", n1)

var player = {
“n1” : 0,
“n2” : 0,
“n3” : 0,
“n4” : 0,
“n5” : 0,
“n6” : 0


func save():

var file = File.new()
file.open(FILE_NAME, File.WRITE)
print("file open")

func load_game():
var file = File.new()
if file.file_exists(FILE_NAME):
file.open(FILE_NAME, File.READ)
var data = parse_json(file.get_as_text())
if typeof(data) == TYPE_DICTIONARY:
print(“data=”, data)
player = data
printerr(“Corrupted data!”)
printerr(“No saved data!”)
print(“antes del guardado”, player)
NivelCode.nivel1 = n1
NivelCode.nivel2 = n2
NivelCode.nivel3 = n3
NivelCode.nivel4 = n4
NivelCode.nivel5 = n5
NivelCode.nivel6 = n6
player = {
n1 = NivelCode.nivel1,
n2 = NivelCode.nivel2,
n3 = NivelCode.nivel3,
n4 = NivelCode.nivel4,
n5 = NivelCode.nivel5,
n6 = NivelCode.nivel6

print("despues de Func cargar")
print("player guardado = ",  player)
print("Nivel 1 gloval var = ",  NivelCode.nivel1)
print("n1 = ", n1)

that’s the code i’m using, i don’t know if someone can guide me or something, i really need help
ese es el codigo que estoy usando, no se si alguien me puede orientar o algo, enserio necesito ayuda

:bust_in_silhouette: Reply From: hamza_memon

How I did it was create a dictionary and put it in autoload. Then whenever, I make changes to it, I also use .save() method at end.

This is one part of my dictionary.

var all_levels_info = {
	'sand': {
			'unlocked' : true, 
			'high_score' : 0, 
			'stars' : 0,
			'goals' : {
				'camel' : 100,
			'level_to_load' : 'res://scenes/levels/sand/1.tscn'

This script is named GamaDataManager and is present in autoload, this is the save and load data functions

    func _ready():
	level_info = load_data();
#	level_info = all_levels_info;

func save_data():
	var file = File.new();
	var err = file.open(path, File.WRITE);
	if err != OK:
		print('error in saving');

func load_data():
	var file = File.new();
	var err = file.open(path, File.READ);
	if err != OK:
		return default_level_info
		print('error in loading')
	var read = {};
	read = file.get_var()
	return read;

Now, when I do any changes to this, for e.g I call it and set the unlocked value of the next level to true upon stage clear, I do this

	GameDataManager.level_info[lvl_type][lvl_num+1]['unlocked'] = true;