class_fileaccess.rst 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  1. :github_url: hide
  2. .. DO NOT EDIT THIS FILE!!!
  3. .. Generated automatically from Godot engine sources.
  4. .. Generator: https://github.com/godotengine/godot/tree/4.2/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/4.2/doc/classes/FileAccess.xml.
  6. .. _class_FileAccess:
  7. FileAccess
  8. ==========
  9. **Inherits:** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
  10. Provides methods for file reading and writing operations.
  11. .. rst-class:: classref-introduction-group
  12. Description
  13. -----------
  14. 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.
  15. Here's a sample on how to write and read from a file:
  16. .. tabs::
  17. .. code-tab:: gdscript
  18. func save(content):
  19. var file = FileAccess.open("user://save_game.dat", FileAccess.WRITE)
  20. file.store_string(content)
  21. func load():
  22. var file = FileAccess.open("user://save_game.dat", FileAccess.READ)
  23. var content = file.get_as_text()
  24. return content
  25. .. code-tab:: csharp
  26. public void Save(string content)
  27. {
  28. using var file = FileAccess.Open("user://save_game.dat", FileAccess.ModeFlags.Write);
  29. file.StoreString(content);
  30. }
  31. public string Load()
  32. {
  33. using var file = FileAccess.Open("user://save_game.dat", FileAccess.ModeFlags.Read);
  34. string content = file.GetAsText();
  35. return content;
  36. }
  37. In the example above, the file will be saved in the user data folder as specified in the :doc:`Data paths <../tutorials/io/data_paths>` documentation.
  38. \ **FileAccess** will close when it's freed, which happens when it goes out of scope or when it gets assigned with ``null``. :ref:`close<class_FileAccess_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 ``using`` statement or by calling the ``Dispose`` method directly.
  39. \ **Note:** To access project resources once exported, it is recommended to use :ref:`ResourceLoader<class_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.
  40. \ **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 :ref:`flush<class_FileAccess_method_flush>` at regular intervals.
  41. .. rst-class:: classref-introduction-group
  42. Tutorials
  43. ---------
  44. - :doc:`File system <../tutorials/scripting/filesystem>`
  45. - :doc:`Runtime file loading and saving <../tutorials/io/runtime_file_loading_and_saving>`
  46. - `3D Voxel Demo <https://godotengine.org/asset-library/asset/676>`__
  47. .. rst-class:: classref-reftable-group
  48. Properties
  49. ----------
  50. .. table::
  51. :widths: auto
  52. +-------------------------+---------------------------------------------------------+
  53. | :ref:`bool<class_bool>` | :ref:`big_endian<class_FileAccess_property_big_endian>` |
  54. +-------------------------+---------------------------------------------------------+
  55. .. rst-class:: classref-reftable-group
  56. Methods
  57. -------
  58. .. table::
  59. :widths: auto
  60. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  61. | void | :ref:`close<class_FileAccess_method_close>` **(** **)** |
  62. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  63. | :ref:`bool<class_bool>` | :ref:`eof_reached<class_FileAccess_method_eof_reached>` **(** **)** |const| |
  64. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  65. | :ref:`bool<class_bool>` | :ref:`file_exists<class_FileAccess_method_file_exists>` **(** :ref:`String<class_String>` path **)** |static| |
  66. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  67. | void | :ref:`flush<class_FileAccess_method_flush>` **(** **)** |
  68. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  69. | :ref:`int<class_int>` | :ref:`get_8<class_FileAccess_method_get_8>` **(** **)** |const| |
  70. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  71. | :ref:`int<class_int>` | :ref:`get_16<class_FileAccess_method_get_16>` **(** **)** |const| |
  72. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  73. | :ref:`int<class_int>` | :ref:`get_32<class_FileAccess_method_get_32>` **(** **)** |const| |
  74. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  75. | :ref:`int<class_int>` | :ref:`get_64<class_FileAccess_method_get_64>` **(** **)** |const| |
  76. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  77. | :ref:`String<class_String>` | :ref:`get_as_text<class_FileAccess_method_get_as_text>` **(** :ref:`bool<class_bool>` skip_cr=false **)** |const| |
  78. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  79. | :ref:`PackedByteArray<class_PackedByteArray>` | :ref:`get_buffer<class_FileAccess_method_get_buffer>` **(** :ref:`int<class_int>` length **)** |const| |
  80. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  81. | :ref:`PackedStringArray<class_PackedStringArray>` | :ref:`get_csv_line<class_FileAccess_method_get_csv_line>` **(** :ref:`String<class_String>` delim="," **)** |const| |
  82. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  83. | :ref:`float<class_float>` | :ref:`get_double<class_FileAccess_method_get_double>` **(** **)** |const| |
  84. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  85. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`get_error<class_FileAccess_method_get_error>` **(** **)** |const| |
  86. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  87. | :ref:`PackedByteArray<class_PackedByteArray>` | :ref:`get_file_as_bytes<class_FileAccess_method_get_file_as_bytes>` **(** :ref:`String<class_String>` path **)** |static| |
  88. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  89. | :ref:`String<class_String>` | :ref:`get_file_as_string<class_FileAccess_method_get_file_as_string>` **(** :ref:`String<class_String>` path **)** |static| |
  90. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  91. | :ref:`float<class_float>` | :ref:`get_float<class_FileAccess_method_get_float>` **(** **)** |const| |
  92. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  93. | :ref:`bool<class_bool>` | :ref:`get_hidden_attribute<class_FileAccess_method_get_hidden_attribute>` **(** :ref:`String<class_String>` file **)** |static| |
  94. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  95. | :ref:`int<class_int>` | :ref:`get_length<class_FileAccess_method_get_length>` **(** **)** |const| |
  96. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  97. | :ref:`String<class_String>` | :ref:`get_line<class_FileAccess_method_get_line>` **(** **)** |const| |
  98. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  99. | :ref:`String<class_String>` | :ref:`get_md5<class_FileAccess_method_get_md5>` **(** :ref:`String<class_String>` path **)** |static| |
  100. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  101. | :ref:`int<class_int>` | :ref:`get_modified_time<class_FileAccess_method_get_modified_time>` **(** :ref:`String<class_String>` file **)** |static| |
  102. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  103. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`get_open_error<class_FileAccess_method_get_open_error>` **(** **)** |static| |
  104. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  105. | :ref:`String<class_String>` | :ref:`get_pascal_string<class_FileAccess_method_get_pascal_string>` **(** **)** |
  106. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  107. | :ref:`String<class_String>` | :ref:`get_path<class_FileAccess_method_get_path>` **(** **)** |const| |
  108. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  109. | :ref:`String<class_String>` | :ref:`get_path_absolute<class_FileAccess_method_get_path_absolute>` **(** **)** |const| |
  110. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  111. | :ref:`int<class_int>` | :ref:`get_position<class_FileAccess_method_get_position>` **(** **)** |const| |
  112. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  113. | :ref:`bool<class_bool>` | :ref:`get_read_only_attribute<class_FileAccess_method_get_read_only_attribute>` **(** :ref:`String<class_String>` file **)** |static| |
  114. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  115. | :ref:`float<class_float>` | :ref:`get_real<class_FileAccess_method_get_real>` **(** **)** |const| |
  116. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  117. | :ref:`String<class_String>` | :ref:`get_sha256<class_FileAccess_method_get_sha256>` **(** :ref:`String<class_String>` path **)** |static| |
  118. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  119. | |bitfield|\<:ref:`UnixPermissionFlags<enum_FileAccess_UnixPermissionFlags>`\> | :ref:`get_unix_permissions<class_FileAccess_method_get_unix_permissions>` **(** :ref:`String<class_String>` file **)** |static| |
  120. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  121. | :ref:`Variant<class_Variant>` | :ref:`get_var<class_FileAccess_method_get_var>` **(** :ref:`bool<class_bool>` allow_objects=false **)** |const| |
  122. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  123. | :ref:`bool<class_bool>` | :ref:`is_open<class_FileAccess_method_is_open>` **(** **)** |const| |
  124. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  125. | :ref:`FileAccess<class_FileAccess>` | :ref:`open<class_FileAccess_method_open>` **(** :ref:`String<class_String>` path, :ref:`ModeFlags<enum_FileAccess_ModeFlags>` flags **)** |static| |
  126. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  127. | :ref:`FileAccess<class_FileAccess>` | :ref:`open_compressed<class_FileAccess_method_open_compressed>` **(** :ref:`String<class_String>` path, :ref:`ModeFlags<enum_FileAccess_ModeFlags>` mode_flags, :ref:`CompressionMode<enum_FileAccess_CompressionMode>` compression_mode=0 **)** |static| |
  128. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  129. | :ref:`FileAccess<class_FileAccess>` | :ref:`open_encrypted<class_FileAccess_method_open_encrypted>` **(** :ref:`String<class_String>` path, :ref:`ModeFlags<enum_FileAccess_ModeFlags>` mode_flags, :ref:`PackedByteArray<class_PackedByteArray>` key **)** |static| |
  130. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  131. | :ref:`FileAccess<class_FileAccess>` | :ref:`open_encrypted_with_pass<class_FileAccess_method_open_encrypted_with_pass>` **(** :ref:`String<class_String>` path, :ref:`ModeFlags<enum_FileAccess_ModeFlags>` mode_flags, :ref:`String<class_String>` pass **)** |static| |
  132. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  133. | void | :ref:`seek<class_FileAccess_method_seek>` **(** :ref:`int<class_int>` position **)** |
  134. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  135. | void | :ref:`seek_end<class_FileAccess_method_seek_end>` **(** :ref:`int<class_int>` position=0 **)** |
  136. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  137. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`set_hidden_attribute<class_FileAccess_method_set_hidden_attribute>` **(** :ref:`String<class_String>` file, :ref:`bool<class_bool>` hidden **)** |static| |
  138. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  139. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`set_read_only_attribute<class_FileAccess_method_set_read_only_attribute>` **(** :ref:`String<class_String>` file, :ref:`bool<class_bool>` ro **)** |static| |
  140. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  141. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`set_unix_permissions<class_FileAccess_method_set_unix_permissions>` **(** :ref:`String<class_String>` file, |bitfield|\<:ref:`UnixPermissionFlags<enum_FileAccess_UnixPermissionFlags>`\> permissions **)** |static| |
  142. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  143. | void | :ref:`store_8<class_FileAccess_method_store_8>` **(** :ref:`int<class_int>` value **)** |
  144. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  145. | void | :ref:`store_16<class_FileAccess_method_store_16>` **(** :ref:`int<class_int>` value **)** |
  146. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  147. | void | :ref:`store_32<class_FileAccess_method_store_32>` **(** :ref:`int<class_int>` value **)** |
  148. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  149. | void | :ref:`store_64<class_FileAccess_method_store_64>` **(** :ref:`int<class_int>` value **)** |
  150. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  151. | void | :ref:`store_buffer<class_FileAccess_method_store_buffer>` **(** :ref:`PackedByteArray<class_PackedByteArray>` buffer **)** |
  152. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  153. | void | :ref:`store_csv_line<class_FileAccess_method_store_csv_line>` **(** :ref:`PackedStringArray<class_PackedStringArray>` values, :ref:`String<class_String>` delim="," **)** |
  154. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  155. | void | :ref:`store_double<class_FileAccess_method_store_double>` **(** :ref:`float<class_float>` value **)** |
  156. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  157. | void | :ref:`store_float<class_FileAccess_method_store_float>` **(** :ref:`float<class_float>` value **)** |
  158. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  159. | void | :ref:`store_line<class_FileAccess_method_store_line>` **(** :ref:`String<class_String>` line **)** |
  160. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  161. | void | :ref:`store_pascal_string<class_FileAccess_method_store_pascal_string>` **(** :ref:`String<class_String>` string **)** |
  162. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  163. | void | :ref:`store_real<class_FileAccess_method_store_real>` **(** :ref:`float<class_float>` value **)** |
  164. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  165. | void | :ref:`store_string<class_FileAccess_method_store_string>` **(** :ref:`String<class_String>` string **)** |
  166. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  167. | void | :ref:`store_var<class_FileAccess_method_store_var>` **(** :ref:`Variant<class_Variant>` value, :ref:`bool<class_bool>` full_objects=false **)** |
  168. +-------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  169. .. rst-class:: classref-section-separator
  170. ----
  171. .. rst-class:: classref-descriptions-group
  172. Enumerations
  173. ------------
  174. .. _enum_FileAccess_ModeFlags:
  175. .. rst-class:: classref-enumeration
  176. enum **ModeFlags**:
  177. .. _class_FileAccess_constant_READ:
  178. .. rst-class:: classref-enumeration-constant
  179. :ref:`ModeFlags<enum_FileAccess_ModeFlags>` **READ** = ``1``
  180. Opens the file for read operations. The cursor is positioned at the beginning of the file.
  181. .. _class_FileAccess_constant_WRITE:
  182. .. rst-class:: classref-enumeration-constant
  183. :ref:`ModeFlags<enum_FileAccess_ModeFlags>` **WRITE** = ``2``
  184. Opens the file for write operations. The file is created if it does not exist, and truncated if it does.
  185. \ **Note:** When creating a file it must be in an already existing directory. To recursively create directories for a file path, see :ref:`DirAccess.make_dir_recursive<class_DirAccess_method_make_dir_recursive>`).
  186. .. _class_FileAccess_constant_READ_WRITE:
  187. .. rst-class:: classref-enumeration-constant
  188. :ref:`ModeFlags<enum_FileAccess_ModeFlags>` **READ_WRITE** = ``3``
  189. Opens the file for read and write operations. Does not truncate the file. The cursor is positioned at the beginning of the file.
  190. .. _class_FileAccess_constant_WRITE_READ:
  191. .. rst-class:: classref-enumeration-constant
  192. :ref:`ModeFlags<enum_FileAccess_ModeFlags>` **WRITE_READ** = ``7``
  193. 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.
  194. \ **Note:** When creating a file it must be in an already existing directory. To recursively create directories for a file path, see :ref:`DirAccess.make_dir_recursive<class_DirAccess_method_make_dir_recursive>`).
  195. .. rst-class:: classref-item-separator
  196. ----
  197. .. _enum_FileAccess_CompressionMode:
  198. .. rst-class:: classref-enumeration
  199. enum **CompressionMode**:
  200. .. _class_FileAccess_constant_COMPRESSION_FASTLZ:
  201. .. rst-class:: classref-enumeration-constant
  202. :ref:`CompressionMode<enum_FileAccess_CompressionMode>` **COMPRESSION_FASTLZ** = ``0``
  203. Uses the `FastLZ <https://fastlz.org/>`__ compression method.
  204. .. _class_FileAccess_constant_COMPRESSION_DEFLATE:
  205. .. rst-class:: classref-enumeration-constant
  206. :ref:`CompressionMode<enum_FileAccess_CompressionMode>` **COMPRESSION_DEFLATE** = ``1``
  207. Uses the `DEFLATE <https://en.wikipedia.org/wiki/DEFLATE>`__ compression method.
  208. .. _class_FileAccess_constant_COMPRESSION_ZSTD:
  209. .. rst-class:: classref-enumeration-constant
  210. :ref:`CompressionMode<enum_FileAccess_CompressionMode>` **COMPRESSION_ZSTD** = ``2``
  211. Uses the `Zstandard <https://facebook.github.io/zstd/>`__ compression method.
  212. .. _class_FileAccess_constant_COMPRESSION_GZIP:
  213. .. rst-class:: classref-enumeration-constant
  214. :ref:`CompressionMode<enum_FileAccess_CompressionMode>` **COMPRESSION_GZIP** = ``3``
  215. Uses the `gzip <https://www.gzip.org/>`__ compression method.
  216. .. _class_FileAccess_constant_COMPRESSION_BROTLI:
  217. .. rst-class:: classref-enumeration-constant
  218. :ref:`CompressionMode<enum_FileAccess_CompressionMode>` **COMPRESSION_BROTLI** = ``4``
  219. Uses the `brotli <https://github.com/google/brotli>`__ compression method (only decompression is supported).
  220. .. rst-class:: classref-item-separator
  221. ----
  222. .. _enum_FileAccess_UnixPermissionFlags:
  223. .. rst-class:: classref-enumeration
  224. flags **UnixPermissionFlags**:
  225. .. _class_FileAccess_constant_UNIX_READ_OWNER:
  226. .. rst-class:: classref-enumeration-constant
  227. :ref:`UnixPermissionFlags<enum_FileAccess_UnixPermissionFlags>` **UNIX_READ_OWNER** = ``256``
  228. Read for owner bit.
  229. .. _class_FileAccess_constant_UNIX_WRITE_OWNER:
  230. .. rst-class:: classref-enumeration-constant
  231. :ref:`UnixPermissionFlags<enum_FileAccess_UnixPermissionFlags>` **UNIX_WRITE_OWNER** = ``128``
  232. Write for owner bit.
  233. .. _class_FileAccess_constant_UNIX_EXECUTE_OWNER:
  234. .. rst-class:: classref-enumeration-constant
  235. :ref:`UnixPermissionFlags<enum_FileAccess_UnixPermissionFlags>` **UNIX_EXECUTE_OWNER** = ``64``
  236. Execute for owner bit.
  237. .. _class_FileAccess_constant_UNIX_READ_GROUP:
  238. .. rst-class:: classref-enumeration-constant
  239. :ref:`UnixPermissionFlags<enum_FileAccess_UnixPermissionFlags>` **UNIX_READ_GROUP** = ``32``
  240. Read for group bit.
  241. .. _class_FileAccess_constant_UNIX_WRITE_GROUP:
  242. .. rst-class:: classref-enumeration-constant
  243. :ref:`UnixPermissionFlags<enum_FileAccess_UnixPermissionFlags>` **UNIX_WRITE_GROUP** = ``16``
  244. Write for group bit.
  245. .. _class_FileAccess_constant_UNIX_EXECUTE_GROUP:
  246. .. rst-class:: classref-enumeration-constant
  247. :ref:`UnixPermissionFlags<enum_FileAccess_UnixPermissionFlags>` **UNIX_EXECUTE_GROUP** = ``8``
  248. Execute for group bit.
  249. .. _class_FileAccess_constant_UNIX_READ_OTHER:
  250. .. rst-class:: classref-enumeration-constant
  251. :ref:`UnixPermissionFlags<enum_FileAccess_UnixPermissionFlags>` **UNIX_READ_OTHER** = ``4``
  252. Read for other bit.
  253. .. _class_FileAccess_constant_UNIX_WRITE_OTHER:
  254. .. rst-class:: classref-enumeration-constant
  255. :ref:`UnixPermissionFlags<enum_FileAccess_UnixPermissionFlags>` **UNIX_WRITE_OTHER** = ``2``
  256. Write for other bit.
  257. .. _class_FileAccess_constant_UNIX_EXECUTE_OTHER:
  258. .. rst-class:: classref-enumeration-constant
  259. :ref:`UnixPermissionFlags<enum_FileAccess_UnixPermissionFlags>` **UNIX_EXECUTE_OTHER** = ``1``
  260. Execute for other bit.
  261. .. _class_FileAccess_constant_UNIX_SET_USER_ID:
  262. .. rst-class:: classref-enumeration-constant
  263. :ref:`UnixPermissionFlags<enum_FileAccess_UnixPermissionFlags>` **UNIX_SET_USER_ID** = ``2048``
  264. Set user id on execution bit.
  265. .. _class_FileAccess_constant_UNIX_SET_GROUP_ID:
  266. .. rst-class:: classref-enumeration-constant
  267. :ref:`UnixPermissionFlags<enum_FileAccess_UnixPermissionFlags>` **UNIX_SET_GROUP_ID** = ``1024``
  268. Set group id on execution bit.
  269. .. _class_FileAccess_constant_UNIX_RESTRICTED_DELETE:
  270. .. rst-class:: classref-enumeration-constant
  271. :ref:`UnixPermissionFlags<enum_FileAccess_UnixPermissionFlags>` **UNIX_RESTRICTED_DELETE** = ``512``
  272. Restricted deletion (sticky) bit.
  273. .. rst-class:: classref-section-separator
  274. ----
  275. .. rst-class:: classref-descriptions-group
  276. Property Descriptions
  277. ---------------------
  278. .. _class_FileAccess_property_big_endian:
  279. .. rst-class:: classref-property
  280. :ref:`bool<class_bool>` **big_endian**
  281. .. rst-class:: classref-property-setget
  282. - void **set_big_endian** **(** :ref:`bool<class_bool>` value **)**
  283. - :ref:`bool<class_bool>` **is_big_endian** **(** **)**
  284. If ``true``, the file is read with big-endian `endianness <https://en.wikipedia.org/wiki/Endianness>`__. If ``false``, the file is read with little-endian endianness. If in doubt, leave this to ``false`` as most files are written with little-endian endianness.
  285. \ **Note:** :ref:`big_endian<class_FileAccess_property_big_endian>` is only about the file format, not the CPU type. The CPU endianness doesn't affect the default endianness for files written.
  286. \ **Note:** This is always reset to ``false`` whenever you open the file. Therefore, you must set :ref:`big_endian<class_FileAccess_property_big_endian>` *after* opening the file, not before.
  287. .. rst-class:: classref-section-separator
  288. ----
  289. .. rst-class:: classref-descriptions-group
  290. Method Descriptions
  291. -------------------
  292. .. _class_FileAccess_method_close:
  293. .. rst-class:: classref-method
  294. void **close** **(** **)**
  295. Closes the currently opened file and prevents subsequent read/write operations. Use :ref:`flush<class_FileAccess_method_flush>` to persist the data to disk without closing the file.
  296. \ **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.
  297. .. rst-class:: classref-item-separator
  298. ----
  299. .. _class_FileAccess_method_eof_reached:
  300. .. rst-class:: classref-method
  301. :ref:`bool<class_bool>` **eof_reached** **(** **)** |const|
  302. Returns ``true`` if the file cursor has already read past the end of the file.
  303. \ **Note:** ``eof_reached() == false`` cannot be used to check whether there is more data available. To loop while there is more data available, use:
  304. .. tabs::
  305. .. code-tab:: gdscript
  306. while file.get_position() < file.get_length():
  307. # Read data
  308. .. code-tab:: csharp
  309. while (file.GetPosition() < file.GetLength())
  310. {
  311. // Read data
  312. }
  313. .. rst-class:: classref-item-separator
  314. ----
  315. .. _class_FileAccess_method_file_exists:
  316. .. rst-class:: classref-method
  317. :ref:`bool<class_bool>` **file_exists** **(** :ref:`String<class_String>` path **)** |static|
  318. Returns ``true`` if the file exists in the given path.
  319. \ **Note:** 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 :ref:`ResourceLoader.exists<class_ResourceLoader_method_exists>` for an alternative approach that takes resource remapping into account.
  320. For a non-static, relative equivalent, use :ref:`DirAccess.file_exists<class_DirAccess_method_file_exists>`.
  321. .. rst-class:: classref-item-separator
  322. ----
  323. .. _class_FileAccess_method_flush:
  324. .. rst-class:: classref-method
  325. void **flush** **(** **)**
  326. Writes the file's buffer to disk. Flushing is automatically performed when the file is closed. This means you don't need to call :ref:`flush<class_FileAccess_method_flush>` manually before closing a file. Still, calling :ref:`flush<class_FileAccess_method_flush>` can be used to ensure the data is safe even if the project crashes instead of being closed gracefully.
  327. \ **Note:** Only call :ref:`flush<class_FileAccess_method_flush>` when you actually need it. Otherwise, it will decrease performance due to constant disk writes.
  328. .. rst-class:: classref-item-separator
  329. ----
  330. .. _class_FileAccess_method_get_8:
  331. .. rst-class:: classref-method
  332. :ref:`int<class_int>` **get_8** **(** **)** |const|
  333. Returns the next 8 bits from the file as an integer. See :ref:`store_8<class_FileAccess_method_store_8>` for details on what values can be stored and retrieved this way.
  334. .. rst-class:: classref-item-separator
  335. ----
  336. .. _class_FileAccess_method_get_16:
  337. .. rst-class:: classref-method
  338. :ref:`int<class_int>` **get_16** **(** **)** |const|
  339. Returns the next 16 bits from the file as an integer. See :ref:`store_16<class_FileAccess_method_store_16>` for details on what values can be stored and retrieved this way.
  340. .. rst-class:: classref-item-separator
  341. ----
  342. .. _class_FileAccess_method_get_32:
  343. .. rst-class:: classref-method
  344. :ref:`int<class_int>` **get_32** **(** **)** |const|
  345. Returns the next 32 bits from the file as an integer. See :ref:`store_32<class_FileAccess_method_store_32>` for details on what values can be stored and retrieved this way.
  346. .. rst-class:: classref-item-separator
  347. ----
  348. .. _class_FileAccess_method_get_64:
  349. .. rst-class:: classref-method
  350. :ref:`int<class_int>` **get_64** **(** **)** |const|
  351. Returns the next 64 bits from the file as an integer. See :ref:`store_64<class_FileAccess_method_store_64>` for details on what values can be stored and retrieved this way.
  352. .. rst-class:: classref-item-separator
  353. ----
  354. .. _class_FileAccess_method_get_as_text:
  355. .. rst-class:: classref-method
  356. :ref:`String<class_String>` **get_as_text** **(** :ref:`bool<class_bool>` skip_cr=false **)** |const|
  357. Returns the whole file as a :ref:`String<class_String>`. Text is interpreted as being UTF-8 encoded.
  358. If ``skip_cr`` is ``true``, carriage return characters (``\r``, CR) will be ignored when parsing the UTF-8, so that only line feed characters (``\n``, LF) represent a new line (Unix convention).
  359. .. rst-class:: classref-item-separator
  360. ----
  361. .. _class_FileAccess_method_get_buffer:
  362. .. rst-class:: classref-method
  363. :ref:`PackedByteArray<class_PackedByteArray>` **get_buffer** **(** :ref:`int<class_int>` length **)** |const|
  364. Returns next ``length`` bytes of the file as a :ref:`PackedByteArray<class_PackedByteArray>`.
  365. .. rst-class:: classref-item-separator
  366. ----
  367. .. _class_FileAccess_method_get_csv_line:
  368. .. rst-class:: classref-method
  369. :ref:`PackedStringArray<class_PackedStringArray>` **get_csv_line** **(** :ref:`String<class_String>` delim="," **)** |const|
  370. Returns the next value of the file in CSV (Comma-Separated Values) format. You can pass a different delimiter ``delim`` to use other than the default ``","`` (comma). This delimiter must be one-character long, and cannot be a double quotation mark.
  371. 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.
  372. For example, the following CSV lines are valid and will be properly parsed as two strings each:
  373. ::
  374. Alice,"Hello, Bob!"
  375. Bob,Alice! What a surprise!
  376. Alice,"I thought you'd reply with ""Hello, world""."
  377. Note how the second line can omit the enclosing quotes as it does not include the delimiter. However it *could* very well use quotes, it was only written without for demonstration purposes. The third line must use ``""`` for each quotation mark that needs to be interpreted as such instead of the end of a text value.
  378. .. rst-class:: classref-item-separator
  379. ----
  380. .. _class_FileAccess_method_get_double:
  381. .. rst-class:: classref-method
  382. :ref:`float<class_float>` **get_double** **(** **)** |const|
  383. Returns the next 64 bits from the file as a floating-point number.
  384. .. rst-class:: classref-item-separator
  385. ----
  386. .. _class_FileAccess_method_get_error:
  387. .. rst-class:: classref-method
  388. :ref:`Error<enum_@GlobalScope_Error>` **get_error** **(** **)** |const|
  389. Returns the last error that happened when trying to perform operations. Compare with the ``ERR_FILE_*`` constants from :ref:`Error<enum_@GlobalScope_Error>`.
  390. .. rst-class:: classref-item-separator
  391. ----
  392. .. _class_FileAccess_method_get_file_as_bytes:
  393. .. rst-class:: classref-method
  394. :ref:`PackedByteArray<class_PackedByteArray>` **get_file_as_bytes** **(** :ref:`String<class_String>` path **)** |static|
  395. Returns the whole ``path`` file contents as a :ref:`PackedByteArray<class_PackedByteArray>` without any decoding.
  396. Returns an empty :ref:`PackedByteArray<class_PackedByteArray>` if an error occurred while opening the file. You can use :ref:`get_open_error<class_FileAccess_method_get_open_error>` to check the error that occurred.
  397. .. rst-class:: classref-item-separator
  398. ----
  399. .. _class_FileAccess_method_get_file_as_string:
  400. .. rst-class:: classref-method
  401. :ref:`String<class_String>` **get_file_as_string** **(** :ref:`String<class_String>` path **)** |static|
  402. Returns the whole ``path`` file contents as a :ref:`String<class_String>`. Text is interpreted as being UTF-8 encoded.
  403. Returns an empty :ref:`String<class_String>` if an error occurred while opening the file. You can use :ref:`get_open_error<class_FileAccess_method_get_open_error>` to check the error that occurred.
  404. .. rst-class:: classref-item-separator
  405. ----
  406. .. _class_FileAccess_method_get_float:
  407. .. rst-class:: classref-method
  408. :ref:`float<class_float>` **get_float** **(** **)** |const|
  409. Returns the next 32 bits from the file as a floating-point number.
  410. .. rst-class:: classref-item-separator
  411. ----
  412. .. _class_FileAccess_method_get_hidden_attribute:
  413. .. rst-class:: classref-method
  414. :ref:`bool<class_bool>` **get_hidden_attribute** **(** :ref:`String<class_String>` file **)** |static|
  415. Returns ``true``, if file ``hidden`` attribute is set.
  416. \ **Note:** This method is implemented on iOS, BSD, macOS, and Windows.
  417. .. rst-class:: classref-item-separator
  418. ----
  419. .. _class_FileAccess_method_get_length:
  420. .. rst-class:: classref-method
  421. :ref:`int<class_int>` **get_length** **(** **)** |const|
  422. Returns the size of the file in bytes.
  423. .. rst-class:: classref-item-separator
  424. ----
  425. .. _class_FileAccess_method_get_line:
  426. .. rst-class:: classref-method
  427. :ref:`String<class_String>` **get_line** **(** **)** |const|
  428. Returns the next line of the file as a :ref:`String<class_String>`.
  429. Text is interpreted as being UTF-8 encoded.
  430. .. rst-class:: classref-item-separator
  431. ----
  432. .. _class_FileAccess_method_get_md5:
  433. .. rst-class:: classref-method
  434. :ref:`String<class_String>` **get_md5** **(** :ref:`String<class_String>` path **)** |static|
  435. Returns an MD5 String representing the file at the given path or an empty :ref:`String<class_String>` on failure.
  436. .. rst-class:: classref-item-separator
  437. ----
  438. .. _class_FileAccess_method_get_modified_time:
  439. .. rst-class:: classref-method
  440. :ref:`int<class_int>` **get_modified_time** **(** :ref:`String<class_String>` file **)** |static|
  441. Returns the last time the ``file`` was modified in Unix timestamp format, or ``0`` on error. This Unix timestamp can be converted to another format using the :ref:`Time<class_Time>` singleton.
  442. .. rst-class:: classref-item-separator
  443. ----
  444. .. _class_FileAccess_method_get_open_error:
  445. .. rst-class:: classref-method
  446. :ref:`Error<enum_@GlobalScope_Error>` **get_open_error** **(** **)** |static|
  447. Returns the result of the last :ref:`open<class_FileAccess_method_open>` call in the current thread.
  448. .. rst-class:: classref-item-separator
  449. ----
  450. .. _class_FileAccess_method_get_pascal_string:
  451. .. rst-class:: classref-method
  452. :ref:`String<class_String>` **get_pascal_string** **(** **)**
  453. Returns a :ref:`String<class_String>` saved in Pascal format from the file.
  454. Text is interpreted as being UTF-8 encoded.
  455. .. rst-class:: classref-item-separator
  456. ----
  457. .. _class_FileAccess_method_get_path:
  458. .. rst-class:: classref-method
  459. :ref:`String<class_String>` **get_path** **(** **)** |const|
  460. Returns the path as a :ref:`String<class_String>` for the current open file.
  461. .. rst-class:: classref-item-separator
  462. ----
  463. .. _class_FileAccess_method_get_path_absolute:
  464. .. rst-class:: classref-method
  465. :ref:`String<class_String>` **get_path_absolute** **(** **)** |const|
  466. Returns the absolute path as a :ref:`String<class_String>` for the current open file.
  467. .. rst-class:: classref-item-separator
  468. ----
  469. .. _class_FileAccess_method_get_position:
  470. .. rst-class:: classref-method
  471. :ref:`int<class_int>` **get_position** **(** **)** |const|
  472. Returns the file cursor's position.
  473. .. rst-class:: classref-item-separator
  474. ----
  475. .. _class_FileAccess_method_get_read_only_attribute:
  476. .. rst-class:: classref-method
  477. :ref:`bool<class_bool>` **get_read_only_attribute** **(** :ref:`String<class_String>` file **)** |static|
  478. Returns ``true``, if file ``read only`` attribute is set.
  479. \ **Note:** This method is implemented on iOS, BSD, macOS, and Windows.
  480. .. rst-class:: classref-item-separator
  481. ----
  482. .. _class_FileAccess_method_get_real:
  483. .. rst-class:: classref-method
  484. :ref:`float<class_float>` **get_real** **(** **)** |const|
  485. Returns the next bits from the file as a floating-point number.
  486. .. rst-class:: classref-item-separator
  487. ----
  488. .. _class_FileAccess_method_get_sha256:
  489. .. rst-class:: classref-method
  490. :ref:`String<class_String>` **get_sha256** **(** :ref:`String<class_String>` path **)** |static|
  491. Returns a SHA-256 :ref:`String<class_String>` representing the file at the given path or an empty :ref:`String<class_String>` on failure.
  492. .. rst-class:: classref-item-separator
  493. ----
  494. .. _class_FileAccess_method_get_unix_permissions:
  495. .. rst-class:: classref-method
  496. |bitfield|\<:ref:`UnixPermissionFlags<enum_FileAccess_UnixPermissionFlags>`\> **get_unix_permissions** **(** :ref:`String<class_String>` file **)** |static|
  497. Returns file UNIX permissions.
  498. \ **Note:** This method is implemented on iOS, Linux/BSD, and macOS.
  499. .. rst-class:: classref-item-separator
  500. ----
  501. .. _class_FileAccess_method_get_var:
  502. .. rst-class:: classref-method
  503. :ref:`Variant<class_Variant>` **get_var** **(** :ref:`bool<class_bool>` allow_objects=false **)** |const|
  504. Returns the next :ref:`Variant<class_Variant>` value from the file. If ``allow_objects`` is ``true``, decoding objects is allowed.
  505. Internally, this uses the same decoding mechanism as the :ref:`@GlobalScope.bytes_to_var<class_@GlobalScope_method_bytes_to_var>` method.
  506. \ **Warning:** 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.
  507. .. rst-class:: classref-item-separator
  508. ----
  509. .. _class_FileAccess_method_is_open:
  510. .. rst-class:: classref-method
  511. :ref:`bool<class_bool>` **is_open** **(** **)** |const|
  512. Returns ``true`` if the file is currently opened.
  513. .. rst-class:: classref-item-separator
  514. ----
  515. .. _class_FileAccess_method_open:
  516. .. rst-class:: classref-method
  517. :ref:`FileAccess<class_FileAccess>` **open** **(** :ref:`String<class_String>` path, :ref:`ModeFlags<enum_FileAccess_ModeFlags>` flags **)** |static|
  518. Creates a new **FileAccess** object and opens the file for writing or reading, depending on the flags.
  519. Returns ``null`` if opening the file failed. You can use :ref:`get_open_error<class_FileAccess_method_get_open_error>` to check the error that occurred.
  520. .. rst-class:: classref-item-separator
  521. ----
  522. .. _class_FileAccess_method_open_compressed:
  523. .. rst-class:: classref-method
  524. :ref:`FileAccess<class_FileAccess>` **open_compressed** **(** :ref:`String<class_String>` path, :ref:`ModeFlags<enum_FileAccess_ModeFlags>` mode_flags, :ref:`CompressionMode<enum_FileAccess_CompressionMode>` compression_mode=0 **)** |static|
  525. Creates a new **FileAccess** object and opens a compressed file for reading or writing.
  526. \ **Note:** :ref:`open_compressed<class_FileAccess_method_open_compressed>` can only read files that were saved by Godot, not third-party compression formats. See `GitHub issue #28999 <https://github.com/godotengine/godot/issues/28999>`__ for a workaround.
  527. Returns ``null`` if opening the file failed. You can use :ref:`get_open_error<class_FileAccess_method_get_open_error>` to check the error that occurred.
  528. .. rst-class:: classref-item-separator
  529. ----
  530. .. _class_FileAccess_method_open_encrypted:
  531. .. rst-class:: classref-method
  532. :ref:`FileAccess<class_FileAccess>` **open_encrypted** **(** :ref:`String<class_String>` path, :ref:`ModeFlags<enum_FileAccess_ModeFlags>` mode_flags, :ref:`PackedByteArray<class_PackedByteArray>` key **)** |static|
  533. 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.
  534. \ **Note:** The provided key must be 32 bytes long.
  535. Returns ``null`` if opening the file failed. You can use :ref:`get_open_error<class_FileAccess_method_get_open_error>` to check the error that occurred.
  536. .. rst-class:: classref-item-separator
  537. ----
  538. .. _class_FileAccess_method_open_encrypted_with_pass:
  539. .. rst-class:: classref-method
  540. :ref:`FileAccess<class_FileAccess>` **open_encrypted_with_pass** **(** :ref:`String<class_String>` path, :ref:`ModeFlags<enum_FileAccess_ModeFlags>` mode_flags, :ref:`String<class_String>` pass **)** |static|
  541. 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.
  542. Returns ``null`` if opening the file failed. You can use :ref:`get_open_error<class_FileAccess_method_get_open_error>` to check the error that occurred.
  543. .. rst-class:: classref-item-separator
  544. ----
  545. .. _class_FileAccess_method_seek:
  546. .. rst-class:: classref-method
  547. void **seek** **(** :ref:`int<class_int>` position **)**
  548. Changes the file reading/writing cursor to the specified position (in bytes from the beginning of the file).
  549. .. rst-class:: classref-item-separator
  550. ----
  551. .. _class_FileAccess_method_seek_end:
  552. .. rst-class:: classref-method
  553. void **seek_end** **(** :ref:`int<class_int>` position=0 **)**
  554. Changes the file reading/writing cursor to the specified position (in bytes from the end of the file).
  555. \ **Note:** This is an offset, so you should use negative numbers or the cursor will be at the end of the file.
  556. .. rst-class:: classref-item-separator
  557. ----
  558. .. _class_FileAccess_method_set_hidden_attribute:
  559. .. rst-class:: classref-method
  560. :ref:`Error<enum_@GlobalScope_Error>` **set_hidden_attribute** **(** :ref:`String<class_String>` file, :ref:`bool<class_bool>` hidden **)** |static|
  561. Sets file **hidden** attribute.
  562. \ **Note:** This method is implemented on iOS, BSD, macOS, and Windows.
  563. .. rst-class:: classref-item-separator
  564. ----
  565. .. _class_FileAccess_method_set_read_only_attribute:
  566. .. rst-class:: classref-method
  567. :ref:`Error<enum_@GlobalScope_Error>` **set_read_only_attribute** **(** :ref:`String<class_String>` file, :ref:`bool<class_bool>` ro **)** |static|
  568. Sets file **read only** attribute.
  569. \ **Note:** This method is implemented on iOS, BSD, macOS, and Windows.
  570. .. rst-class:: classref-item-separator
  571. ----
  572. .. _class_FileAccess_method_set_unix_permissions:
  573. .. rst-class:: classref-method
  574. :ref:`Error<enum_@GlobalScope_Error>` **set_unix_permissions** **(** :ref:`String<class_String>` file, |bitfield|\<:ref:`UnixPermissionFlags<enum_FileAccess_UnixPermissionFlags>`\> permissions **)** |static|
  575. Sets file UNIX permissions.
  576. \ **Note:** This method is implemented on iOS, Linux/BSD, and macOS.
  577. .. rst-class:: classref-item-separator
  578. ----
  579. .. _class_FileAccess_method_store_8:
  580. .. rst-class:: classref-method
  581. void **store_8** **(** :ref:`int<class_int>` value **)**
  582. Stores an integer as 8 bits in the file.
  583. \ **Note:** The ``value`` should lie in the interval ``[0, 255]``. Any other value will overflow and wrap around.
  584. To store a signed integer, use :ref:`store_64<class_FileAccess_method_store_64>`, or convert it manually (see :ref:`store_16<class_FileAccess_method_store_16>` for an example).
  585. .. rst-class:: classref-item-separator
  586. ----
  587. .. _class_FileAccess_method_store_16:
  588. .. rst-class:: classref-method
  589. void **store_16** **(** :ref:`int<class_int>` value **)**
  590. Stores an integer as 16 bits in the file.
  591. \ **Note:** The ``value`` should lie in the interval ``[0, 2^16 - 1]``. Any other value will overflow and wrap around.
  592. To store a signed integer, use :ref:`store_64<class_FileAccess_method_store_64>` or store a signed integer from the interval ``[-2^15, 2^15 - 1]`` (i.e. keeping one bit for the signedness) and compute its sign manually when reading. For example:
  593. .. tabs::
  594. .. code-tab:: gdscript
  595. const MAX_15B = 1 << 15
  596. const MAX_16B = 1 << 16
  597. func unsigned16_to_signed(unsigned):
  598. return (unsigned + MAX_15B) % MAX_16B - MAX_15B
  599. func _ready():
  600. var f = FileAccess.open("user://file.dat", FileAccess.WRITE_READ)
  601. f.store_16(-42) # This wraps around and stores 65494 (2^16 - 42).
  602. f.store_16(121) # In bounds, will store 121.
  603. f.seek(0) # Go back to start to read the stored value.
  604. var read1 = f.get_16() # 65494
  605. var read2 = f.get_16() # 121
  606. var converted1 = unsigned16_to_signed(read1) # -42
  607. var converted2 = unsigned16_to_signed(read2) # 121
  608. .. code-tab:: csharp
  609. public override void _Ready()
  610. {
  611. using var f = FileAccess.Open("user://file.dat", FileAccess.ModeFlags.WriteRead);
  612. f.Store16(unchecked((ushort)-42)); // This wraps around and stores 65494 (2^16 - 42).
  613. f.Store16(121); // In bounds, will store 121.
  614. f.Seek(0); // Go back to start to read the stored value.
  615. ushort read1 = f.Get16(); // 65494
  616. ushort read2 = f.Get16(); // 121
  617. short converted1 = (short)read1; // -42
  618. short converted2 = (short)read2; // 121
  619. }
  620. .. rst-class:: classref-item-separator
  621. ----
  622. .. _class_FileAccess_method_store_32:
  623. .. rst-class:: classref-method
  624. void **store_32** **(** :ref:`int<class_int>` value **)**
  625. Stores an integer as 32 bits in the file.
  626. \ **Note:** The ``value`` should lie in the interval ``[0, 2^32 - 1]``. Any other value will overflow and wrap around.
  627. To store a signed integer, use :ref:`store_64<class_FileAccess_method_store_64>`, or convert it manually (see :ref:`store_16<class_FileAccess_method_store_16>` for an example).
  628. .. rst-class:: classref-item-separator
  629. ----
  630. .. _class_FileAccess_method_store_64:
  631. .. rst-class:: classref-method
  632. void **store_64** **(** :ref:`int<class_int>` value **)**
  633. Stores an integer as 64 bits in the file.
  634. \ **Note:** The ``value`` must lie in the interval ``[-2^63, 2^63 - 1]`` (i.e. be a valid :ref:`int<class_int>` value).
  635. .. rst-class:: classref-item-separator
  636. ----
  637. .. _class_FileAccess_method_store_buffer:
  638. .. rst-class:: classref-method
  639. void **store_buffer** **(** :ref:`PackedByteArray<class_PackedByteArray>` buffer **)**
  640. Stores the given array of bytes in the file.
  641. .. rst-class:: classref-item-separator
  642. ----
  643. .. _class_FileAccess_method_store_csv_line:
  644. .. rst-class:: classref-method
  645. void **store_csv_line** **(** :ref:`PackedStringArray<class_PackedStringArray>` values, :ref:`String<class_String>` delim="," **)**
  646. Store the given :ref:`PackedStringArray<class_PackedStringArray>` in the file as a line formatted in the CSV (Comma-Separated Values) format. You can pass a different delimiter ``delim`` to use other than the default ``","`` (comma). This delimiter must be one-character long.
  647. Text will be encoded as UTF-8.
  648. .. rst-class:: classref-item-separator
  649. ----
  650. .. _class_FileAccess_method_store_double:
  651. .. rst-class:: classref-method
  652. void **store_double** **(** :ref:`float<class_float>` value **)**
  653. Stores a floating-point number as 64 bits in the file.
  654. .. rst-class:: classref-item-separator
  655. ----
  656. .. _class_FileAccess_method_store_float:
  657. .. rst-class:: classref-method
  658. void **store_float** **(** :ref:`float<class_float>` value **)**
  659. Stores a floating-point number as 32 bits in the file.
  660. .. rst-class:: classref-item-separator
  661. ----
  662. .. _class_FileAccess_method_store_line:
  663. .. rst-class:: classref-method
  664. void **store_line** **(** :ref:`String<class_String>` line **)**
  665. Appends ``line`` to the file followed by a line return character (``\n``), encoding the text as UTF-8.
  666. .. rst-class:: classref-item-separator
  667. ----
  668. .. _class_FileAccess_method_store_pascal_string:
  669. .. rst-class:: classref-method
  670. void **store_pascal_string** **(** :ref:`String<class_String>` string **)**
  671. Stores the given :ref:`String<class_String>` as a line in the file in Pascal format (i.e. also store the length of the string).
  672. Text will be encoded as UTF-8.
  673. .. rst-class:: classref-item-separator
  674. ----
  675. .. _class_FileAccess_method_store_real:
  676. .. rst-class:: classref-method
  677. void **store_real** **(** :ref:`float<class_float>` value **)**
  678. Stores a floating-point number in the file.
  679. .. rst-class:: classref-item-separator
  680. ----
  681. .. _class_FileAccess_method_store_string:
  682. .. rst-class:: classref-method
  683. void **store_string** **(** :ref:`String<class_String>` string **)**
  684. Appends ``string`` to the file without a line return, encoding the text as UTF-8.
  685. \ **Note:** 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 :ref:`store_pascal_string<class_FileAccess_method_store_pascal_string>` instead. For retrieving strings from a text file, you can use ``get_buffer(length).get_string_from_utf8()`` (if you know the length) or :ref:`get_as_text<class_FileAccess_method_get_as_text>`.
  686. .. rst-class:: classref-item-separator
  687. ----
  688. .. _class_FileAccess_method_store_var:
  689. .. rst-class:: classref-method
  690. void **store_var** **(** :ref:`Variant<class_Variant>` value, :ref:`bool<class_bool>` full_objects=false **)**
  691. Stores any Variant value in the file. If ``full_objects`` is ``true``, encoding objects is allowed (and can potentially include code).
  692. Internally, this uses the same encoding mechanism as the :ref:`@GlobalScope.var_to_bytes<class_@GlobalScope_method_var_to_bytes>` method.
  693. \ **Note:** Not all properties are included. Only properties that are configured with the :ref:`@GlobalScope.PROPERTY_USAGE_STORAGE<class_@GlobalScope_constant_PROPERTY_USAGE_STORAGE>` flag set will be serialized. You can add a new usage flag to a property by overriding the :ref:`Object._get_property_list<class_Object_private_method__get_property_list>` method in your class. You can also check how property usage is configured by calling :ref:`Object._get_property_list<class_Object_private_method__get_property_list>`. See :ref:`PropertyUsageFlags<enum_@GlobalScope_PropertyUsageFlags>` for the possible usage flags.
  694. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  695. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  696. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  697. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  698. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  699. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
  700. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`