FileAccess

Inherits: RefCounted < Object

Provides methods for file reading and writing operations.

Description

This class can be used to permanently store data in the user device's file system and to read from it. This is useful for store game save data or player configuration files.

Here's a sample on how to write and read from a file:

func save(content):
    var file = FileAccess.open("user://save_game.dat", FileAccess.WRITE)
    file.store_string(content)

func load():
    var file = FileAccess.open("user://save_game.dat", FileAccess.READ)
    var content = file.get_as_text()
    return content

In the example above, the file will be saved in the user data folder as specified in the Data paths documentation.

FileAccess will close when it's freed, which happens when it goes out of scope or when it gets assigned with null. close can be used to close it before then explicitly. In C# the reference must be disposed manually, which can be done with the using statement or by calling the Dispose method directly.

Note: To access project resources once exported, it is recommended to use ResourceLoader instead of FileAccess, as some files are converted to engine-specific formats and their original source files might not be present in the exported PCK package.

Note: Files are automatically closed only if the process exits "normally" (such as by clicking the window manager's close button or pressing Alt + F4). If you stop the project execution by pressing F8 while the project is running, the file won't be closed as the game process will be killed. You can work around this by calling flush at regular intervals.

Tutorials

Properties

bool

big_endian

Methods

void

close ( )

bool

eof_reached ( ) const

bool

file_exists ( String path ) static

void

flush ( )

int

get_8 ( ) const

int

get_16 ( ) const

int

get_32 ( ) const

int

get_64 ( ) const

String

get_as_text ( bool skip_cr=false ) const

PackedByteArray

get_buffer ( int length ) const

PackedStringArray

get_csv_line ( String delim="," ) const

float

get_double ( ) const

Error

get_error ( ) const

PackedByteArray

get_file_as_bytes ( String path ) static

String

get_file_as_string ( String path ) static

float

get_float ( ) const

bool

get_hidden_attribute ( String file ) static

<