Save file breaks after game export

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: 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.

:bust_in_silhouette: Reply From: Inces

Godot has this export issue, that You have to manually add extensions of all files used by Godot that are not .tres .tscn .gd and so on. I was using JSON files, and I had to manually input *.json in Project/Export/Resources tab. I don’t know what extension Your file has, if You insert this extension in described tab, Your error should go away

Thank you for the quick reply! I just gave it a try and unfortunately the error persists. I’m saving to a .dat file, but I believe it just writes a text file. I tried including both *.dat and *.txt in the resources tab you mentioned, but no luck. Anything else worth trying?

crayonfish | 2021-03-28 18:20

Wouldn’t exactly call it an issue as it’s a design choice that works as intended

Export

Should work with the extension .dat not the actual content.txtand remember when specifying multiple exports to seperate them with a comma *.dat, *.txt

Wakatta | 2021-03-28 22:28

Thanks for the reply! Yes, I tried that, but no dice, unfortunately. I did manage to figure it out, though, so I’ll post a separate answer.

crayonfish | 2021-03-28 23:04

:bust_in_silhouette: Reply From: crayonfish

So I ended up messing around with several different options in the export dialogue window and it turns out the culprit was the Script Export Mode in the Script tab. By default (for me, anyway), it was set to ‘Compiled’. Setting it to ‘Text’ resolved the issue.

Thank you to everyone who replied - it helped me consider trying different options out!

Fantastic great to know you solved it and I also learned something as a result

Wakatta | 2021-03-28 23:36