class_fileaccess.rst 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  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.3/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/4.3/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_to_file(content):
  19. var file = FileAccess.open("user://save_game.dat", FileAccess.WRITE)
  20. file.store_string(content)
  21. func load_from_file():
  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 SaveToFile(string content)
  27. {
  28. using var file = FileAccess.Open("user://save_game.dat", FileAccess.ModeFlags.Write);
  29. file.StoreString(content);
  30. }
  31. public string LoadFromFile()
  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/2755>`__
  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>`\ (\ path\: :ref:`String<class_String>`\ ) |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>`\ (\ skip_cr\: :ref:`bool<class_bool>` = false\ ) |const| |
  78. +-------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  79. | :ref:`PackedByteArray<class_PackedByteArray>` | :ref:`get_buffer<class_FileAccess_method_get_buffer>`\ (\ length\: :ref:`int<class_int>`\ ) |const| |
  80. +-------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  81. | :ref:`PackedStringArray<class_PackedStringArray>` | :ref:`get_csv_line<class_FileAccess_method_get_csv_line>`\ (\ delim\: :ref:`String<class_String>` = ","\ ) |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>`\ (\ path\: :ref:`String<class_String>`\ ) |static| |
  88. +-------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  89. | :ref:`String<class_String>` | :ref:`get_file_as_string<class_FileAccess_method_get_file_as_string>`\ (\ path\: :ref:`String<class_String>`\ ) |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>`\ (\ file\: :ref:`String<class_String>`\ ) |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>`\ (\ path\: :ref:`String<class_String>`\ ) |static| |
  100. +-------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  101. | :ref:`int<class_int>` | :ref:`get_modified_time<class_FileAccess_method_get_modified_time>`\ (\ file\: :ref:`String<class_String>`\ ) |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>`\ (\ file\: :ref:`String<class_String>`\ ) |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>`\ (\ path\: :ref:`String<class_String>`\ ) |static| |
  118. +-------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  119. | |bitfield|\[:ref:`UnixPermissionFlags<enum_FileAccess_UnixPermissionFlags>`\] | :ref:`get_unix_permissions<class_FileAccess_method_get_unix_permissions>`\ (\ file\: :ref:`String<class_String>`\ ) |static| |
  120. +-------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  121. | :ref:`Variant<class_Variant>` | :ref:`get_var<class_FileAccess_method_get_var>`\ (\ allow_objects\: :ref:`bool<class_bool>` = 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>`\ (\ path\: :ref:`String<class_String>`, flags\: :ref:`ModeFlags<enum_FileAccess_ModeFlags>`\ ) |static| |
  126. +-------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  127. | :ref:`FileAccess<class_FileAccess>` | :ref:`open_compressed<class_FileAccess_method_open_compressed>`\ (\ path\: :ref:`String<class_String>`, mode_flags\: :ref:`ModeFlags<enum_FileAccess_ModeFlags>`, compression_mode\: :ref:`CompressionMode<enum_FileAccess_CompressionMode>` = 0\ ) |static| |
  128. +-------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  129. | :ref:`FileAccess<class_FileAccess>` | :ref:`open_encrypted<class_FileAccess_method_open_encrypted>`\ (\ path\: :ref:`String<class_String>`, mode_flags\: :ref:`ModeFlags<enum_FileAccess_ModeFlags>`, key\: :ref:`PackedByteArray<class_PackedByteArray>`\ ) |static| |
  130. +-------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  131. | :ref:`FileAccess<class_FileAccess>` | :ref:`open_encrypted_with_pass<class_FileAccess_method_open_encrypted_with_pass>`\ (\ path\: :ref:`String<class_String>`, mode_flags\: :ref:`ModeFlags<enum_FileAccess_ModeFlags>`, pass\: :ref:`String<class_String>`\ ) |static| |
  132. +-------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  133. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`resize<class_FileAccess_method_resize>`\ (\ length\: :ref:`int<class_int>`\ ) |
  134. +-------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  135. | |void| | :ref:`seek<class_FileAccess_method_seek>`\ (\ position\: :ref:`int<class_int>`\ ) |
  136. +-------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  137. | |void| | :ref:`seek_end<class_FileAccess_method_seek_end>`\ (\ position\: :ref:`int<class_int>` = 0\ ) |
  138. +-------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  139. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`set_hidden_attribute<class_FileAccess_method_set_hidden_attribute>`\ (\ file\: :ref:`String<class_String>`, hidden\: :ref:`bool<class_bool>`\ ) |static| |
  140. +-------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  141. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`set_read_only_attribute<class_FileAccess_method_set_read_only_attribute>`\ (\ file\: :ref:`String<class_String>`, ro\: :ref:`bool<class_bool>`\ ) |static| |
  142. +-------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  143. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`set_unix_permissions<class_FileAccess_method_set_unix_permissions>`\ (\ file\: :ref:`String<class_String>`, permissions\: |bitfield|\[:ref:`UnixPermissionFlags<enum_FileAccess_UnixPermissionFlags>`\]\ ) |static| |
  144. +-------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  145. | |void| | :ref:`store_8<class_FileAccess_method_store_8>`\ (\ value\: :ref:`int<class_int>`\ ) |
  146. +-------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  147. | |void| | :ref:`store_16<class_FileAccess_method_store_16>`\ (\ value\: :ref:`int<class_int>`\ ) |
  148. +-------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  149. | |void| | :ref:`store_32<class_FileAccess_method_store_32>`\ (\ value\: :ref:`int<class_int>`\ ) |
  150. +-------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  151. | |void| | :ref:`store_64<class_FileAccess_method_store_64>`\ (\ value\: :ref:`int<class_int>`\ ) |
  152. +-------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  153. | |void| | :ref:`store_buffer<class_FileAccess_method_store_buffer>`\ (\ buffer\: :ref:`PackedByteArray<class_PackedByteArray>`\ ) |
  154. +-------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  155. | |void| | :ref:`store_csv_line<class_FileAccess_method_store_csv_line>`\ (\ values\: :ref:`PackedStringArray<class_PackedStringArray>`, delim\: :ref:`String<class_String>` = ","\ ) |
  156. +-------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  157. | |void| | :ref:`store_double<class_FileAccess_method_store_double>`\ (\ value\: :ref:`float<class_float>`\ ) |
  158. +-------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  159. | |void| | :ref:`store_float<class_FileAccess_method_store_float>`\ (\ value\: :ref:`float<class_float>`\ ) |
  160. +-------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  161. | |void| | :ref:`store_line<class_FileAccess_method_store_line>`\ (\ line\: :ref:`String<class_String>`\ ) |
  162. +-------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  163. | |void| | :ref:`store_pascal_string<class_FileAccess_method_store_pascal_string>`\ (\ string\: :ref:`String<class_String>`\ ) |
  164. +-------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  165. | |void| | :ref:`store_real<class_FileAccess_method_store_real>`\ (\ value\: :ref:`float<class_float>`\ ) |
  166. +-------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  167. | |void| | :ref:`store_string<class_FileAccess_method_store_string>`\ (\ string\: :ref:`String<class_String>`\ ) |
  168. +-------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  169. | |void| | :ref:`store_var<class_FileAccess_method_store_var>`\ (\ value\: :ref:`Variant<class_Variant>`, full_objects\: :ref:`bool<class_bool>` = false\ ) |
  170. +-------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  171. .. rst-class:: classref-section-separator
  172. ----
  173. .. rst-class:: classref-descriptions-group
  174. Enumerations
  175. ------------
  176. .. _enum_FileAccess_ModeFlags:
  177. .. rst-class:: classref-enumeration
  178. enum **ModeFlags**: :ref:`🔗<enum_FileAccess_ModeFlags>`
  179. .. _class_FileAccess_constant_READ:
  180. .. rst-class:: classref-enumeration-constant
  181. :ref:`ModeFlags<enum_FileAccess_ModeFlags>` **READ** = ``1``
  182. Opens the file for read operations. The cursor is positioned at the beginning of the file.
  183. .. _class_FileAccess_constant_WRITE:
  184. .. rst-class:: classref-enumeration-constant
  185. :ref:`ModeFlags<enum_FileAccess_ModeFlags>` **WRITE** = ``2``
  186. Opens the file for write operations. The file is created if it does not exist, and truncated if it does.
  187. \ **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>`.
  188. .. _class_FileAccess_constant_READ_WRITE:
  189. .. rst-class:: classref-enumeration-constant
  190. :ref:`ModeFlags<enum_FileAccess_ModeFlags>` **READ_WRITE** = ``3``
  191. Opens the file for read and write operations. Does not truncate the file. The cursor is positioned at the beginning of the file.
  192. .. _class_FileAccess_constant_WRITE_READ:
  193. .. rst-class:: classref-enumeration-constant
  194. :ref:`ModeFlags<enum_FileAccess_ModeFlags>` **WRITE_READ** = ``7``
  195. 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.
  196. \ **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>`.
  197. .. rst-class:: classref-item-separator
  198. ----
  199. .. _enum_FileAccess_CompressionMode:
  200. .. rst-class:: classref-enumeration
  201. enum **CompressionMode**: :ref:`🔗<enum_FileAccess_CompressionMode>`
  202. .. _class_FileAccess_constant_COMPRESSION_FASTLZ:
  203. .. rst-class:: classref-enumeration-constant
  204. :ref:`CompressionMode<enum_FileAccess_CompressionMode>` **COMPRESSION_FASTLZ** = ``0``
  205. Uses the `FastLZ <https://fastlz.org/>`__ compression method.
  206. .. _class_FileAccess_constant_COMPRESSION_DEFLATE:
  207. .. rst-class:: classref-enumeration-constant
  208. :ref:`CompressionMode<enum_FileAccess_CompressionMode>` **COMPRESSION_DEFLATE** = ``1``
  209. Uses the `DEFLATE <https://en.wikipedia.org/wiki/DEFLATE>`__ compression method.
  210. .. _class_FileAccess_constant_COMPRESSION_ZSTD:
  211. .. rst-class:: classref-enumeration-constant
  212. :ref:`CompressionMode<enum_FileAccess_CompressionMode>` **COMPRESSION_ZSTD** = ``2``
  213. Uses the `Zstandard <https://facebook.github.io/zstd/>`__ compression method.
  214. .. _class_FileAccess_constant_COMPRESSION_GZIP:
  215. .. rst-class:: classref-enumeration-constant
  216. :ref:`CompressionMode<enum_FileAccess_CompressionMode>` **COMPRESSION_GZIP** = ``3``
  217. Uses the `gzip <https://www.gzip.org/>`__ compression method.
  218. .. _class_FileAccess_constant_COMPRESSION_BROTLI:
  219. .. rst-class:: classref-enumeration-constant
  220. :ref:`CompressionMode<enum_FileAccess_CompressionMode>` **COMPRESSION_BROTLI** = ``4``
  221. Uses the `brotli <https://github.com/google/brotli>`__ compression method (only decompression is supported).
  222. .. rst-class:: classref-item-separator
  223. ----
  224. .. _enum_FileAccess_UnixPermissionFlags:
  225. .. rst-class:: classref-enumeration
  226. flags **UnixPermissionFlags**: :ref:`🔗<enum_FileAccess_UnixPermissionFlags>`
  227. .. _class_FileAccess_constant_UNIX_READ_OWNER:
  228. .. rst-class:: classref-enumeration-constant
  229. :ref:`UnixPermissionFlags<enum_FileAccess_UnixPermissionFlags>` **UNIX_READ_OWNER** = ``256``
  230. Read for owner bit.
  231. .. _class_FileAccess_constant_UNIX_WRITE_OWNER:
  232. .. rst-class:: classref-enumeration-constant
  233. :ref:`UnixPermissionFlags<enum_FileAccess_UnixPermissionFlags>` **UNIX_WRITE_OWNER** = ``128``
  234. Write for owner bit.
  235. .. _class_FileAccess_constant_UNIX_EXECUTE_OWNER:
  236. .. rst-class:: classref-enumeration-constant
  237. :ref:`UnixPermissionFlags<enum_FileAccess_UnixPermissionFlags>` **UNIX_EXECUTE_OWNER** = ``64``
  238. Execute for owner bit.
  239. .. _class_FileAccess_constant_UNIX_READ_GROUP:
  240. .. rst-class:: classref-enumeration-constant
  241. :ref:`UnixPermissionFlags<enum_FileAccess_UnixPermissionFlags>` **UNIX_READ_GROUP** = ``32``
  242. Read for group bit.
  243. .. _class_FileAccess_constant_UNIX_WRITE_GROUP:
  244. .. rst-class:: classref-enumeration-constant
  245. :ref:`UnixPermissionFlags<enum_FileAccess_UnixPermissionFlags>` **UNIX_WRITE_GROUP** = ``16``
  246. Write for group bit.
  247. .. _class_FileAccess_constant_UNIX_EXECUTE_GROUP:
  248. .. rst-class:: classref-enumeration-constant
  249. :ref:`UnixPermissionFlags<enum_FileAccess_UnixPermissionFlags>` **UNIX_EXECUTE_GROUP** = ``8``
  250. Execute for group bit.
  251. .. _class_FileAccess_constant_UNIX_READ_OTHER:
  252. .. rst-class:: classref-enumeration-constant
  253. :ref:`UnixPermissionFlags<enum_FileAccess_UnixPermissionFlags>` **UNIX_READ_OTHER** = ``4``
  254. Read for other bit.
  255. .. _class_FileAccess_constant_UNIX_WRITE_OTHER:
  256. .. rst-class:: classref-enumeration-constant
  257. :ref:`UnixPermissionFlags<enum_FileAccess_UnixPermissionFlags>` **UNIX_WRITE_OTHER** = ``2``
  258. Write for other bit.
  259. .. _class_FileAccess_constant_UNIX_EXECUTE_OTHER:
  260. .. rst-class:: classref-enumeration-constant
  261. :ref:`UnixPermissionFlags<enum_FileAccess_UnixPermissionFlags>` **UNIX_EXECUTE_OTHER** = ``1``
  262. Execute for other bit.
  263. .. _class_FileAccess_constant_UNIX_SET_USER_ID:
  264. .. rst-class:: classref-enumeration-constant
  265. :ref:`UnixPermissionFlags<enum_FileAccess_UnixPermissionFlags>` **UNIX_SET_USER_ID** = ``2048``
  266. Set user id on execution bit.
  267. .. _class_FileAccess_constant_UNIX_SET_GROUP_ID:
  268. .. rst-class:: classref-enumeration-constant
  269. :ref:`UnixPermissionFlags<enum_FileAccess_UnixPermissionFlags>` **UNIX_SET_GROUP_ID** = ``1024``
  270. Set group id on execution bit.
  271. .. _class_FileAccess_constant_UNIX_RESTRICTED_DELETE:
  272. .. rst-class:: classref-enumeration-constant
  273. :ref:`UnixPermissionFlags<enum_FileAccess_UnixPermissionFlags>` **UNIX_RESTRICTED_DELETE** = ``512``
  274. Restricted deletion (sticky) bit.
  275. .. rst-class:: classref-section-separator
  276. ----
  277. .. rst-class:: classref-descriptions-group
  278. Property Descriptions
  279. ---------------------
  280. .. _class_FileAccess_property_big_endian:
  281. .. rst-class:: classref-property
  282. :ref:`bool<class_bool>` **big_endian** :ref:`🔗<class_FileAccess_property_big_endian>`
  283. .. rst-class:: classref-property-setget
  284. - |void| **set_big_endian**\ (\ value\: :ref:`bool<class_bool>`\ )
  285. - :ref:`bool<class_bool>` **is_big_endian**\ (\ )
  286. 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.
  287. \ **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.
  288. \ **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.
  289. .. rst-class:: classref-section-separator
  290. ----
  291. .. rst-class:: classref-descriptions-group
  292. Method Descriptions
  293. -------------------
  294. .. _class_FileAccess_method_close:
  295. .. rst-class:: classref-method
  296. |void| **close**\ (\ ) :ref:`🔗<class_FileAccess_method_close>`
  297. 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.
  298. \ **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.
  299. .. rst-class:: classref-item-separator
  300. ----
  301. .. _class_FileAccess_method_eof_reached:
  302. .. rst-class:: classref-method
  303. :ref:`bool<class_bool>` **eof_reached**\ (\ ) |const| :ref:`🔗<class_FileAccess_method_eof_reached>`
  304. Returns ``true`` if the file cursor has already read past the end of the file.
  305. \ **Note:** ``eof_reached() == false`` cannot be used to check whether there is more data available. To loop while there is more data available, use:
  306. .. tabs::
  307. .. code-tab:: gdscript
  308. while file.get_position() < file.get_length():
  309. # Read data
  310. .. code-tab:: csharp
  311. while (file.GetPosition() < file.GetLength())
  312. {
  313. // Read data
  314. }
  315. .. rst-class:: classref-item-separator
  316. ----
  317. .. _class_FileAccess_method_file_exists:
  318. .. rst-class:: classref-method
  319. :ref:`bool<class_bool>` **file_exists**\ (\ path\: :ref:`String<class_String>`\ ) |static| :ref:`🔗<class_FileAccess_method_file_exists>`
  320. Returns ``true`` if the file exists in the given path.
  321. \ **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.
  322. For a non-static, relative equivalent, use :ref:`DirAccess.file_exists<class_DirAccess_method_file_exists>`.
  323. .. rst-class:: classref-item-separator
  324. ----
  325. .. _class_FileAccess_method_flush:
  326. .. rst-class:: classref-method
  327. |void| **flush**\ (\ ) :ref:`🔗<class_FileAccess_method_flush>`
  328. 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.
  329. \ **Note:** Only call :ref:`flush<class_FileAccess_method_flush>` when you actually need it. Otherwise, it will decrease performance due to constant disk writes.
  330. .. rst-class:: classref-item-separator
  331. ----
  332. .. _class_FileAccess_method_get_8:
  333. .. rst-class:: classref-method
  334. :ref:`int<class_int>` **get_8**\ (\ ) |const| :ref:`🔗<class_FileAccess_method_get_8>`
  335. 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.
  336. .. rst-class:: classref-item-separator
  337. ----
  338. .. _class_FileAccess_method_get_16:
  339. .. rst-class:: classref-method
  340. :ref:`int<class_int>` **get_16**\ (\ ) |const| :ref:`🔗<class_FileAccess_method_get_16>`
  341. 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.
  342. .. rst-class:: classref-item-separator
  343. ----
  344. .. _class_FileAccess_method_get_32:
  345. .. rst-class:: classref-method
  346. :ref:`int<class_int>` **get_32**\ (\ ) |const| :ref:`🔗<class_FileAccess_method_get_32>`
  347. 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.
  348. .. rst-class:: classref-item-separator
  349. ----
  350. .. _class_FileAccess_method_get_64:
  351. .. rst-class:: classref-method
  352. :ref:`int<class_int>` **get_64**\ (\ ) |const| :ref:`🔗<class_FileAccess_method_get_64>`
  353. 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.
  354. .. rst-class:: classref-item-separator
  355. ----
  356. .. _class_FileAccess_method_get_as_text:
  357. .. rst-class:: classref-method
  358. :ref:`String<class_String>` **get_as_text**\ (\ skip_cr\: :ref:`bool<class_bool>` = false\ ) |const| :ref:`🔗<class_FileAccess_method_get_as_text>`
  359. Returns the whole file as a :ref:`String<class_String>`. Text is interpreted as being UTF-8 encoded.
  360. 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).
  361. .. rst-class:: classref-item-separator
  362. ----
  363. .. _class_FileAccess_method_get_buffer:
  364. .. rst-class:: classref-method
  365. :ref:`PackedByteArray<class_PackedByteArray>` **get_buffer**\ (\ length\: :ref:`int<class_int>`\ ) |const| :ref:`🔗<class_FileAccess_method_get_buffer>`
  366. Returns next ``length`` bytes of the file as a :ref:`PackedByteArray<class_PackedByteArray>`.
  367. .. rst-class:: classref-item-separator
  368. ----
  369. .. _class_FileAccess_method_get_csv_line:
  370. .. rst-class:: classref-method
  371. :ref:`PackedStringArray<class_PackedStringArray>` **get_csv_line**\ (\ delim\: :ref:`String<class_String>` = ","\ ) |const| :ref:`🔗<class_FileAccess_method_get_csv_line>`
  372. 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.
  373. 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.
  374. For example, the following CSV lines are valid and will be properly parsed as two strings each:
  375. .. code:: text
  376. Alice,"Hello, Bob!"
  377. Bob,Alice! What a surprise!
  378. Alice,"I thought you'd reply with ""Hello, world""."
  379. 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.
  380. .. rst-class:: classref-item-separator
  381. ----
  382. .. _class_FileAccess_method_get_double:
  383. .. rst-class:: classref-method
  384. :ref:`float<class_float>` **get_double**\ (\ ) |const| :ref:`🔗<class_FileAccess_method_get_double>`
  385. Returns the next 64 bits from the file as a floating-point number.
  386. .. rst-class:: classref-item-separator
  387. ----
  388. .. _class_FileAccess_method_get_error:
  389. .. rst-class:: classref-method
  390. :ref:`Error<enum_@GlobalScope_Error>` **get_error**\ (\ ) |const| :ref:`🔗<class_FileAccess_method_get_error>`
  391. Returns the last error that happened when trying to perform operations. Compare with the ``ERR_FILE_*`` constants from :ref:`Error<enum_@GlobalScope_Error>`.
  392. .. rst-class:: classref-item-separator
  393. ----
  394. .. _class_FileAccess_method_get_file_as_bytes:
  395. .. rst-class:: classref-method
  396. :ref:`PackedByteArray<class_PackedByteArray>` **get_file_as_bytes**\ (\ path\: :ref:`String<class_String>`\ ) |static| :ref:`🔗<class_FileAccess_method_get_file_as_bytes>`
  397. Returns the whole ``path`` file contents as a :ref:`PackedByteArray<class_PackedByteArray>` without any decoding.
  398. 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.
  399. .. rst-class:: classref-item-separator
  400. ----
  401. .. _class_FileAccess_method_get_file_as_string:
  402. .. rst-class:: classref-method
  403. :ref:`String<class_String>` **get_file_as_string**\ (\ path\: :ref:`String<class_String>`\ ) |static| :ref:`🔗<class_FileAccess_method_get_file_as_string>`
  404. Returns the whole ``path`` file contents as a :ref:`String<class_String>`. Text is interpreted as being UTF-8 encoded.
  405. 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.
  406. .. rst-class:: classref-item-separator
  407. ----
  408. .. _class_FileAccess_method_get_float:
  409. .. rst-class:: classref-method
  410. :ref:`float<class_float>` **get_float**\ (\ ) |const| :ref:`🔗<class_FileAccess_method_get_float>`
  411. Returns the next 32 bits from the file as a floating-point number.
  412. .. rst-class:: classref-item-separator
  413. ----
  414. .. _class_FileAccess_method_get_hidden_attribute:
  415. .. rst-class:: classref-method
  416. :ref:`bool<class_bool>` **get_hidden_attribute**\ (\ file\: :ref:`String<class_String>`\ ) |static| :ref:`🔗<class_FileAccess_method_get_hidden_attribute>`
  417. Returns ``true``, if file ``hidden`` attribute is set.
  418. \ **Note:** This method is implemented on iOS, BSD, macOS, and Windows.
  419. .. rst-class:: classref-item-separator
  420. ----
  421. .. _class_FileAccess_method_get_length:
  422. .. rst-class:: classref-method
  423. :ref:`int<class_int>` **get_length**\ (\ ) |const| :ref:`🔗<class_FileAccess_method_get_length>`
  424. Returns the size of the file in bytes.
  425. .. rst-class:: classref-item-separator
  426. ----
  427. .. _class_FileAccess_method_get_line:
  428. .. rst-class:: classref-method
  429. :ref:`String<class_String>` **get_line**\ (\ ) |const| :ref:`🔗<class_FileAccess_method_get_line>`
  430. Returns the next line of the file as a :ref:`String<class_String>`. The returned string doesn't include newline (``\n``) or carriage return (``\r``) characters, but does include any other leading or trailing whitespace.
  431. Text is interpreted as being UTF-8 encoded.
  432. .. rst-class:: classref-item-separator
  433. ----
  434. .. _class_FileAccess_method_get_md5:
  435. .. rst-class:: classref-method
  436. :ref:`String<class_String>` **get_md5**\ (\ path\: :ref:`String<class_String>`\ ) |static| :ref:`🔗<class_FileAccess_method_get_md5>`
  437. Returns an MD5 String representing the file at the given path or an empty :ref:`String<class_String>` on failure.
  438. .. rst-class:: classref-item-separator
  439. ----
  440. .. _class_FileAccess_method_get_modified_time:
  441. .. rst-class:: classref-method
  442. :ref:`int<class_int>` **get_modified_time**\ (\ file\: :ref:`String<class_String>`\ ) |static| :ref:`🔗<class_FileAccess_method_get_modified_time>`
  443. 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.
  444. .. rst-class:: classref-item-separator
  445. ----
  446. .. _class_FileAccess_method_get_open_error:
  447. .. rst-class:: classref-method
  448. :ref:`Error<enum_@GlobalScope_Error>` **get_open_error**\ (\ ) |static| :ref:`🔗<class_FileAccess_method_get_open_error>`
  449. Returns the result of the last :ref:`open<class_FileAccess_method_open>` call in the current thread.
  450. .. rst-class:: classref-item-separator
  451. ----
  452. .. _class_FileAccess_method_get_pascal_string:
  453. .. rst-class:: classref-method
  454. :ref:`String<class_String>` **get_pascal_string**\ (\ ) :ref:`🔗<class_FileAccess_method_get_pascal_string>`
  455. Returns a :ref:`String<class_String>` saved in Pascal format from the file.
  456. Text is interpreted as being UTF-8 encoded.
  457. .. rst-class:: classref-item-separator
  458. ----
  459. .. _class_FileAccess_method_get_path:
  460. .. rst-class:: classref-method
  461. :ref:`String<class_String>` **get_path**\ (\ ) |const| :ref:`🔗<class_FileAccess_method_get_path>`
  462. Returns the path as a :ref:`String<class_String>` for the current open file.
  463. .. rst-class:: classref-item-separator
  464. ----
  465. .. _class_FileAccess_method_get_path_absolute:
  466. .. rst-class:: classref-method
  467. :ref:`String<class_String>` **get_path_absolute**\ (\ ) |const| :ref:`🔗<class_FileAccess_method_get_path_absolute>`
  468. Returns the absolute path as a :ref:`String<class_String>` for the current open file.
  469. .. rst-class:: classref-item-separator
  470. ----
  471. .. _class_FileAccess_method_get_position:
  472. .. rst-class:: classref-method
  473. :ref:`int<class_int>` **get_position**\ (\ ) |const| :ref:`🔗<class_FileAccess_method_get_position>`
  474. Returns the file cursor's position.
  475. .. rst-class:: classref-item-separator
  476. ----
  477. .. _class_FileAccess_method_get_read_only_attribute:
  478. .. rst-class:: classref-method
  479. :ref:`bool<class_bool>` **get_read_only_attribute**\ (\ file\: :ref:`String<class_String>`\ ) |static| :ref:`🔗<class_FileAccess_method_get_read_only_attribute>`
  480. Returns ``true``, if file ``read only`` attribute is set.
  481. \ **Note:** This method is implemented on iOS, BSD, macOS, and Windows.
  482. .. rst-class:: classref-item-separator
  483. ----
  484. .. _class_FileAccess_method_get_real:
  485. .. rst-class:: classref-method
  486. :ref:`float<class_float>` **get_real**\ (\ ) |const| :ref:`🔗<class_FileAccess_method_get_real>`
  487. Returns the next bits from the file as a floating-point number.
  488. .. rst-class:: classref-item-separator
  489. ----
  490. .. _class_FileAccess_method_get_sha256:
  491. .. rst-class:: classref-method
  492. :ref:`String<class_String>` **get_sha256**\ (\ path\: :ref:`String<class_String>`\ ) |static| :ref:`🔗<class_FileAccess_method_get_sha256>`
  493. Returns an SHA-256 :ref:`String<class_String>` representing the file at the given path or an empty :ref:`String<class_String>` on failure.
  494. .. rst-class:: classref-item-separator
  495. ----
  496. .. _class_FileAccess_method_get_unix_permissions:
  497. .. rst-class:: classref-method
  498. |bitfield|\[:ref:`UnixPermissionFlags<enum_FileAccess_UnixPermissionFlags>`\] **get_unix_permissions**\ (\ file\: :ref:`String<class_String>`\ ) |static| :ref:`🔗<class_FileAccess_method_get_unix_permissions>`
  499. Returns file UNIX permissions.
  500. \ **Note:** This method is implemented on iOS, Linux/BSD, and macOS.
  501. .. rst-class:: classref-item-separator
  502. ----
  503. .. _class_FileAccess_method_get_var:
  504. .. rst-class:: classref-method
  505. :ref:`Variant<class_Variant>` **get_var**\ (\ allow_objects\: :ref:`bool<class_bool>` = false\ ) |const| :ref:`🔗<class_FileAccess_method_get_var>`
  506. Returns the next :ref:`Variant<class_Variant>` value from the file. If ``allow_objects`` is ``true``, decoding objects is allowed.
  507. Internally, this uses the same decoding mechanism as the :ref:`@GlobalScope.bytes_to_var<class_@GlobalScope_method_bytes_to_var>` method.
  508. \ **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.
  509. .. rst-class:: classref-item-separator
  510. ----
  511. .. _class_FileAccess_method_is_open:
  512. .. rst-class:: classref-method
  513. :ref:`bool<class_bool>` **is_open**\ (\ ) |const| :ref:`🔗<class_FileAccess_method_is_open>`
  514. Returns ``true`` if the file is currently opened.
  515. .. rst-class:: classref-item-separator
  516. ----
  517. .. _class_FileAccess_method_open:
  518. .. rst-class:: classref-method
  519. :ref:`FileAccess<class_FileAccess>` **open**\ (\ path\: :ref:`String<class_String>`, flags\: :ref:`ModeFlags<enum_FileAccess_ModeFlags>`\ ) |static| :ref:`🔗<class_FileAccess_method_open>`
  520. Creates a new **FileAccess** object and opens the file for writing or reading, depending on the flags.
  521. 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.
  522. .. rst-class:: classref-item-separator
  523. ----
  524. .. _class_FileAccess_method_open_compressed:
  525. .. rst-class:: classref-method
  526. :ref:`FileAccess<class_FileAccess>` **open_compressed**\ (\ path\: :ref:`String<class_String>`, mode_flags\: :ref:`ModeFlags<enum_FileAccess_ModeFlags>`, compression_mode\: :ref:`CompressionMode<enum_FileAccess_CompressionMode>` = 0\ ) |static| :ref:`🔗<class_FileAccess_method_open_compressed>`
  527. Creates a new **FileAccess** object and opens a compressed file for reading or writing.
  528. \ **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.
  529. 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.
  530. .. rst-class:: classref-item-separator
  531. ----
  532. .. _class_FileAccess_method_open_encrypted:
  533. .. rst-class:: classref-method
  534. :ref:`FileAccess<class_FileAccess>` **open_encrypted**\ (\ path\: :ref:`String<class_String>`, mode_flags\: :ref:`ModeFlags<enum_FileAccess_ModeFlags>`, key\: :ref:`PackedByteArray<class_PackedByteArray>`\ ) |static| :ref:`🔗<class_FileAccess_method_open_encrypted>`
  535. 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.
  536. \ **Note:** The provided key must be 32 bytes long.
  537. 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.
  538. .. rst-class:: classref-item-separator
  539. ----
  540. .. _class_FileAccess_method_open_encrypted_with_pass:
  541. .. rst-class:: classref-method
  542. :ref:`FileAccess<class_FileAccess>` **open_encrypted_with_pass**\ (\ path\: :ref:`String<class_String>`, mode_flags\: :ref:`ModeFlags<enum_FileAccess_ModeFlags>`, pass\: :ref:`String<class_String>`\ ) |static| :ref:`🔗<class_FileAccess_method_open_encrypted_with_pass>`
  543. 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.
  544. 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.
  545. .. rst-class:: classref-item-separator
  546. ----
  547. .. _class_FileAccess_method_resize:
  548. .. rst-class:: classref-method
  549. :ref:`Error<enum_@GlobalScope_Error>` **resize**\ (\ length\: :ref:`int<class_int>`\ ) :ref:`🔗<class_FileAccess_method_resize>`
  550. 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.
  551. .. rst-class:: classref-item-separator
  552. ----
  553. .. _class_FileAccess_method_seek:
  554. .. rst-class:: classref-method
  555. |void| **seek**\ (\ position\: :ref:`int<class_int>`\ ) :ref:`🔗<class_FileAccess_method_seek>`
  556. Changes the file reading/writing cursor to the specified position (in bytes from the beginning of the file).
  557. .. rst-class:: classref-item-separator
  558. ----
  559. .. _class_FileAccess_method_seek_end:
  560. .. rst-class:: classref-method
  561. |void| **seek_end**\ (\ position\: :ref:`int<class_int>` = 0\ ) :ref:`🔗<class_FileAccess_method_seek_end>`
  562. Changes the file reading/writing cursor to the specified position (in bytes from the end of the file).
  563. \ **Note:** This is an offset, so you should use negative numbers or the cursor will be at the end of the file.
  564. .. rst-class:: classref-item-separator
  565. ----
  566. .. _class_FileAccess_method_set_hidden_attribute:
  567. .. rst-class:: classref-method
  568. :ref:`Error<enum_@GlobalScope_Error>` **set_hidden_attribute**\ (\ file\: :ref:`String<class_String>`, hidden\: :ref:`bool<class_bool>`\ ) |static| :ref:`🔗<class_FileAccess_method_set_hidden_attribute>`
  569. Sets file **hidden** attribute.
  570. \ **Note:** This method is implemented on iOS, BSD, macOS, and Windows.
  571. .. rst-class:: classref-item-separator
  572. ----
  573. .. _class_FileAccess_method_set_read_only_attribute:
  574. .. rst-class:: classref-method
  575. :ref:`Error<enum_@GlobalScope_Error>` **set_read_only_attribute**\ (\ file\: :ref:`String<class_String>`, ro\: :ref:`bool<class_bool>`\ ) |static| :ref:`🔗<class_FileAccess_method_set_read_only_attribute>`
  576. Sets file **read only** attribute.
  577. \ **Note:** This method is implemented on iOS, BSD, macOS, and Windows.
  578. .. rst-class:: classref-item-separator
  579. ----
  580. .. _class_FileAccess_method_set_unix_permissions:
  581. .. rst-class:: classref-method
  582. :ref:`Error<enum_@GlobalScope_Error>` **set_unix_permissions**\ (\ file\: :ref:`String<class_String>`, permissions\: |bitfield|\[:ref:`UnixPermissionFlags<enum_FileAccess_UnixPermissionFlags>`\]\ ) |static| :ref:`🔗<class_FileAccess_method_set_unix_permissions>`
  583. Sets file UNIX permissions.
  584. \ **Note:** This method is implemented on iOS, Linux/BSD, and macOS.
  585. .. rst-class:: classref-item-separator
  586. ----
  587. .. _class_FileAccess_method_store_8:
  588. .. rst-class:: classref-method
  589. |void| **store_8**\ (\ value\: :ref:`int<class_int>`\ ) :ref:`🔗<class_FileAccess_method_store_8>`
  590. Stores an integer as 8 bits in the file.
  591. \ **Note:** The ``value`` should lie in the interval ``[0, 255]``. Any other value will overflow and wrap around.
  592. 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).
  593. .. rst-class:: classref-item-separator
  594. ----
  595. .. _class_FileAccess_method_store_16:
  596. .. rst-class:: classref-method
  597. |void| **store_16**\ (\ value\: :ref:`int<class_int>`\ ) :ref:`🔗<class_FileAccess_method_store_16>`
  598. Stores an integer as 16 bits in the file.
  599. \ **Note:** The ``value`` should lie in the interval ``[0, 2^16 - 1]``. Any other value will overflow and wrap around.
  600. 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:
  601. .. tabs::
  602. .. code-tab:: gdscript
  603. const MAX_15B = 1 << 15
  604. const MAX_16B = 1 << 16
  605. func unsigned16_to_signed(unsigned):
  606. return (unsigned + MAX_15B) % MAX_16B - MAX_15B
  607. func _ready():
  608. var f = FileAccess.open("user://file.dat", FileAccess.WRITE_READ)
  609. f.store_16(-42) # This wraps around and stores 65494 (2^16 - 42).
  610. f.store_16(121) # In bounds, will store 121.
  611. f.seek(0) # Go back to start to read the stored value.
  612. var read1 = f.get_16() # 65494
  613. var read2 = f.get_16() # 121
  614. var converted1 = unsigned16_to_signed(read1) # -42
  615. var converted2 = unsigned16_to_signed(read2) # 121
  616. .. code-tab:: csharp
  617. public override void _Ready()
  618. {
  619. using var f = FileAccess.Open("user://file.dat", FileAccess.ModeFlags.WriteRead);
  620. f.Store16(unchecked((ushort)-42)); // This wraps around and stores 65494 (2^16 - 42).
  621. f.Store16(121); // In bounds, will store 121.
  622. f.Seek(0); // Go back to start to read the stored value.
  623. ushort read1 = f.Get16(); // 65494
  624. ushort read2 = f.Get16(); // 121
  625. short converted1 = (short)read1; // -42
  626. short converted2 = (short)read2; // 121
  627. }
  628. .. rst-class:: classref-item-separator
  629. ----
  630. .. _class_FileAccess_method_store_32:
  631. .. rst-class:: classref-method
  632. |void| **store_32**\ (\ value\: :ref:`int<class_int>`\ ) :ref:`🔗<class_FileAccess_method_store_32>`
  633. Stores an integer as 32 bits in the file.
  634. \ **Note:** The ``value`` should lie in the interval ``[0, 2^32 - 1]``. Any other value will overflow and wrap around.
  635. 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).
  636. .. rst-class:: classref-item-separator
  637. ----
  638. .. _class_FileAccess_method_store_64:
  639. .. rst-class:: classref-method
  640. |void| **store_64**\ (\ value\: :ref:`int<class_int>`\ ) :ref:`🔗<class_FileAccess_method_store_64>`
  641. Stores an integer as 64 bits in the file.
  642. \ **Note:** The ``value`` must lie in the interval ``[-2^63, 2^63 - 1]`` (i.e. be a valid :ref:`int<class_int>` value).
  643. .. rst-class:: classref-item-separator
  644. ----
  645. .. _class_FileAccess_method_store_buffer:
  646. .. rst-class:: classref-method
  647. |void| **store_buffer**\ (\ buffer\: :ref:`PackedByteArray<class_PackedByteArray>`\ ) :ref:`🔗<class_FileAccess_method_store_buffer>`
  648. Stores the given array of bytes in the file.
  649. .. rst-class:: classref-item-separator
  650. ----
  651. .. _class_FileAccess_method_store_csv_line:
  652. .. rst-class:: classref-method
  653. |void| **store_csv_line**\ (\ values\: :ref:`PackedStringArray<class_PackedStringArray>`, delim\: :ref:`String<class_String>` = ","\ ) :ref:`🔗<class_FileAccess_method_store_csv_line>`
  654. 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.
  655. Text will be encoded as UTF-8.
  656. .. rst-class:: classref-item-separator
  657. ----
  658. .. _class_FileAccess_method_store_double:
  659. .. rst-class:: classref-method
  660. |void| **store_double**\ (\ value\: :ref:`float<class_float>`\ ) :ref:`🔗<class_FileAccess_method_store_double>`
  661. Stores a floating-point number as 64 bits in the file.
  662. .. rst-class:: classref-item-separator
  663. ----
  664. .. _class_FileAccess_method_store_float:
  665. .. rst-class:: classref-method
  666. |void| **store_float**\ (\ value\: :ref:`float<class_float>`\ ) :ref:`🔗<class_FileAccess_method_store_float>`
  667. Stores a floating-point number as 32 bits in the file.
  668. .. rst-class:: classref-item-separator
  669. ----
  670. .. _class_FileAccess_method_store_line:
  671. .. rst-class:: classref-method
  672. |void| **store_line**\ (\ line\: :ref:`String<class_String>`\ ) :ref:`🔗<class_FileAccess_method_store_line>`
  673. Appends ``line`` to the file followed by a line return character (``\n``), encoding the text as UTF-8.
  674. .. rst-class:: classref-item-separator
  675. ----
  676. .. _class_FileAccess_method_store_pascal_string:
  677. .. rst-class:: classref-method
  678. |void| **store_pascal_string**\ (\ string\: :ref:`String<class_String>`\ ) :ref:`🔗<class_FileAccess_method_store_pascal_string>`
  679. 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).
  680. Text will be encoded as UTF-8.
  681. .. rst-class:: classref-item-separator
  682. ----
  683. .. _class_FileAccess_method_store_real:
  684. .. rst-class:: classref-method
  685. |void| **store_real**\ (\ value\: :ref:`float<class_float>`\ ) :ref:`🔗<class_FileAccess_method_store_real>`
  686. Stores a floating-point number in the file.
  687. .. rst-class:: classref-item-separator
  688. ----
  689. .. _class_FileAccess_method_store_string:
  690. .. rst-class:: classref-method
  691. |void| **store_string**\ (\ string\: :ref:`String<class_String>`\ ) :ref:`🔗<class_FileAccess_method_store_string>`
  692. Appends ``string`` to the file without a line return, encoding the text as UTF-8.
  693. \ **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>`.
  694. .. rst-class:: classref-item-separator
  695. ----
  696. .. _class_FileAccess_method_store_var:
  697. .. rst-class:: classref-method
  698. |void| **store_var**\ (\ value\: :ref:`Variant<class_Variant>`, full_objects\: :ref:`bool<class_bool>` = false\ ) :ref:`🔗<class_FileAccess_method_store_var>`
  699. Stores any Variant value in the file. If ``full_objects`` is ``true``, encoding objects is allowed (and can potentially include code).
  700. Internally, this uses the same encoding mechanism as the :ref:`@GlobalScope.var_to_bytes<class_@GlobalScope_method_var_to_bytes>` method.
  701. \ **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.
  702. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  703. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  704. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  705. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  706. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  707. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
  708. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`
  709. .. |void| replace:: :abbr:`void (No return value.)`