File must be opened before use

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By juanMAMAW

I trying to save my scene

just like this…

export var saveScene: String = “”
var file = File.new()

func _on_save_btn_button_up():
save_data()

func save_data():
file.open(“res://Save/save.text”, File.WRITE)
file.store_string(saveScene)
file.close()

but i got this error message “File must be opened before use” in windows

:bust_in_silhouette: Reply From: exuin

Try moving the File.new() assignment inside the save_data() function?

I tried to put the

file = file.new()
inside save_data() function

but got the same result…

juanMAMAW | 2020-09-29 07:33

Oh right, res:// is inaccessible during runtime. You can’t open files in it when you run the program. Try saving files in the user:// directory instead.

exuin | 2020-09-29 15:47

thanks bro, i got it

juanMAMAW | 2020-10-03 00:01

where is the user:// folder located

51m0n55 | 2022-08-25 17:44

:bust_in_silhouette: Reply From: jarlowrey

I encountered this error when I forgot to call file.Close() when initially reading/loading data in _Ready(). Later, when I tried to save and write to the file, the error was thrown.