my save system doesn´t work after export the game

Post any engine-related development questions or issues you are having to receive advice, support, or help.
Post Reply
PotatoWithComputer
Posts: 23
Joined: Tue Jul 18, 2023 3:30 pm
Location: brazil, in vic's farm

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?
Facts about me:

1. my english still a work in process
2. im a potato

Tags:
Owen Lloyd
Posts: 30
Joined: Wed Jul 19, 2023 2:37 am
Location: Brisbane Australia

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.
Last edited by Owen Lloyd on Wed Jul 19, 2023 7:38 pm, edited 1 time in total.
PotatoWithComputer
Posts: 23
Joined: Tue Jul 18, 2023 3:30 pm
Location: brazil, in vic's farm

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!
Facts about me:

1. my english still a work in process
2. im a potato
Post Reply