class_fileaccess.rst 71 KB

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