Godot Version
4.5
Question
Dear Godot Veterans,
I am looking at FileAccess — Godot Engine (stable) documentation in English
I would like to make my code robust in error handling when any error has occurred during saving a file. I see there are get_open_error() and get_error() in there, so I believe this should be just this, right?
For obtaining errors during opening a file:
var file:FileAccess = FileAccess.open( path, FileAccess.WRITE)
if file == null:
var err:Error = FileAccess.get_open_error()
print( "Error Code " + str(err) )
...
...
For obtaining errors during store_line or any other FileAccess operation:
var store_result:bool = file.store_line( ... )
if store_result:
print( "saved successfully" )
file.close()
else:
var err:Error = file.get_error()
print( "Error Code " + str(err) )
file.close()
Questions:
- I believe
file.close()should be performed afterfile.get_error(). Correct? - For file.close() , could there be a possible chance that it could create an error? From FileAccess — Godot Engine (stable) documentation in English :
void close()
Closes the currently opened file and prevents subsequent read/write operations. Use flush() to persist the data to disk without closing the file.
Note: FileAccess will automatically close when it’s freed, which happens when it goes out of scope or when it gets assigned with null. In C# the reference must be disposed after we are done using it, this can be done with the using statement or calling the Dispose method directly.
and also:
void flush()
Writes the file’s buffer to disk. Flushing is automatically performed when the file is closed. This means you don’t need to call flush() manually before closing a file. Still, calling flush() can be used to ensure the data is safe even if the project crashes instead of being closed gracefully.
Note: Only call flush() when you actually need it. Otherwise, it will decrease performance due to constant disk writes.
This means those store_ functions store data in buffers first before they are written to the file, yes? This means there is one possible error. If the disk is full upon closing the file, the game will fail to save the file, yes? Will we get something like Error 13 ERR_FILE_CANT_WRITE here after file.close()? What would be the robust way to deal with this to prevent data loss?
- For disk almost full, I see we can just warn the player of their remaining disk space if it is too low before they start playing with DirAccess — Godot Engine (stable) documentation in English
var free_bytes:int = DirAccess.get_space_left()
From the document, Returns the available space on the current directory's disk, in bytes. - current directory’s disk? How can I point to user:// in this case? I just need to point to the disk where Godot will save, I believe?