Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | crayonfish |
Hello,
I’m new to Godot and I’ve completed a simple arcade-style game using Godot that includes a basic save system to save the high score. The save system uses the save_var()
's full_objects
parameter based on this tutorial.
Here is the save and load code:
var data_file = "save.dat"
func save_data():
var data_holder = DataHolder.new()
var file_system = File.new()
file_system.open(data_file, File.WRITE)
file_system.store_var(data_holder, true)
file_system.close()
func load_data():
var data_holder = DataHolder.new()
var file_system = File.new()
if file_system.file_exists(data_file):
file_system.open(data_file, File.READ)
data_holder = file_system.get_var(true)
Score.high_score = data_holder.high_score
file_system.close()
else:
Score.high_score = 0
Here is the code for the custom ‘DataHolder’ object referenced:
extends Reference #was node
class_name DataHolder
#this file will hold all the data we need to save
export var score = 0
export var high_score = 0
The save and load system works as expected in the editor, but once the game has been exported, the system throws the following error:
SCRIPT ERROR: load_data: Invalid get index 'high_score' (on base: 'Reference ()').
The save file is not encrypted, so I’ve taken a look at the both the file created in the editor and by the exported game via text editor and I see that the file is different.
Here’s the plain text version of the file created by the editor:
X Reference script GDScript resource_local_to_scene
resource_name
script/source ・ extends Reference #was node
class_name DataHolder
#this file will hold all the data we need to save
export var score = 0
export var high_score = 0
score
high_score
…and this is the file created by the exported game:
タ Reference script GDScript resource_local_to_scene
resource_name
script/source score
high_score a
Interestingly, if I copy the save file created by the editor over to the save path for the game, the save file works as expected. However, I can’t realistically expect users to ensure they always have a base save file available or that it’s in the right place.
I’m developing in and exporting to Windows, in case that’s relevant.
Any ideas on why this is happening? I’m hoping I’m just missing something obvious.