Data loading problem

Godot Version

v4.3

Problem

I have an data saving system. I tried to make it in 2 ways the first way i copied it from my godot v4.2 project but it wasnt working so i made a new script that schould be working but only works the saving part of the script. Help will be appreciated.

Script

extends Node


var iron = 0
var amon = 0
var SAVE_PATH : String = "res://resource.bin"

func _ready():
	load_game()
func save():
	var dict = {
		"amon": amon,
		"iron": iron
		
	}
	return dict

func  save_game():
	var saveGame = FileAccess.open(SAVE_PATH, FileAccess.WRITE)
	var jsstr = JSON.stringify(save())
	
	saveGame.store_line(jsstr)
	
func load_game():
	if not FileAccess.file_exists(SAVE_PATH):
		return
	var saveGame = FileAccess.open(SAVE_PATH, FileAccess.READ)
	
	while saveGame.get_position() < saveGame.get_length():
		var jsstr = saveGame.get_line()
		var json = JSON.new()
		var parseResult = json.parse(jsstr)
		var nodeData = json.get_data()
		print(nodeData)

check this demo: godot-demo-projects/loading/serialization at master · godotengine/godot-demo-projects · GitHub

Yea but i want to make it with dictonary, do you have any idea why my script isnt working?

also with Dictionary: godot-demo-projects/loading/serialization/save_load_json.gd at master · godotengine/godot-demo-projects · GitHub


I combined your example with demo, without a “while” loop

example
extends Node

var iron = 5
var amon = 5
var SAVE_PATH : String = "res://resource.bin"

func _ready():
	save_game()
	load_game()

func save():
	var dict = {
		"amon": amon,
		"iron": iron
	}
	return dict

func save_game():
	var save_game = FileAccess.open(SAVE_PATH, FileAccess.WRITE)
	if save_game:
		var jsstr = JSON.stringify(save())
		save_game.store_line(jsstr)
		save_game.close()

func load_game():
	if not FileAccess.file_exists(SAVE_PATH):
		return
		
	var file := FileAccess.open(SAVE_PATH, FileAccess.READ)
	var json := JSON.new()
	json.parse(file.get_line())
	var save_dict := json.get_data() as Dictionary
	
	print("LOAD save_dict: ", save_dict)

output:
LOAD save_dict: { "amon": 5, "iron": 5 }

Your script seems to miss closing the file after saving. Make sure to call save_game.close() to ensure data is properly written. Also, when loading, double-check the file’s existence and the JSON parsing to avoid issues.

It doesnt work for me too idk why.Like its not saving the data that im adding in-game with it.

You should save to the user’s folder:
var SAVE_PATH : String = "user://resource.bin"

The problem didnt changed

Have you tried to stringify the “dict” directly, without a function?

Are you using SLib? If you are, it’s really easy:

extends Node


var iron = 0
var amon = 0
var SAVE_PATH : String = "res://resource.json"

func _ready():
	load_game()

func  save_game():
	SLib.save_json_file(SAVE_PATH, {"iron" : iron, "amon" : amon})
	
func load_game():
	var dict = SLib.load_json_file(SAVE_PATH)
        iron = dict["iron"]
        amon = dict["amon"]

If you’re not using SLib, you can easily install it from here:

I recommend installing and using SLib for saving and loading files, but if you want to work with JSON files without installing it, you can check out these lines in the source: JSON file manager from SLib

FWIW, I tried to execute your script (with a slight modification) and it works in my 4.2.2 stable version of Godot. The produced JSON file seems correct, and the output is: LOAD save_dict: { "amon": 5, "iron": 5 }

extends Node2D

var iron = 5
var amon = 5
var SAVE_PATH : String = "user://resource.bin"

func _ready():
	save_game()
	load_game()

func save():
	var dict = {
		"amon": amon,
		"iron": iron
	}
	return dict

func save_game():
	var savegame = FileAccess.open(SAVE_PATH, FileAccess.WRITE)
	if savegame:
		var jsstr = JSON.stringify(save())
		savegame.store_line(jsstr)
		savegame.close()

func load_game():
	if not FileAccess.file_exists(SAVE_PATH):
		return
		
	var file := FileAccess.open(SAVE_PATH, FileAccess.READ)
	var json := JSON.new()
	json.parse(file.get_line())
	var save_dict := json.get_data() as Dictionary
	
	print("LOAD save_dict: ", save_dict)

Yea but im using godot 4.3 and it has an problme with refreshing the data that is added in-game