Debian Codegraph @

doc/classes/FileAccess.xml

raw symbols SCIP
1 <?xml version="1.0" encoding="UTF-8" ?>
2 <class name="FileAccess" inherits="RefCounted" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
3 <brief_description>
4 Provides methods for file reading and writing operations.
5 </brief_description>
6 <description>
7 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 storing game save data or player configuration files.
8 [b]Example:[/b] How to write and read from a file. The file named [code]"save_game.dat"[/code] will be stored in the user data folder, as specified in the [url=$DOCS_URL/tutorials/io/data_paths.html]Data paths[/url] documentation:
9 [codeblocks]
10 [gdscript]
11 func save_to_file(content):
12 var file = FileAccess.open("user://save_game.dat", FileAccess.WRITE)
13 file.store_string(content)
14
15 func load_from_file():
16 var file = FileAccess.open("user://save_game.dat", FileAccess.READ)
17 var content = file.get_as_text()
18 return content
19 [/gdscript]
20 [csharp]
21 public void SaveToFile(string content)
22 {
23 using var file = FileAccess.Open("user://save_game.dat", FileAccess.ModeFlags.Write);
24 file.StoreString(content);
25 }
26
27 public string LoadFromFile()
28 {
29 using var file = FileAccess.Open("user://save_game.dat", FileAccess.ModeFlags.Read);
30 string content = file.GetAsText();
31 return content;
32 }
33 [/csharp]
34 [/codeblocks]
35 A [FileAccess] instance has its own file cursor, which is the position in bytes in the file where the next read/write operation will occur. Functions such as [method get_8], [method get_16], [method store_8], and [method store_16] will move the file cursor forward by the number of bytes read/written. The file cursor can be moved to a specific position using [method seek] or [method seek_end], and its position can be retrieved using [method get_position].
36 A [FileAccess] instance will close its file when the instance is freed. Since it inherits [RefCounted], this happens automatically when it is no longer in use. [method close] can be called to close it earlier. 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.
37 [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. If using [FileAccess], make sure the file is included in the export by changing its import mode to [b]Keep File (exported as is)[/b] in the Import dock, or, for files where this option is not available, change the non-resource export filter in the Export dialog to include the file's extension (e.g. [code]*.txt[/code]).
38 [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 [kbd]Alt + F4[/kbd]). If you stop the project execution by pressing [kbd]F8[/kbd] 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.
39 </description>
40 <tutorials>
41 <link title="File system">$DOCS_URL/tutorials/scripting/filesystem.html</link>
42 <link title="Runtime file loading and saving">$DOCS_URL/tutorials/io/runtime_file_loading_and_saving.html</link>
43 <link title="Binary serialization API">$DOCS_URL/tutorials/io/binary_serialization_api.html</link>
44 <link title="3D Voxel Demo">https://godotengine.org/asset-library/asset/2755</link>
45 </tutorials>
46 <methods>
47 <method name="close">
48 <return type="void" />
49 <description>
50 Closes the currently opened file and prevents subsequent read/write operations. Use [method flush] to persist the data to disk without closing the file.
51 [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.
52 </description>
53 </method>
54 <method name="create_temp" qualifiers="static">
55 <return type="FileAccess" />
56 <param index="0" name="mode_flags" type="int" enum="FileAccess.ModeFlags" />
57 <param index="1" name="prefix" type="String" default="&quot;&quot;" />
58 <param index="2" name="extension" type="String" default="&quot;&quot;" />
59 <param index="3" name="keep" type="bool" default="false" />
60 <description>
61 Creates a temporary file. This file will be freed when the returned [FileAccess] is freed.
62 If [param prefix] is not empty, it will be prefixed to the file name, separated by a [code]-[/code].
63 If [param extension] is not empty, it will be appended to the temporary file name.
64 If [param keep] is [code]true[/code], the file is not deleted when the returned [FileAccess] is freed.
65 Returns [code]null[/code] if opening the file failed. You can use [method get_open_error] to check the error that occurred.
66 </description>
67 </method>
68 <method name="eof_reached" qualifiers="const">
69 <return type="bool" />
70 <description>
71 Returns [code]true[/code] if the file cursor has already read past the end of the file.
72 [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:
73 [codeblocks]
74 [gdscript]
75 while file.get_position() &lt; file.get_length():
76 # Read data
77 [/gdscript]
78 [csharp]
79 while (file.GetPosition() &lt; file.GetLength())
80 {
81 // Read data
82 }
83 [/csharp]
84 [/codeblocks]
85 </description>
86 </method>
87 <method name="file_exists" qualifiers="static">
88 <return type="bool" />
89 <param index="0" name="path" type="String" />
90 <description>
91 Returns [code]true[/code] if the file exists in the given path.
92 [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.
93 For a non-static, relative equivalent, use [method DirAccess.file_exists].
94 </description>
95 </method>
96 <method name="flush">
97 <return type="void" />
98 <description>
99 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.
100 [b]Note:[/b] Only call [method flush] when you actually need it. Otherwise, it will decrease performance due to constant disk writes.
101 </description>
102 </method>
103 <method name="get_8" qualifiers="const">
104 <return type="int" />
105 <description>
106 Returns the next 8 bits from the file as an integer. This advances the file cursor by 1 byte. See [method store_8] for details on what values can be stored and retrieved this way.
107 </description>
108 </method>
109 <method name="get_16" qualifiers="const">
110 <return type="int" />
111 <description>
112 Returns the next 16 bits from the file as an integer. This advances the file cursor by 2 bytes. See [method store_16] for details on what values can be stored and retrieved this way.
113 </description>
114 </method>
115 <method name="get_32" qualifiers="const">
116 <return type="int" />
117 <description>
118 Returns the next 32 bits from the file as an integer. This advances the file cursor by 4 bytes. See [method store_32] for details on what values can be stored and retrieved this way.
119 </description>
120 </method>
121 <method name="get_64" qualifiers="const">
122 <return type="int" />
123 <description>
124 Returns the next 64 bits from the file as an integer. This advances the file cursor by 8 bytes. See [method store_64] for details on what values can be stored and retrieved this way.
125 </description>
126 </method>
127 <method name="get_access_time" qualifiers="static">
128 <return type="int" />
129 <param index="0" name="file" type="String" />
130 <description>
131 Returns the last time the [param file] was accessed in Unix timestamp format, or [code]0[/code] on error. This Unix timestamp can be converted to another format using the [Time] singleton.
132 </description>
133 </method>
134 <method name="get_as_text" qualifiers="const">
135 <return type="String" />
136 <description>
137 Returns the whole file as a [String]. Text is interpreted as being UTF-8 encoded. This ignores the file cursor and does not affect it.
138 </description>
139 </method>
140 <method name="get_buffer" qualifiers="const">
141 <return type="PackedByteArray" />
142 <param index="0" name="length" type="int" />
143 <description>
144 Returns next [param length] bytes of the file as a [PackedByteArray]. This advances the file cursor by [param length] bytes.
145 </description>
146 </method>
147 <method name="get_csv_line" qualifiers="const">
148 <return type="PackedStringArray" />
149 <param index="0" name="delim" type="String" default="&quot;,&quot;" />
150 <description>
151 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.
152 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. This advances the file cursor to after the newline character at the end of the line.
153 For example, the following CSV lines are valid and will be properly parsed as two strings each:
154 [codeblock lang=text]
155 Alice,"Hello, Bob!"
156 Bob,Alice! What a surprise!
157 Alice,"I thought you'd reply with ""Hello, world""."
158 [/codeblock]
159 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.
160 </description>
161 </method>
162 <method name="get_double" qualifiers="const">
163 <return type="float" />
164 <description>
165 Returns the next 64 bits from the file as a floating-point number. This advances the file cursor by 8 bytes.
166 </description>
167 </method>
168 <method name="get_error" qualifiers="const">
169 <return type="int" enum="Error" />
170 <description>
171 Returns the last error that happened when trying to perform operations. Compare with the [code]ERR_FILE_*[/code] constants from [enum Error].
172 </description>
173 </method>
174 <method name="get_extended_attribute" qualifiers="static">
175 <return type="PackedByteArray" />
176 <param index="0" name="file" type="String" />
177 <param index="1" name="attribute_name" type="String" />
178 <description>
179 Reads the file extended attribute with name [param attribute_name] as a byte array.
180 [b]Note:[/b] This method is implemented on Linux, macOS, and Windows.
181 [b]Note:[/b] Extended attributes support depends on the file system. Attributes will be lost when the file is moved between incompatible file systems.
182 [b]Note:[/b] On Linux, only "user" namespace attributes are accessible, namespace prefix should not be included.
183 [b]Note:[/b] On Windows, alternate data streams are used to store extended attributes.
184 </description>
185 </method>
186 <method name="get_extended_attribute_string" qualifiers="static">
187 <return type="String" />
188 <param index="0" name="file" type="String" />
189 <param index="1" name="attribute_name" type="String" />
190 <description>
191 Reads the file extended attribute with name [param attribute_name] as a UTF-8 encoded string.
192 [b]Note:[/b] This method is implemented on Linux, macOS, and Windows.
193 [b]Note:[/b] Extended attributes support depends on the file system. Attributes will be lost when the file is moved between incompatible file systems.
194 [b]Note:[/b] On Linux, only "user" namespace attributes are accessible, namespace prefix should not be included.
195 [b]Note:[/b] On Windows, alternate data streams are used to store extended attributes.
196 </description>
197 </method>
198 <method name="get_extended_attributes_list" qualifiers="static">
199 <return type="PackedStringArray" />
200 <param index="0" name="file" type="String" />
201 <description>
202 Returns a list of file extended attributes.
203 [b]Note:[/b] This method is implemented on Linux, macOS, and Windows.
204 [b]Note:[/b] Extended attributes support depends on the file system. Attributes will be lost when the file is moved between incompatible file systems.
205 [b]Note:[/b] On Linux, only "user" namespace attributes are accessible, namespace prefix should not be included.
206 [b]Note:[/b] On Windows, alternate data streams are used to store extended attributes.
207 </description>
208 </method>
209 <method name="get_file_as_bytes" qualifiers="static">
210 <return type="PackedByteArray" />
211 <param index="0" name="path" type="String" />
212 <description>
213 Returns the whole [param path] file contents as a [PackedByteArray] without any decoding.
214 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.
215 </description>
216 </method>
217 <method name="get_file_as_string" qualifiers="static">
218 <return type="String" />
219 <param index="0" name="path" type="String" />
220 <description>
221 Returns the whole [param path] file contents as a [String]. Text is interpreted as being UTF-8 encoded.
222 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.
223 </description>
224 </method>
225 <method name="get_float" qualifiers="const">
226 <return type="float" />
227 <description>
228 Returns the next 32 bits from the file as a floating-point number. This advances the file cursor by 4 bytes.
229 </description>
230 </method>
231 <method name="get_half" qualifiers="const">
232 <return type="float" />
233 <description>
234 Returns the next 16 bits from the file as a half-precision floating-point number. This advances the file cursor by 2 bytes.
235 </description>
236 </method>
237 <method name="get_hidden_attribute" qualifiers="static">
238 <return type="bool" />
239 <param index="0" name="file" type="String" />
240 <description>
241 Returns [code]true[/code] if the [b]hidden[/b] attribute is set on the file at the given path.
242 [b]Note:[/b] This method is implemented on iOS, BSD, macOS, and Windows.
243 </description>
244 </method>
245 <method name="get_length" qualifiers="const">
246 <return type="int" />
247 <description>
248 Returns the size of the file in bytes. For a pipe, returns the number of bytes available for reading from the pipe.
249 </description>
250 </method>
251 <method name="get_line" qualifiers="const">
252 <return type="String" />
253 <description>
254 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. This advances the file cursor to after the newline character at the end of the line.
255 Text is interpreted as being UTF-8 encoded.
256 </description>
257 </method>
258 <method name="get_md5" qualifiers="static">
259 <return type="String" />
260 <param index="0" name="path" type="String" />
261 <description>
262 Returns an MD5 String representing the file at the given path or an empty [String] on failure.
263 </description>
264 </method>
265 <method name="get_modified_time" qualifiers="static">
266 <return type="int" />
267 <param index="0" name="file" type="String" />
268 <description>
269 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.
270 </description>
271 </method>
272 <method name="get_open_error" qualifiers="static">
273 <return type="int" enum="Error" />
274 <description>
275 Returns the result of the last [method open] call in the current thread.
276 </description>
277 </method>
278 <method name="get_pascal_string">
279 <return type="String" />
280 <description>
281 Returns a [String] saved in Pascal format from the file, meaning that the length of the string is explicitly stored at the start. See [method store_pascal_string]. This may include newline characters. The file cursor is advanced after the bytes read.
282 Text is interpreted as being UTF-8 encoded.
283 </description>
284 </method>
285 <method name="get_path" qualifiers="const">
286 <return type="String" />
287 <description>
288 Returns the path as a [String] for the current open file.
289 </description>
290 </method>
291 <method name="get_path_absolute" qualifiers="const">
292 <return type="String" />
293 <description>
294 Returns the absolute path as a [String] for the current open file.
295 </description>
296 </method>
297 <method name="get_position" qualifiers="const">
298 <return type="int" />
299 <description>
300 Returns the file cursor's position in bytes from the beginning of the file. This is the file reading/writing cursor set by [method seek] or [method seek_end] and advanced by read/write operations.
301 </description>
302 </method>
303 <method name="get_read_only_attribute" qualifiers="static">
304 <return type="bool" />
305 <param index="0" name="file" type="String" />
306 <description>
307 Returns [code]true[/code] if the [b]read only[/b] attribute is set on the file at the given path.
308 [b]Note:[/b] This method is implemented on iOS, BSD, macOS, and Windows.
309 </description>
310 </method>
311 <method name="get_real" qualifiers="const">
312 <return type="float" />
313 <description>
314 Returns the next bits from the file as a floating-point number. This advances the file cursor by either 4 or 8 bytes, depending on the precision used by the Godot build that saved the file.
315 If the file was saved by a Godot build compiled with the [code]precision=single[/code] option (the default), the number of read bits for that file is 32. Otherwise, if compiled with the [code]precision=double[/code] option, the number of read bits is 64.
316 </description>
317 </method>
318 <method name="get_sha256" qualifiers="static">
319 <return type="String" />
320 <param index="0" name="path" type="String" />
321 <description>
322 Returns an SHA-256 [String] representing the file at the given path or an empty [String] on failure.
323 </description>
324 </method>
325 <method name="get_size" qualifiers="static">
326 <return type="int" />
327 <param index="0" name="file" type="String" />
328 <description>
329 Returns the size of the file at the given path, in bytes, or [code]-1[/code] on error.
330 </description>
331 </method>
332 <method name="get_unix_permissions" qualifiers="static">
333 <return type="int" enum="FileAccess.UnixPermissionFlags" is_bitfield="true" />
334 <param index="0" name="file" type="String" />
335 <description>
336 Returns the UNIX permissions of the file at the given path.
337 [b]Note:[/b] This method is implemented on iOS, Linux/BSD, and macOS.
338 </description>
339 </method>
340 <method name="get_var" qualifiers="const">
341 <return type="Variant" />
342 <param index="0" name="allow_objects" type="bool" default="false" />
343 <description>
344 Returns the next [Variant] value from the file. If [param allow_objects] is [code]true[/code], decoding objects is allowed. This advances the file cursor by the number of bytes read.
345 Internally, this uses the same decoding mechanism as the [method @GlobalScope.bytes_to_var] method, as described in the [url=$DOCS_URL/tutorials/io/binary_serialization_api.html]Binary serialization API[/url] documentation.
346 [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.
347 </description>
348 </method>
349 <method name="is_open" qualifiers="const">
350 <return type="bool" />
351 <description>
352 Returns [code]true[/code] if the file is currently opened.
353 </description>
354 </method>
355 <method name="open" qualifiers="static">
356 <return type="FileAccess" />
357 <param index="0" name="path" type="String" />
358 <param index="1" name="flags" type="int" enum="FileAccess.ModeFlags" />
359 <description>
360 Creates a new [FileAccess] object and opens the file for writing or reading, depending on the flags.
361 Returns [code]null[/code] if opening the file failed. You can use [method get_open_error] to check the error that occurred.
362 </description>
363 </method>
364 <method name="open_compressed" qualifiers="static">
365 <return type="FileAccess" />
366 <param index="0" name="path" type="String" />
367 <param index="1" name="mode_flags" type="int" enum="FileAccess.ModeFlags" />
368 <param index="2" name="compression_mode" type="int" enum="FileAccess.CompressionMode" default="0" />
369 <description>
370 Creates a new [FileAccess] object and opens a compressed file for reading or writing.
371 [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.
372 Returns [code]null[/code] if opening the file failed. You can use [method get_open_error] to check the error that occurred.
373 </description>
374 </method>
375 <method name="open_encrypted" qualifiers="static">
376 <return type="FileAccess" />
377 <param index="0" name="path" type="String" />
378 <param index="1" name="mode_flags" type="int" enum="FileAccess.ModeFlags" />
379 <param index="2" name="key" type="PackedByteArray" />
380 <param index="3" name="iv" type="PackedByteArray" default="PackedByteArray()" />
381 <description>
382 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.
383 [b]Note:[/b] The provided key must be 32 bytes long.
384 Returns [code]null[/code] if opening the file failed. You can use [method get_open_error] to check the error that occurred.
385 </description>
386 </method>
387 <method name="open_encrypted_with_pass" qualifiers="static">
388 <return type="FileAccess" />
389 <param index="0" name="path" type="String" />
390 <param index="1" name="mode_flags" type="int" enum="FileAccess.ModeFlags" />
391 <param index="2" name="pass" type="String" />
392 <description>
393 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.
394 Returns [code]null[/code] if opening the file failed. You can use [method get_open_error] to check the error that occurred.
395 </description>
396 </method>
397 <method name="remove_extended_attribute" qualifiers="static">
398 <return type="int" enum="Error" />
399 <param index="0" name="file" type="String" />
400 <param index="1" name="attribute_name" type="String" />
401 <description>
402 Removes file extended attribute with name [param attribute_name].
403 [b]Note:[/b] This method is implemented on Linux, macOS, and Windows.
404 [b]Note:[/b] Extended attributes support depends on the file system. Attributes will be lost when the file is moved between incompatible file systems.
405 [b]Note:[/b] On Linux, only "user" namespace attributes are accessible, namespace prefix should not be included.
406 [b]Note:[/b] On Windows, alternate data streams are used to store extended attributes.
407 </description>
408 </method>
409 <method name="resize">
410 <return type="int" enum="Error" />
411 <param index="0" name="length" type="int" />
412 <description>
413 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.
414 </description>
415 </method>
416 <method name="seek">
417 <return type="void" />
418 <param index="0" name="position" type="int" />
419 <description>
420 Sets the file cursor to the specified position in bytes, from the beginning of the file. This changes the value returned by [method get_position].
421 </description>
422 </method>
423 <method name="seek_end">
424 <return type="void" />
425 <param index="0" name="position" type="int" default="0" />
426 <description>
427 Sets the file cursor to the specified position in bytes, from the end of the file. This changes the value returned by [method get_position].
428 [b]Note:[/b] This is an offset, so you should use negative numbers otherwise the file cursor will be at the end of the file.
429 </description>
430 </method>
431 <method name="set_extended_attribute" qualifiers="static">
432 <return type="int" enum="Error" />
433 <param index="0" name="file" type="String" />
434 <param index="1" name="attribute_name" type="String" />
435 <param index="2" name="data" type="PackedByteArray" />
436 <description>
437 Writes file extended attribute with name [param attribute_name] as a byte array.
438 [b]Note:[/b] This method is implemented on Linux, macOS, and Windows.
439 [b]Note:[/b] Extended attributes support depends on the file system. Attributes will be lost when the file is moved between incompatible file systems.
440 [b]Note:[/b] On Linux, only "user" namespace attributes are accessible, namespace prefix should not be included.
441 [b]Note:[/b] On Windows, alternate data streams are used to store extended attributes.
442 </description>
443 </method>
444 <method name="set_extended_attribute_string" qualifiers="static">
445 <return type="int" enum="Error" />
446 <param index="0" name="file" type="String" />
447 <param index="1" name="attribute_name" type="String" />
448 <param index="2" name="data" type="String" />
449 <description>
450 Writes file extended attribute with name [param attribute_name] as a UTF-8 encoded string.
451 [b]Note:[/b] This method is implemented on Linux, macOS, and Windows.
452 [b]Note:[/b] Extended attributes support depends on the file system. Attributes will be lost when the file is moved between incompatible file systems.
453 [b]Note:[/b] On Linux, only "user" namespace attributes are accessible, namespace prefix should not be included.
454 [b]Note:[/b] On Windows, alternate data streams are used to store extended attributes.
455 </description>
456 </method>
457 <method name="set_hidden_attribute" qualifiers="static">
458 <return type="int" enum="Error" />
459 <param index="0" name="file" type="String" />
460 <param index="1" name="hidden" type="bool" />
461 <description>
462 Sets file [b]hidden[/b] attribute.
463 [b]Note:[/b] This method is implemented on iOS, BSD, macOS, and Windows.
464 </description>
465 </method>
466 <method name="set_read_only_attribute" qualifiers="static">
467 <return type="int" enum="Error" />
468 <param index="0" name="file" type="String" />
469 <param index="1" name="ro" type="bool" />
470 <description>
471 Sets file [b]read only[/b] attribute.
472 [b]Note:[/b] This method is implemented on iOS, BSD, macOS, and Windows.
473 </description>
474 </method>
475 <method name="set_unix_permissions" qualifiers="static">
476 <return type="int" enum="Error" />
477 <param index="0" name="file" type="String" />
478 <param index="1" name="permissions" type="int" enum="FileAccess.UnixPermissionFlags" is_bitfield="true" />
479 <description>
480 Sets file UNIX permissions.
481 [b]Note:[/b] This method is implemented on iOS, Linux/BSD, and macOS.
482 </description>
483 </method>
484 <method name="store_8">
485 <return type="bool" />
486 <param index="0" name="value" type="int" />
487 <description>
488 Stores an integer as 8 bits in the file. This advances the file cursor by 1 byte. Returns [code]true[/code] if the operation is successful.
489 [b]Note:[/b] The [param value] should lie in the interval [code][0, 255][/code]. Any other value will overflow and wrap around.
490 [b]Note:[/b] If an error occurs, the resulting value of the file position indicator is indeterminate.
491 To store a signed integer, use [method store_64], or convert it manually (see [method store_16] for an example).
492 </description>
493 </method>
494 <method name="store_16">
495 <return type="bool" />
496 <param index="0" name="value" type="int" />
497 <description>
498 Stores an integer as 16 bits in the file. This advances the file cursor by 2 bytes. Returns [code]true[/code] if the operation is successful.
499 [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.
500 [b]Note:[/b] If an error occurs, the resulting value of the file position indicator is indeterminate.
501 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:
502 [codeblocks]
503 [gdscript]
504 const MAX_15B = 1 &lt;&lt; 15
505 const MAX_16B = 1 &lt;&lt; 16
506
507 func unsigned16_to_signed(unsigned):
508 return (unsigned + MAX_15B) % MAX_16B - MAX_15B
509
510 func _ready():
511 var f = FileAccess.open("user://file.dat", FileAccess.WRITE_READ)
512 f.store_16(-42) # This wraps around and stores 65494 (2^16 - 42).
513 f.store_16(121) # In bounds, will store 121.
514 f.seek(0) # Go back to start to read the stored value.
515 var read1 = f.get_16() # 65494
516 var read2 = f.get_16() # 121
517 var converted1 = unsigned16_to_signed(read1) # -42
518 var converted2 = unsigned16_to_signed(read2) # 121
519 [/gdscript]
520 [csharp]
521 public override void _Ready()
522 {
523 using var f = FileAccess.Open("user://file.dat", FileAccess.ModeFlags.WriteRead);
524 f.Store16(unchecked((ushort)-42)); // This wraps around and stores 65494 (2^16 - 42).
525 f.Store16(121); // In bounds, will store 121.
526 f.Seek(0); // Go back to start to read the stored value.
527 ushort read1 = f.Get16(); // 65494
528 ushort read2 = f.Get16(); // 121
529 short converted1 = (short)read1; // -42
530 short converted2 = (short)read2; // 121
531 }
532 [/csharp]
533 [/codeblocks]
534 </description>
535 </method>
536 <method name="store_32">
537 <return type="bool" />
538 <param index="0" name="value" type="int" />
539 <description>
540 Stores an integer as 32 bits in the file. This advances the file cursor by 4 bytes. Returns [code]true[/code] if the operation is successful.
541 [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.
542 [b]Note:[/b] If an error occurs, the resulting value of the file position indicator is indeterminate.
543 To store a signed integer, use [method store_64], or convert it manually (see [method store_16] for an example).
544 </description>
545 </method>
546 <method name="store_64">
547 <return type="bool" />
548 <param index="0" name="value" type="int" />
549 <description>
550 Stores an integer as 64 bits in the file. This advances the file cursor by 8 bytes. Returns [code]true[/code] if the operation is successful.
551 [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).
552 [b]Note:[/b] If an error occurs, the resulting value of the file position indicator is indeterminate.
553 </description>
554 </method>
555 <method name="store_buffer">
556 <return type="bool" />
557 <param index="0" name="buffer" type="PackedByteArray" />
558 <description>
559 Stores the given array of bytes in the file. This advances the file cursor by the number of bytes written. Returns [code]true[/code] if the operation is successful.
560 [b]Note:[/b] If an error occurs, the resulting value of the file position indicator is indeterminate.
561 </description>
562 </method>
563 <method name="store_csv_line">
564 <return type="bool" />
565 <param index="0" name="values" type="PackedStringArray" />
566 <param index="1" name="delim" type="String" default="&quot;,&quot;" />
567 <description>
568 Stores 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.
569 Text will be encoded as UTF-8. Returns [code]true[/code] if the operation is successful.
570 [b]Note:[/b] If an error occurs, the resulting value of the file position indicator is indeterminate.
571 </description>
572 </method>
573 <method name="store_double">
574 <return type="bool" />
575 <param index="0" name="value" type="float" />
576 <description>
577 Stores a floating-point number as 64 bits in the file. This advances the file cursor by 8 bytes. Returns [code]true[/code] if the operation is successful.
578 [b]Note:[/b] If an error occurs, the resulting value of the file position indicator is indeterminate.
579 </description>
580 </method>
581 <method name="store_float">
582 <return type="bool" />
583 <param index="0" name="value" type="float" />
584 <description>
585 Stores a floating-point number as 32 bits in the file. This advances the file cursor by 4 bytes. Returns [code]true[/code] if the operation is successful.
586 [b]Note:[/b] If an error occurs, the resulting value of the file position indicator is indeterminate.
587 </description>
588 </method>
589 <method name="store_half">
590 <return type="bool" />
591 <param index="0" name="value" type="float" />
592 <description>
593 Stores a half-precision floating-point number as 16 bits in the file. This advances the file cursor by 2 bytes. Returns [code]true[/code] if the operation is successful.
594 [b]Note:[/b] If an error occurs, the resulting value of the file position indicator is indeterminate.
595 </description>
596 </method>
597 <method name="store_line">
598 <return type="bool" />
599 <param index="0" name="line" type="String" />
600 <description>
601 Stores [param line] in the file followed by a newline character ([code]\n[/code]), encoding the text as UTF-8. This advances the file cursor by the length of the line, after the newline character. The amount of bytes written depends on the UTF-8 encoded bytes, which may be different from [method String.length] which counts the number of UTF-32 codepoints. Returns [code]true[/code] if the operation is successful.
602 [b]Note:[/b] If an error occurs, the resulting value of the file position indicator is indeterminate.
603 </description>
604 </method>
605 <method name="store_pascal_string">
606 <return type="bool" />
607 <param index="0" name="string" type="String" />
608 <description>
609 Stores the given [String] as a line in the file in Pascal format (i.e. also store the length of the string). Text will be encoded as UTF-8. This advances the file cursor by the number of bytes written depending on the UTF-8 encoded bytes, which may be different from [method String.length] which counts the number of UTF-32 codepoints. Returns [code]true[/code] if the operation is successful.
610 [b]Note:[/b] If an error occurs, the resulting value of the file position indicator is indeterminate.
611 </description>
612 </method>
613 <method name="store_real">
614 <return type="bool" />
615 <param index="0" name="value" type="float" />
616 <description>
617 Stores a floating-point number in the file. This advances the file cursor by either 4 or 8 bytes, depending on the precision used by the current Godot build.
618 If using a Godot build compiled with the [code]precision=single[/code] option (the default), this method will save a 32-bit float. Otherwise, if compiled with the [code]precision=double[/code] option, this will save a 64-bit float. Returns [code]true[/code] if the operation is successful.
619 [b]Note:[/b] If an error occurs, the resulting value of the file position indicator is indeterminate.
620 </description>
621 </method>
622 <method name="store_string">
623 <return type="bool" />
624 <param index="0" name="string" type="String" />
625 <description>
626 Stores [param string] in the file without a newline character ([code]\n[/code]), encoding the text as UTF-8. This advances the file cursor by the length of the string in UTF-8 encoded bytes, which may be different from [method String.length] which counts the number of UTF-32 codepoints. Returns [code]true[/code] if the operation is successful.
627 [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].
628 [b]Note:[/b] If an error occurs, the resulting value of the file position indicator is indeterminate.
629 </description>
630 </method>
631 <method name="store_var">
632 <return type="bool" />
633 <param index="0" name="value" type="Variant" />
634 <param index="1" name="full_objects" type="bool" default="false" />
635 <description>
636 Stores any Variant value in the file. If [param full_objects] is [code]true[/code], encoding objects is allowed (and can potentially include code). This advances the file cursor by the number of bytes written. Returns [code]true[/code] if the operation is successful.
637 Internally, this uses the same encoding mechanism as the [method @GlobalScope.var_to_bytes] method, as described in the [url=$DOCS_URL/tutorials/io/binary_serialization_api.html]Binary serialization API[/url] documentation.
638 [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.
639 [b]Note:[/b] If an error occurs, the resulting value of the file position indicator is indeterminate.
640 </description>
641 </method>
642 </methods>
643 <members>
644 <member name="big_endian" type="bool" setter="set_big_endian" getter="is_big_endian">
645 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.
646 [b]Note:[/b] This is always reset to system endianness, which is little-endian on all supported platforms, whenever you open the file. Therefore, you must set [member big_endian] [i]after[/i] opening the file, not before.
647 </member>
648 </members>
649 <constants>
650 <constant name="READ" value="1" enum="ModeFlags">
651 Opens the file for read operations. The file cursor is positioned at the beginning of the file.
652 </constant>
653 <constant name="WRITE" value="2" enum="ModeFlags">
654 Opens the file for write operations. If the file exists, it is truncated to zero length and its contents are cleared. Otherwise, it is created.
655 [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].
656 </constant>
657 <constant name="READ_WRITE" value="3" enum="ModeFlags">
658 Opens the file for read and write operations. Does not truncate the file. The file cursor is positioned at the beginning of the file.
659 </constant>
660 <constant name="WRITE_READ" value="7" enum="ModeFlags">
661 Opens the file for read and write operations. If the file exists, it is truncated to zero length and its contents are cleared. Otherwise, it is created. The file cursor is positioned at the beginning of the file.
662 [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].
663 </constant>
664 <constant name="COMPRESSION_FASTLZ" value="0" enum="CompressionMode">
665 Uses the [url=https://fastlz.org/]FastLZ[/url] compression method.
666 </constant>
667 <constant name="COMPRESSION_DEFLATE" value="1" enum="CompressionMode">
668 Uses the [url=https://en.wikipedia.org/wiki/DEFLATE]DEFLATE[/url] compression method.
669 </constant>
670 <constant name="COMPRESSION_ZSTD" value="2" enum="CompressionMode">
671 Uses the [url=https://facebook.github.io/zstd/]Zstandard[/url] compression method.
672 </constant>
673 <constant name="COMPRESSION_GZIP" value="3" enum="CompressionMode">
674 Uses the [url=https://www.gzip.org/]gzip[/url] compression method.
675 </constant>
676 <constant name="COMPRESSION_BROTLI" value="4" enum="CompressionMode">
677 Uses the [url=https://github.com/google/brotli]brotli[/url] compression method (only decompression is supported).
678 </constant>
679 <constant name="UNIX_READ_OWNER" value="256" enum="UnixPermissionFlags" is_bitfield="true">
680 Read for owner bit.
681 </constant>
682 <constant name="UNIX_WRITE_OWNER" value="128" enum="UnixPermissionFlags" is_bitfield="true">
683 Write for owner bit.
684 </constant>
685 <constant name="UNIX_EXECUTE_OWNER" value="64" enum="UnixPermissionFlags" is_bitfield="true">
686 Execute for owner bit.
687 </constant>
688 <constant name="UNIX_READ_GROUP" value="32" enum="UnixPermissionFlags" is_bitfield="true">
689 Read for group bit.
690 </constant>
691 <constant name="UNIX_WRITE_GROUP" value="16" enum="UnixPermissionFlags" is_bitfield="true">
692 Write for group bit.
693 </constant>
694 <constant name="UNIX_EXECUTE_GROUP" value="8" enum="UnixPermissionFlags" is_bitfield="true">
695 Execute for group bit.
696 </constant>
697 <constant name="UNIX_READ_OTHER" value="4" enum="UnixPermissionFlags" is_bitfield="true">
698 Read for other bit.
699 </constant>
700 <constant name="UNIX_WRITE_OTHER" value="2" enum="UnixPermissionFlags" is_bitfield="true">
701 Write for other bit.
702 </constant>
703 <constant name="UNIX_EXECUTE_OTHER" value="1" enum="UnixPermissionFlags" is_bitfield="true">
704 Execute for other bit.
705 </constant>
706 <constant name="UNIX_SET_USER_ID" value="2048" enum="UnixPermissionFlags" is_bitfield="true">
707 Set user id on execution bit.
708 </constant>
709 <constant name="UNIX_SET_GROUP_ID" value="1024" enum="UnixPermissionFlags" is_bitfield="true">
710 Set group id on execution bit.
711 </constant>
712 <constant name="UNIX_RESTRICTED_DELETE" value="512" enum="UnixPermissionFlags" is_bitfield="true">
713 Restricted deletion (sticky) bit.
714 </constant>
715 </constants>
716 </class>