class_fileaccess.rst 71 KB

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