0 votes

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        

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.

Godot version 3.2.3
in Engine by (21 points)

2 Answers

+1 vote
Best answer

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!

by (21 points)

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

+2 votes

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

by (2,434 points)

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?

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

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.