0 votes

Hi,
I am trying to implement save/load data from a file following a tutorial (https://www.youtube.com/watch?v=2U3iiqd4Wjg&list=PL4vbr3u7UKWqwQlvwvgNcgDL1p_3hcNn2&index=56) but it fails. When I print the error, it just says 7. How can I know what error 7 means? This is the code:

onready var path = "user://save.dat"

func load_data():
var file = File.new()
var err = file.open(path, File.READ)
if err != OK:
    print("error opening data file")
    print(str(err))
    return default_level_info       
var read = {}
read = file.get_var()
return read 
in Engine by (18 points)

1 Answer

+1 vote

To know what the error code means, check the Error enum documentation. 7 is for ERR_FILE_NOT_FOUND. To read a file, it needs to exist already. To create the file if it doesn't exist (and truncate it if it exists), use the File.WRITE mode instead of File.READ.

Call file_exists(path) from your File instance to check whether a file exists at the given path.

by (12,727 points)

Thank you so much, it worked exactly as you described.

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 Frequently asked questions and 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 [email protected] with your username.