Page 1 of 1

my save system doesn´t work after export the game

Posted: Wed Jul 19, 2023 4:24 pm
by PotatoWithComputer
hello!

I made a saving system that saves the variables in a file created so that when the game runs again, it returns with the saved data... it works when I run it in the godot editor but, for some reason, it doesn't work when I I export the game and run it... it simply doesn't save the data... here is the code:

Code: Select all

extends Node

onready var player = get_node("/root/test
/player")

const Save_file = "res://assets/save_file.save"
var game_data = {}

func _ready():
	load_data()

func save_data():
	var file = File.new()
	if player == null:
		game_data = {
		#var
		}
	else:
		game_data = {
		#var
		}
	file.open(Save_file, File.WRITE)
	file.store_var(game_data)
	file.close()

func load_data():
	var file = File.new()
	if not file.file_exists(Save_file):
		if player == null:
			game_data = {
			#var
			}
		else:
			Global.save_existe = true
			game_data = {
			#var
			}
		save_data()
	file.open(Save_file, File.READ)
	game_data = file.get_var()
	file.close()
	#var
	if player != null:
		#var
am i missing something? Is it possible to make this code work?

Re: my save system doesn´t work after export the game

Posted: Wed Jul 19, 2023 5:45 pm
by Owen Lloyd
Hi, hopefully I am not about to lead you astray.

I think you need to use "user" instead of "res" in your data path.

"res://assets/save_file.save"
becomes
"user://assets/save_file.save"

"res" refers to the project proper. Many reasons not to save there, including that a compiled/installed project might end up in folders that do not have write permissions.

"user" refers to the area where the OS suggests user data can be read and written. You can also use "user" within the engine.

If you dont want to use "user" in your development stage you could try something like this:

Code: Select all

	var datapath:String
	if not OS.has_feature("standalone"):
		datapath = ProjectSettings.globalize_path("res://yourpathhere")
	else:
		datapath = ProjectSettings.globalize_path("user://yourpathhere")
which should save in the right place if compiled, and in the project folder if not.


Edit: You might need to create the "user" path if you are going to use subfolders in the godot generated "user" path.

Re: my save system doesn´t work after export the game

Posted: Wed Jul 19, 2023 7:33 pm
by PotatoWithComputer
Ok, thanks for the answer!

I will try to use this code... I dont know almost anything about save system in godot so, this will help me a lot!