2017-09-12 20:42:36 +00:00
<?xml version="1.0" encoding="UTF-8" ?>
2023-07-06 08:08:05 +00:00
<class name= "FileAccess" inherits= "RefCounted" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "../class.xsd" >
2017-09-12 20:42:36 +00:00
<brief_description >
2023-04-27 23:35:33 +00:00
Provides methods for file reading and writing operations.
2017-09-12 20:42:36 +00:00
</brief_description>
<description >
2023-04-27 23:35:33 +00:00
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.
2017-09-12 20:42:36 +00:00
Here's a sample on how to write and read from a file:
2020-10-30 23:37:55 +00:00
[codeblocks]
[gdscript]
2024-04-25 22:51:37 +00:00
func save_to_file(content):
2022-09-05 11:01:31 +00:00
var file = FileAccess.open("user://save_game.dat", FileAccess.WRITE)
2017-09-12 20:42:36 +00:00
file.store_string(content)
2024-04-25 22:51:37 +00:00
func load_from_file():
2022-09-05 11:01:31 +00:00
var file = FileAccess.open("user://save_game.dat", FileAccess.READ)
2017-09-12 20:42:36 +00:00
var content = file.get_as_text()
return content
2020-10-30 23:37:55 +00:00
[/gdscript]
[csharp]
2024-04-25 22:51:37 +00:00
public void SaveToFile(string content)
2020-10-30 23:37:55 +00:00
{
2022-10-02 15:22:51 +00:00
using var file = FileAccess.Open("user://save_game.dat", FileAccess.ModeFlags.Write);
2020-10-30 23:37:55 +00:00
file.StoreString(content);
}
2024-04-25 22:51:37 +00:00
public string LoadFromFile()
2020-10-30 23:37:55 +00:00
{
2022-10-02 15:22:51 +00:00
using var file = FileAccess.Open("user://save_game.dat", FileAccess.ModeFlags.Read);
2020-10-30 23:37:55 +00:00
string content = file.GetAsText();
return content;
}
[/csharp]
[/codeblocks]
2021-11-15 09:43:07 +00:00
In the example above, the file will be saved in the user data folder as specified in the [url=$DOCS_URL/tutorials/io/data_paths.html]Data paths[/url] documentation.
2023-05-31 14:28:21 +00:00
[FileAccess] will close when it's freed, which happens when it goes out of scope or when it gets assigned with [code]null[/code]. [method close] can be used to close it before then explicitly. In C# the reference must be disposed manually, which can be done with the [code]using[/code] statement or by calling the [code]Dispose[/code] method directly.
2023-04-27 23:35:33 +00:00
[b]Note:[/b] 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.
2020-12-15 14:31:04 +00:00
[b]Note:[/b] Files are automatically closed only if the process exits "normally" (such as by clicking the window manager's close button or pressing [b]Alt + F4[/b]). If you stop the project execution by pressing [b]F8[/b] 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 [method flush] at regular intervals.
2017-09-12 20:42:36 +00:00
</description>
<tutorials >
2021-11-15 09:43:07 +00:00
<link title= "File system" > $DOCS_URL/tutorials/scripting/filesystem.html</link>
2023-11-13 13:37:27 +00:00
<link title= "Runtime file loading and saving" > $DOCS_URL/tutorials/io/runtime_file_loading_and_saving.html</link>
2024-03-25 01:20:59 +00:00
<link title= "3D Voxel Demo" > https://godotengine.org/asset-library/asset/2755</link>
2017-09-12 20:42:36 +00:00
</tutorials>
<methods >
2023-02-16 13:25:32 +00:00
<method name= "close" >
<return type= "void" />
<description >
2023-02-20 03:18:57 +00:00
Closes the currently opened file and prevents subsequent read/write operations. Use [method flush] to persist the data to disk without closing the file.
2023-02-16 13:25:32 +00:00
[b]Note:[/b] [FileAccess] will automatically close when it's freed, which happens when it goes out of scope or when it gets assigned with [code]null[/code]. In C# the reference must be disposed after we are done using it, this can be done with the [code]using[/code] statement or calling the [code]Dispose[/code] method directly.
</description>
</method>
2017-09-12 20:42:36 +00:00
<method name= "eof_reached" qualifiers= "const" >
2021-07-30 13:28:05 +00:00
<return type= "bool" />
2017-09-12 20:42:36 +00:00
<description >
2021-05-24 17:56:34 +00:00
Returns [code]true[/code] if the file cursor has already read past the end of the file.
[b]Note:[/b] [code]eof_reached() == false[/code] cannot be used to check whether there is more data available. To loop while there is more data available, use:
[codeblocks]
[gdscript]
while file.get_position() < file.get_length():
# Read data
[/gdscript]
[csharp]
while (file.GetPosition() < file.GetLength())
{
// Read data
}
[/csharp]
[/codeblocks]
2017-09-12 20:42:36 +00:00
</description>
</method>
2022-03-19 12:18:52 +00:00
<method name= "file_exists" qualifiers= "static" >
2021-07-30 13:28:05 +00:00
<return type= "bool" />
2022-08-06 18:11:48 +00:00
<param index= "0" name= "path" type= "String" />
2017-09-12 20:42:36 +00:00
<description >
2017-10-09 10:49:12 +00:00
Returns [code]true[/code] if the file exists in the given path.
2020-09-01 17:40:42 +00:00
[b]Note:[/b] Many resources types are imported (e.g. textures or sound files), and their source asset will not be included in the exported game, as only the imported version is used. See [method ResourceLoader.exists] for an alternative approach that takes resource remapping into account.
2022-04-21 11:10:36 +00:00
For a non-static, relative equivalent, use [method DirAccess.file_exists].
2017-09-12 20:42:36 +00:00
</description>
</method>
2020-12-15 14:31:04 +00:00
<method name= "flush" >
2021-07-30 13:28:05 +00:00
<return type= "void" />
2020-12-15 14:31:04 +00:00
<description >
2022-09-05 11:01:31 +00:00
Writes the file's buffer to disk. Flushing is automatically performed when the file is closed. This means you don't need to call [method flush] manually before closing a file. Still, calling [method flush] can be used to ensure the data is safe even if the project crashes instead of being closed gracefully.
2020-12-15 14:31:04 +00:00
[b]Note:[/b] Only call [method flush] when you actually need it. Otherwise, it will decrease performance due to constant disk writes.
</description>
</method>
2023-05-04 13:18:56 +00:00
<method name= "get_8" qualifiers= "const" >
<return type= "int" />
<description >
Returns the next 8 bits from the file as an integer. See [method store_8] for details on what values can be stored and retrieved this way.
</description>
</method>
2017-09-12 20:42:36 +00:00
<method name= "get_16" qualifiers= "const" >
2021-07-30 13:28:05 +00:00
<return type= "int" />
2017-09-12 20:42:36 +00:00
<description >
2020-06-09 11:05:58 +00:00
Returns the next 16 bits from the file as an integer. See [method store_16] for details on what values can be stored and retrieved this way.
2017-09-12 20:42:36 +00:00
</description>
</method>
<method name= "get_32" qualifiers= "const" >
2021-07-30 13:28:05 +00:00
<return type= "int" />
2017-09-12 20:42:36 +00:00
<description >
2020-06-09 11:05:58 +00:00
Returns the next 32 bits from the file as an integer. See [method store_32] for details on what values can be stored and retrieved this way.
2017-09-12 20:42:36 +00:00
</description>
</method>
<method name= "get_64" qualifiers= "const" >
2021-07-30 13:28:05 +00:00
<return type= "int" />
2017-09-12 20:42:36 +00:00
<description >
2020-06-09 11:05:58 +00:00
Returns the next 64 bits from the file as an integer. See [method store_64] for details on what values can be stored and retrieved this way.
2017-09-12 20:42:36 +00:00
</description>
</method>
<method name= "get_as_text" qualifiers= "const" >
2021-07-30 13:28:05 +00:00
<return type= "String" />
2022-08-06 18:11:48 +00:00
<param index= "0" name= "skip_cr" type= "bool" default= "false" />
2017-09-12 20:42:36 +00:00
<description >
2022-07-31 16:46:53 +00:00
Returns the whole file as a [String]. Text is interpreted as being UTF-8 encoded.
2022-08-12 16:07:53 +00:00
If [param skip_cr] is [code]true[/code], carriage return characters ([code]\r[/code], CR) will be ignored when parsing the UTF-8, so that only line feed characters ([code]\n[/code], LF) represent a new line (Unix convention).
2017-09-12 20:42:36 +00:00
</description>
</method>
<method name= "get_buffer" qualifiers= "const" >
2021-07-30 13:28:05 +00:00
<return type= "PackedByteArray" />
2022-08-06 18:11:48 +00:00
<param index= "0" name= "length" type= "int" />
2017-09-12 20:42:36 +00:00
<description >
2022-08-12 16:07:53 +00:00
Returns next [param length] bytes of the file as a [PackedByteArray].
2017-09-12 20:42:36 +00:00
</description>
</method>
<method name= "get_csv_line" qualifiers= "const" >
2021-07-30 13:28:05 +00:00
<return type= "PackedStringArray" />
2022-08-06 18:11:48 +00:00
<param index= "0" name= "delim" type= "String" default= "","" />
2017-09-12 20:42:36 +00:00
<description >
2022-08-12 16:07:53 +00:00
Returns the next value of the file in CSV (Comma-Separated Values) format. You can pass a different delimiter [param delim] to use other than the default [code]","[/code] (comma). This delimiter must be one-character long, and cannot be a double quotation mark.
2021-08-04 09:20:36 +00:00
Text is interpreted as being UTF-8 encoded. Text values must be enclosed in double quotes if they include the delimiter character. Double quotes within a text value can be escaped by doubling their occurrence.
For example, the following CSV lines are valid and will be properly parsed as two strings each:
2024-03-09 15:58:44 +00:00
[codeblock lang=text]
2021-08-04 09:20:36 +00:00
Alice,"Hello, Bob!"
Bob,Alice! What a surprise!
Alice,"I thought you'd reply with ""Hello, world""."
[/codeblock]
Note how the second line can omit the enclosing quotes as it does not include the delimiter. However it [i]could[/i] very well use quotes, it was only written without for demonstration purposes. The third line must use [code]""[/code] for each quotation mark that needs to be interpreted as such instead of the end of a text value.
2017-09-12 20:42:36 +00:00
</description>
</method>
<method name= "get_double" qualifiers= "const" >
2021-07-30 13:28:05 +00:00
<return type= "float" />
2017-09-12 20:42:36 +00:00
<description >
2019-06-21 23:04:47 +00:00
Returns the next 64 bits from the file as a floating-point number.
2017-09-12 20:42:36 +00:00
</description>
</method>
<method name= "get_error" qualifiers= "const" >
2021-07-30 13:28:05 +00:00
<return type= "int" enum= "Error" />
2017-09-12 20:42:36 +00:00
<description >
2019-06-27 11:24:03 +00:00
Returns the last error that happened when trying to perform operations. Compare with the [code]ERR_FILE_*[/code] constants from [enum Error].
2017-09-12 20:42:36 +00:00
</description>
</method>
2022-11-21 13:04:01 +00:00
<method name= "get_file_as_bytes" qualifiers= "static" >
<return type= "PackedByteArray" />
<param index= "0" name= "path" type= "String" />
<description >
Returns the whole [param path] file contents as a [PackedByteArray] without any decoding.
2023-09-30 22:17:00 +00:00
Returns an empty [PackedByteArray] if an error occurred while opening the file. You can use [method get_open_error] to check the error that occurred.
2022-11-21 13:04:01 +00:00
</description>
</method>
<method name= "get_file_as_string" qualifiers= "static" >
<return type= "String" />
<param index= "0" name= "path" type= "String" />
<description >
Returns the whole [param path] file contents as a [String]. Text is interpreted as being UTF-8 encoded.
2023-09-30 22:17:00 +00:00
Returns an empty [String] if an error occurred while opening the file. You can use [method get_open_error] to check the error that occurred.
2022-11-21 13:04:01 +00:00
</description>
</method>
2017-09-12 20:42:36 +00:00
<method name= "get_float" qualifiers= "const" >
2021-07-30 13:28:05 +00:00
<return type= "float" />
2017-09-12 20:42:36 +00:00
<description >
2019-06-21 23:04:47 +00:00
Returns the next 32 bits from the file as a floating-point number.
2017-09-12 20:42:36 +00:00
</description>
</method>
2023-08-08 06:53:41 +00:00
<method name= "get_hidden_attribute" qualifiers= "static" >
<return type= "bool" />
<param index= "0" name= "file" type= "String" />
<description >
Returns [code]true[/code], if file [code]hidden[/code] attribute is set.
[b]Note:[/b] This method is implemented on iOS, BSD, macOS, and Windows.
</description>
</method>
2021-05-25 06:58:49 +00:00
<method name= "get_length" qualifiers= "const" >
2021-07-30 13:28:05 +00:00
<return type= "int" />
2017-09-12 20:42:36 +00:00
<description >
2017-10-08 11:28:52 +00:00
Returns the size of the file in bytes.
2017-09-12 20:42:36 +00:00
</description>
</method>
<method name= "get_line" qualifiers= "const" >
2021-07-30 13:28:05 +00:00
<return type= "String" />
2017-09-12 20:42:36 +00:00
<description >
2024-04-16 17:18:46 +00:00
Returns the next line of the file as a [String]. The returned string doesn't include newline ([code]\n[/code]) or carriage return ([code]\r[/code]) characters, but does include any other leading or trailing whitespace.
2019-03-06 17:26:32 +00:00
Text is interpreted as being UTF-8 encoded.
2017-09-12 20:42:36 +00:00
</description>
</method>
2022-09-05 11:01:31 +00:00
<method name= "get_md5" qualifiers= "static" >
2021-07-30 13:28:05 +00:00
<return type= "String" />
2022-08-06 18:11:48 +00:00
<param index= "0" name= "path" type= "String" />
2017-09-12 20:42:36 +00:00
<description >
2017-10-09 10:49:12 +00:00
Returns an MD5 String representing the file at the given path or an empty [String] on failure.
2017-09-12 20:42:36 +00:00
</description>
</method>
2022-09-05 11:01:31 +00:00
<method name= "get_modified_time" qualifiers= "static" >
2021-07-30 13:28:05 +00:00
<return type= "int" />
2022-08-06 18:11:48 +00:00
<param index= "0" name= "file" type= "String" />
2017-09-12 20:42:36 +00:00
<description >
2023-08-22 08:13:42 +00:00
Returns the last time the [param file] was modified in Unix timestamp format, or [code]0[/code] on error. This Unix timestamp can be converted to another format using the [Time] singleton.
2017-09-12 20:42:36 +00:00
</description>
</method>
2022-09-05 11:01:31 +00:00
<method name= "get_open_error" qualifiers= "static" >
<return type= "int" enum= "Error" />
<description >
Returns the result of the last [method open] call in the current thread.
</description>
</method>
2017-09-12 20:42:36 +00:00
<method name= "get_pascal_string" >
2021-07-30 13:28:05 +00:00
<return type= "String" />
2017-09-12 20:42:36 +00:00
<description >
2017-10-09 10:49:12 +00:00
Returns a [String] saved in Pascal format from the file.
2019-03-06 17:26:32 +00:00
Text is interpreted as being UTF-8 encoded.
2017-09-12 20:42:36 +00:00
</description>
</method>
2018-04-10 08:12:42 +00:00
<method name= "get_path" qualifiers= "const" >
2021-07-30 13:28:05 +00:00
<return type= "String" />
2018-03-09 14:45:21 +00:00
<description >
Returns the path as a [String] for the current open file.
</description>
</method>
2018-04-10 08:12:42 +00:00
<method name= "get_path_absolute" qualifiers= "const" >
2021-07-30 13:28:05 +00:00
<return type= "String" />
2018-03-09 14:45:21 +00:00
<description >
Returns the absolute path as a [String] for the current open file.
</description>
</method>
2017-09-10 13:37:49 +00:00
<method name= "get_position" qualifiers= "const" >
2021-07-30 13:28:05 +00:00
<return type= "int" />
2017-09-12 20:42:36 +00:00
<description >
2017-10-09 10:49:12 +00:00
Returns the file cursor's position.
2017-09-12 20:42:36 +00:00
</description>
</method>
2023-08-08 06:53:41 +00:00
<method name= "get_read_only_attribute" qualifiers= "static" >
<return type= "bool" />
<param index= "0" name= "file" type= "String" />
<description >
Returns [code]true[/code], if file [code]read only[/code] attribute is set.
[b]Note:[/b] This method is implemented on iOS, BSD, macOS, and Windows.
</description>
</method>
2017-09-12 20:42:36 +00:00
<method name= "get_real" qualifiers= "const" >
2021-07-30 13:28:05 +00:00
<return type= "float" />
2017-09-12 20:42:36 +00:00
<description >
2019-06-21 23:04:47 +00:00
Returns the next bits from the file as a floating-point number.
2017-09-12 20:42:36 +00:00
</description>
</method>
2022-09-05 11:01:31 +00:00
<method name= "get_sha256" qualifiers= "static" >
2021-07-30 13:28:05 +00:00
<return type= "String" />
2022-08-06 18:11:48 +00:00
<param index= "0" name= "path" type= "String" />
2017-09-12 20:42:36 +00:00
<description >
2024-03-13 11:43:07 +00:00
Returns an SHA-256 [String] representing the file at the given path or an empty [String] on failure.
2017-09-12 20:42:36 +00:00
</description>
</method>
2023-08-08 06:53:41 +00:00
<method name= "get_unix_permissions" qualifiers= "static" >
<return type= "int" enum= "FileAccess.UnixPermissionFlags" is_bitfield= "true" />
<param index= "0" name= "file" type= "String" />
<description >
Returns file UNIX permissions.
[b]Note:[/b] This method is implemented on iOS, Linux/BSD, and macOS.
</description>
</method>
2017-09-12 20:42:36 +00:00
<method name= "get_var" qualifiers= "const" >
2021-07-30 13:28:05 +00:00
<return type= "Variant" />
2022-08-06 18:11:48 +00:00
<param index= "0" name= "allow_objects" type= "bool" default= "false" />
2017-09-12 20:42:36 +00:00
<description >
2022-08-12 16:07:53 +00:00
Returns the next [Variant] value from the file. If [param allow_objects] is [code]true[/code], decoding objects is allowed.
2023-01-17 17:42:26 +00:00
Internally, this uses the same decoding mechanism as the [method @GlobalScope.bytes_to_var] method.
2019-06-21 23:04:47 +00:00
[b]Warning:[/b] Deserialized objects can contain code which gets executed. Do not use this option if the serialized object comes from untrusted sources to avoid potential security threats such as remote code execution.
2017-09-12 20:42:36 +00:00
</description>
</method>
<method name= "is_open" qualifiers= "const" >
2021-07-30 13:28:05 +00:00
<return type= "bool" />
2017-09-12 20:42:36 +00:00
<description >
2017-10-08 11:28:52 +00:00
Returns [code]true[/code] if the file is currently opened.
2017-09-12 20:42:36 +00:00
</description>
</method>
2022-09-05 11:01:31 +00:00
<method name= "open" qualifiers= "static" >
<return type= "FileAccess" />
2022-08-06 18:11:48 +00:00
<param index= "0" name= "path" type= "String" />
2022-09-05 11:01:31 +00:00
<param index= "1" name= "flags" type= "int" enum= "FileAccess.ModeFlags" />
2017-09-12 20:42:36 +00:00
<description >
2022-09-05 11:01:31 +00:00
Creates a new [FileAccess] object and opens the file for writing or reading, depending on the flags.
2022-09-30 12:23:36 +00:00
Returns [code]null[/code] if opening the file failed. You can use [method get_open_error] to check the error that occurred.
2017-09-12 20:42:36 +00:00
</description>
</method>
2022-09-05 11:01:31 +00:00
<method name= "open_compressed" qualifiers= "static" >
<return type= "FileAccess" />
2022-08-06 18:11:48 +00:00
<param index= "0" name= "path" type= "String" />
2022-09-05 11:01:31 +00:00
<param index= "1" name= "mode_flags" type= "int" enum= "FileAccess.ModeFlags" />
<param index= "2" name= "compression_mode" type= "int" enum= "FileAccess.CompressionMode" default= "0" />
2017-09-12 20:42:36 +00:00
<description >
2022-09-05 11:01:31 +00:00
Creates a new [FileAccess] object and opens a compressed file for reading or writing.
2021-04-28 22:47:16 +00:00
[b]Note:[/b] [method open_compressed] can only read files that were saved by Godot, not third-party compression formats. See [url=https://github.com/godotengine/godot/issues/28999]GitHub issue #28999[/url] for a workaround.
2022-09-30 12:23:36 +00:00
Returns [code]null[/code] if opening the file failed. You can use [method get_open_error] to check the error that occurred.
2017-09-12 20:42:36 +00:00
</description>
</method>
2022-09-05 11:01:31 +00:00
<method name= "open_encrypted" qualifiers= "static" >
<return type= "FileAccess" />
2022-08-06 18:11:48 +00:00
<param index= "0" name= "path" type= "String" />
2022-09-05 11:01:31 +00:00
<param index= "1" name= "mode_flags" type= "int" enum= "FileAccess.ModeFlags" />
2022-08-06 18:11:48 +00:00
<param index= "2" name= "key" type= "PackedByteArray" />
2017-09-12 20:42:36 +00:00
<description >
2022-09-05 11:01:31 +00:00
Creates a new [FileAccess] object and opens an encrypted file in write or read mode. You need to pass a binary key to encrypt/decrypt it.
2020-07-29 05:40:02 +00:00
[b]Note:[/b] The provided key must be 32 bytes long.
2022-09-30 12:23:36 +00:00
Returns [code]null[/code] if opening the file failed. You can use [method get_open_error] to check the error that occurred.
2017-09-12 20:42:36 +00:00
</description>
</method>
2022-09-05 11:01:31 +00:00
<method name= "open_encrypted_with_pass" qualifiers= "static" >
<return type= "FileAccess" />
2022-08-06 18:11:48 +00:00
<param index= "0" name= "path" type= "String" />
2022-09-05 11:01:31 +00:00
<param index= "1" name= "mode_flags" type= "int" enum= "FileAccess.ModeFlags" />
2022-08-06 18:11:48 +00:00
<param index= "2" name= "pass" type= "String" />
2017-09-12 20:42:36 +00:00
<description >
2022-09-05 11:01:31 +00:00
Creates a new [FileAccess] object and opens an encrypted file in write or read mode. You need to pass a password to encrypt/decrypt it.
2022-09-30 12:23:36 +00:00
Returns [code]null[/code] if opening the file failed. You can use [method get_open_error] to check the error that occurred.
2017-09-12 20:42:36 +00:00
</description>
</method>
2024-04-08 19:09:34 +00:00
<method name= "resize" >
<return type= "int" enum= "Error" />
<param index= "0" name= "length" type= "int" />
<description >
Resizes the file to a specified length. The file must be open in a mode that permits writing. If the file is extended, NUL characters are appended. If the file is truncated, all data from the end file to the original length of the file is lost.
</description>
</method>
2017-09-12 20:42:36 +00:00
<method name= "seek" >
2021-07-30 13:28:05 +00:00
<return type= "void" />
2022-08-06 18:11:48 +00:00
<param index= "0" name= "position" type= "int" />
2017-09-12 20:42:36 +00:00
<description >
2019-06-21 23:04:47 +00:00
Changes the file reading/writing cursor to the specified position (in bytes from the beginning of the file).
2017-09-12 20:42:36 +00:00
</description>
</method>
<method name= "seek_end" >
2021-07-30 13:28:05 +00:00
<return type= "void" />
2022-08-06 18:11:48 +00:00
<param index= "0" name= "position" type= "int" default= "0" />
2017-09-12 20:42:36 +00:00
<description >
2019-06-21 23:04:47 +00:00
Changes the file reading/writing cursor to the specified position (in bytes from the end of the file).
[b]Note:[/b] This is an offset, so you should use negative numbers or the cursor will be at the end of the file.
2017-09-12 20:42:36 +00:00
</description>
</method>
2023-08-08 06:53:41 +00:00
<method name= "set_hidden_attribute" qualifiers= "static" >
<return type= "int" enum= "Error" />
<param index= "0" name= "file" type= "String" />
<param index= "1" name= "hidden" type= "bool" />
<description >
2023-10-02 18:11:43 +00:00
Sets file [b]hidden[/b] attribute.
2023-08-08 06:53:41 +00:00
[b]Note:[/b] This method is implemented on iOS, BSD, macOS, and Windows.
</description>
</method>
<method name= "set_read_only_attribute" qualifiers= "static" >
<return type= "int" enum= "Error" />
<param index= "0" name= "file" type= "String" />
<param index= "1" name= "ro" type= "bool" />
<description >
2023-10-02 18:11:43 +00:00
Sets file [b]read only[/b] attribute.
2023-08-08 06:53:41 +00:00
[b]Note:[/b] This method is implemented on iOS, BSD, macOS, and Windows.
</description>
</method>
<method name= "set_unix_permissions" qualifiers= "static" >
<return type= "int" enum= "Error" />
<param index= "0" name= "file" type= "String" />
<param index= "1" name= "permissions" type= "int" enum= "FileAccess.UnixPermissionFlags" is_bitfield= "true" />
<description >
Sets file UNIX permissions.
[b]Note:[/b] This method is implemented on iOS, Linux/BSD, and macOS.
</description>
</method>
2023-05-04 13:18:56 +00:00
<method name= "store_8" >
<return type= "void" />
<param index= "0" name= "value" type= "int" />
<description >
Stores an integer as 8 bits in the file.
[b]Note:[/b] The [param value] should lie in the interval [code][0, 255][/code]. Any other value will overflow and wrap around.
To store a signed integer, use [method store_64], or convert it manually (see [method store_16] for an example).
</description>
</method>
2017-09-12 20:42:36 +00:00
<method name= "store_16" >
2021-07-30 13:28:05 +00:00
<return type= "void" />
2022-08-06 18:11:48 +00:00
<param index= "0" name= "value" type= "int" />
2017-09-12 20:42:36 +00:00
<description >
2017-10-09 10:49:12 +00:00
Stores an integer as 16 bits in the file.
2022-08-12 16:07:53 +00:00
[b]Note:[/b] The [param value] should lie in the interval [code][0, 2^16 - 1][/code]. Any other value will overflow and wrap around.
2020-06-09 11:05:58 +00:00
To store a signed integer, use [method store_64] or store a signed integer from the interval [code][-2^15, 2^15 - 1][/code] (i.e. keeping one bit for the signedness) and compute its sign manually when reading. For example:
2020-10-30 23:37:55 +00:00
[codeblocks]
[gdscript]
2020-06-09 11:05:58 +00:00
const MAX_15B = 1 < < 15
const MAX_16B = 1 < < 16
func unsigned16_to_signed(unsigned):
return (unsigned + MAX_15B) % MAX_16B - MAX_15B
func _ready():
2022-10-02 15:22:51 +00:00
var f = FileAccess.open("user://file.dat", FileAccess.WRITE_READ)
2020-06-09 11:05:58 +00:00
f.store_16(-42) # This wraps around and stores 65494 (2^16 - 42).
f.store_16(121) # In bounds, will store 121.
f.seek(0) # Go back to start to read the stored value.
var read1 = f.get_16() # 65494
var read2 = f.get_16() # 121
var converted1 = unsigned16_to_signed(read1) # -42
var converted2 = unsigned16_to_signed(read2) # 121
2020-10-30 23:37:55 +00:00
[/gdscript]
[csharp]
public override void _Ready()
{
2022-10-02 15:22:51 +00:00
using var f = FileAccess.Open("user://file.dat", FileAccess.ModeFlags.WriteRead);
2020-10-30 23:37:55 +00:00
f.Store16(unchecked((ushort)-42)); // This wraps around and stores 65494 (2^16 - 42).
f.Store16(121); // In bounds, will store 121.
f.Seek(0); // Go back to start to read the stored value.
ushort read1 = f.Get16(); // 65494
ushort read2 = f.Get16(); // 121
2023-01-31 17:21:09 +00:00
short converted1 = (short)read1; // -42
short converted2 = (short)read2; // 121
2020-10-30 23:37:55 +00:00
}
[/csharp]
[/codeblocks]
2017-09-12 20:42:36 +00:00
</description>
</method>
<method name= "store_32" >
2021-07-30 13:28:05 +00:00
<return type= "void" />
2022-08-06 18:11:48 +00:00
<param index= "0" name= "value" type= "int" />
2017-09-12 20:42:36 +00:00
<description >
2017-10-09 10:49:12 +00:00
Stores an integer as 32 bits in the file.
2022-08-12 16:07:53 +00:00
[b]Note:[/b] The [param value] should lie in the interval [code][0, 2^32 - 1][/code]. Any other value will overflow and wrap around.
2020-06-09 11:05:58 +00:00
To store a signed integer, use [method store_64], or convert it manually (see [method store_16] for an example).
2017-09-12 20:42:36 +00:00
</description>
</method>
<method name= "store_64" >
2021-07-30 13:28:05 +00:00
<return type= "void" />
2022-08-06 18:11:48 +00:00
<param index= "0" name= "value" type= "int" />
2017-09-12 20:42:36 +00:00
<description >
2017-10-09 10:49:12 +00:00
Stores an integer as 64 bits in the file.
2022-08-12 16:07:53 +00:00
[b]Note:[/b] The [param value] must lie in the interval [code][-2^63, 2^63 - 1][/code] (i.e. be a valid [int] value).
2017-09-12 20:42:36 +00:00
</description>
</method>
<method name= "store_buffer" >
2021-07-30 13:28:05 +00:00
<return type= "void" />
2022-08-06 18:11:48 +00:00
<param index= "0" name= "buffer" type= "PackedByteArray" />
2017-09-12 20:42:36 +00:00
<description >
2017-10-09 10:49:12 +00:00
Stores the given array of bytes in the file.
2017-09-12 20:42:36 +00:00
</description>
</method>
2018-11-13 11:53:24 +00:00
<method name= "store_csv_line" >
2021-07-30 13:28:05 +00:00
<return type= "void" />
2022-08-06 18:11:48 +00:00
<param index= "0" name= "values" type= "PackedStringArray" />
<param index= "1" name= "delim" type= "String" default= "","" />
2018-11-13 11:53:24 +00:00
<description >
2022-08-12 16:07:53 +00:00
Store the given [PackedStringArray] in the file as a line formatted in the CSV (Comma-Separated Values) format. You can pass a different delimiter [param delim] to use other than the default [code]","[/code] (comma). This delimiter must be one-character long.
2019-03-06 17:26:32 +00:00
Text will be encoded as UTF-8.
2018-11-13 11:53:24 +00:00
</description>
</method>
2017-09-12 20:42:36 +00:00
<method name= "store_double" >
2021-07-30 13:28:05 +00:00
<return type= "void" />
2022-08-06 18:11:48 +00:00
<param index= "0" name= "value" type= "float" />
2017-09-12 20:42:36 +00:00
<description >
2019-06-21 23:04:47 +00:00
Stores a floating-point number as 64 bits in the file.
2017-09-12 20:42:36 +00:00
</description>
</method>
<method name= "store_float" >
2021-07-30 13:28:05 +00:00
<return type= "void" />
2022-08-06 18:11:48 +00:00
<param index= "0" name= "value" type= "float" />
2017-09-12 20:42:36 +00:00
<description >
2019-06-21 23:04:47 +00:00
Stores a floating-point number as 32 bits in the file.
2017-09-12 20:42:36 +00:00
</description>
</method>
<method name= "store_line" >
2021-07-30 13:28:05 +00:00
<return type= "void" />
2022-08-06 18:11:48 +00:00
<param index= "0" name= "line" type= "String" />
2017-09-12 20:42:36 +00:00
<description >
2022-08-12 16:07:53 +00:00
Appends [param line] to the file followed by a line return character ([code]\n[/code]), encoding the text as UTF-8.
2017-09-12 20:42:36 +00:00
</description>
</method>
<method name= "store_pascal_string" >
2021-07-30 13:28:05 +00:00
<return type= "void" />
2022-08-06 18:11:48 +00:00
<param index= "0" name= "string" type= "String" />
2017-09-12 20:42:36 +00:00
<description >
2017-10-09 10:49:12 +00:00
Stores the given [String] as a line in the file in Pascal format (i.e. also store the length of the string).
2019-03-06 17:26:32 +00:00
Text will be encoded as UTF-8.
2017-09-12 20:42:36 +00:00
</description>
</method>
<method name= "store_real" >
2021-07-30 13:28:05 +00:00
<return type= "void" />
2022-08-06 18:11:48 +00:00
<param index= "0" name= "value" type= "float" />
2017-09-12 20:42:36 +00:00
<description >
2019-06-21 23:04:47 +00:00
Stores a floating-point number in the file.
2017-09-12 20:42:36 +00:00
</description>
</method>
<method name= "store_string" >
2021-07-30 13:28:05 +00:00
<return type= "void" />
2022-08-06 18:11:48 +00:00
<param index= "0" name= "string" type= "String" />
2017-09-12 20:42:36 +00:00
<description >
2022-08-12 16:07:53 +00:00
Appends [param string] to the file without a line return, encoding the text as UTF-8.
2021-11-10 13:42:48 +00:00
[b]Note:[/b] This method is intended to be used to write text files. The string is stored as a UTF-8 encoded buffer without string length or terminating zero, which means that it can't be loaded back easily. If you want to store a retrievable string in a binary file, consider using [method store_pascal_string] instead. For retrieving strings from a text file, you can use [code]get_buffer(length).get_string_from_utf8()[/code] (if you know the length) or [method get_as_text].
2017-09-12 20:42:36 +00:00
</description>
</method>
<method name= "store_var" >
2021-07-30 13:28:05 +00:00
<return type= "void" />
2022-08-06 18:11:48 +00:00
<param index= "0" name= "value" type= "Variant" />
<param index= "1" name= "full_objects" type= "bool" default= "false" />
2017-09-12 20:42:36 +00:00
<description >
2022-08-12 16:07:53 +00:00
Stores any Variant value in the file. If [param full_objects] is [code]true[/code], encoding objects is allowed (and can potentially include code).
2023-01-17 17:42:26 +00:00
Internally, this uses the same encoding mechanism as the [method @GlobalScope.var_to_bytes] method.
2021-06-16 03:00:19 +00:00
[b]Note:[/b] Not all properties are included. Only properties that are configured with the [constant PROPERTY_USAGE_STORAGE] flag set will be serialized. You can add a new usage flag to a property by overriding the [method Object._get_property_list] method in your class. You can also check how property usage is configured by calling [method Object._get_property_list]. See [enum PropertyUsageFlags] for the possible usage flags.
2017-09-12 20:42:36 +00:00
</description>
</method>
</methods>
2018-01-11 22:38:35 +00:00
<members >
2022-09-05 11:01:31 +00:00
<member name= "big_endian" type= "bool" setter= "set_big_endian" getter= "is_big_endian" >
2021-02-02 20:59:24 +00:00
If [code]true[/code], the file is read with big-endian [url=https://en.wikipedia.org/wiki/Endianness]endianness[/url]. If [code]false[/code], the file is read with little-endian endianness. If in doubt, leave this to [code]false[/code] as most files are written with little-endian endianness.
2021-05-20 12:58:03 +00:00
[b]Note:[/b] [member big_endian] is only about the file format, not the CPU type. The CPU endianness doesn't affect the default endianness for files written.
[b]Note:[/b] This is always reset to [code]false[/code] whenever you open the file. Therefore, you must set [member big_endian] [i]after[/i] opening the file, not before.
2018-01-11 22:38:35 +00:00
</member>
</members>
2017-09-12 20:42:36 +00:00
<constants >
2017-11-24 22:16:30 +00:00
<constant name= "READ" value= "1" enum= "ModeFlags" >
2020-08-04 05:05:48 +00:00
Opens the file for read operations. The cursor is positioned at the beginning of the file.
2017-09-12 20:42:36 +00:00
</constant>
2017-11-24 22:16:30 +00:00
<constant name= "WRITE" value= "2" enum= "ModeFlags" >
2020-08-04 05:05:48 +00:00
Opens the file for write operations. The file is created if it does not exist, and truncated if it does.
2024-05-30 07:51:41 +00:00
[b]Note:[/b] When creating a file it must be in an already existing directory. To recursively create directories for a file path, see [method DirAccess.make_dir_recursive].
2017-09-12 20:42:36 +00:00
</constant>
2017-11-24 22:16:30 +00:00
<constant name= "READ_WRITE" value= "3" enum= "ModeFlags" >
2020-08-04 05:05:48 +00:00
Opens the file for read and write operations. Does not truncate the file. The cursor is positioned at the beginning of the file.
2017-09-12 20:42:36 +00:00
</constant>
2017-11-24 22:16:30 +00:00
<constant name= "WRITE_READ" value= "7" enum= "ModeFlags" >
2020-08-04 05:05:48 +00:00
Opens the file for read and write operations. The file is created if it does not exist, and truncated if it does. The cursor is positioned at the beginning of the file.
2024-05-30 07:51:41 +00:00
[b]Note:[/b] When creating a file it must be in an already existing directory. To recursively create directories for a file path, see [method DirAccess.make_dir_recursive].
2017-09-12 20:42:36 +00:00
</constant>
2017-11-24 22:16:30 +00:00
<constant name= "COMPRESSION_FASTLZ" value= "0" enum= "CompressionMode" >
2021-08-22 01:56:25 +00:00
Uses the [url=https://fastlz.org/]FastLZ[/url] compression method.
2017-09-12 20:42:36 +00:00
</constant>
2017-11-24 22:16:30 +00:00
<constant name= "COMPRESSION_DEFLATE" value= "1" enum= "CompressionMode" >
2019-06-21 23:04:47 +00:00
Uses the [url=https://en.wikipedia.org/wiki/DEFLATE]DEFLATE[/url] compression method.
2017-09-12 20:42:36 +00:00
</constant>
2017-11-24 22:16:30 +00:00
<constant name= "COMPRESSION_ZSTD" value= "2" enum= "CompressionMode" >
2019-06-21 23:04:47 +00:00
Uses the [url=https://facebook.github.io/zstd/]Zstandard[/url] compression method.
2017-09-12 20:42:36 +00:00
</constant>
2017-11-24 22:16:30 +00:00
<constant name= "COMPRESSION_GZIP" value= "3" enum= "CompressionMode" >
2019-06-21 23:04:47 +00:00
Uses the [url=https://www.gzip.org/]gzip[/url] compression method.
2017-09-12 20:42:36 +00:00
</constant>
2023-03-29 06:31:25 +00:00
<constant name= "COMPRESSION_BROTLI" value= "4" enum= "CompressionMode" >
Uses the [url=https://github.com/google/brotli]brotli[/url] compression method (only decompression is supported).
</constant>
2023-08-08 06:53:41 +00:00
<constant name= "UNIX_READ_OWNER" value= "256" enum= "UnixPermissionFlags" is_bitfield= "true" >
Read for owner bit.
</constant>
<constant name= "UNIX_WRITE_OWNER" value= "128" enum= "UnixPermissionFlags" is_bitfield= "true" >
Write for owner bit.
</constant>
<constant name= "UNIX_EXECUTE_OWNER" value= "64" enum= "UnixPermissionFlags" is_bitfield= "true" >
Execute for owner bit.
</constant>
<constant name= "UNIX_READ_GROUP" value= "32" enum= "UnixPermissionFlags" is_bitfield= "true" >
Read for group bit.
</constant>
<constant name= "UNIX_WRITE_GROUP" value= "16" enum= "UnixPermissionFlags" is_bitfield= "true" >
Write for group bit.
</constant>
<constant name= "UNIX_EXECUTE_GROUP" value= "8" enum= "UnixPermissionFlags" is_bitfield= "true" >
Execute for group bit.
</constant>
<constant name= "UNIX_READ_OTHER" value= "4" enum= "UnixPermissionFlags" is_bitfield= "true" >
Read for other bit.
</constant>
<constant name= "UNIX_WRITE_OTHER" value= "2" enum= "UnixPermissionFlags" is_bitfield= "true" >
Write for other bit.
</constant>
<constant name= "UNIX_EXECUTE_OTHER" value= "1" enum= "UnixPermissionFlags" is_bitfield= "true" >
Execute for other bit.
</constant>
<constant name= "UNIX_SET_USER_ID" value= "2048" enum= "UnixPermissionFlags" is_bitfield= "true" >
Set user id on execution bit.
</constant>
<constant name= "UNIX_SET_GROUP_ID" value= "1024" enum= "UnixPermissionFlags" is_bitfield= "true" >
Set group id on execution bit.
</constant>
<constant name= "UNIX_RESTRICTED_DELETE" value= "512" enum= "UnixPermissionFlags" is_bitfield= "true" >
Restricted deletion (sticky) bit.
</constant>
2017-09-12 20:42:36 +00:00
</constants>
</class>