class_fileaccess.rst 85 KB

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