class_fileaccess.rst 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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. Type to handle file reading and writing operations.
  11. .. rst-class:: classref-introduction-group
  12. Description
  13. -----------
  14. File type. This is used to permanently store data into the user device's file system and to read from it. This can be used to store game save data or player configuration files, for example.
  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. There is no method to close a file in order to free it from use. Instead, **FileAccess** will 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.
  39. .. tabs::
  40. .. code-tab:: gdscript
  41. var file = FileAccess.open("res://something") # File is opened and locked for use.
  42. file = null # File is closed.
  43. .. code-tab:: csharp
  44. using var file = FileAccess.Open("res://something"); // File is opened and locked for use.
  45. // The using statement calls Dispose when going out of scope.
  46. \ **Note:** To access project resources once exported, it is recommended to use :ref:`ResourceLoader<class_ResourceLoader>` instead of the **FileAccess** API, as some files are converted to engine-specific formats and their original source files might not be present in the exported PCK package.
  47. \ **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.
  48. .. rst-class:: classref-introduction-group
  49. Tutorials
  50. ---------
  51. - :doc:`File system <../tutorials/scripting/filesystem>`
  52. - `3D Voxel Demo <https://godotengine.org/asset-library/asset/676>`__
  53. .. rst-class:: classref-reftable-group
  54. Properties
  55. ----------
  56. .. table::
  57. :widths: auto
  58. +-------------------------+---------------------------------------------------------+
  59. | :ref:`bool<class_bool>` | :ref:`big_endian<class_FileAccess_property_big_endian>` |
  60. +-------------------------+---------------------------------------------------------+
  61. .. rst-class:: classref-reftable-group
  62. Methods
  63. -------
  64. .. table::
  65. :widths: auto
  66. +---------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  67. | :ref:`bool<class_bool>` | :ref:`eof_reached<class_FileAccess_method_eof_reached>` **(** **)** |const| |
  68. +---------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  69. | :ref:`bool<class_bool>` | :ref:`file_exists<class_FileAccess_method_file_exists>` **(** :ref:`String<class_String>` path **)** |static| |
  70. +---------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  71. | void | :ref:`flush<class_FileAccess_method_flush>` **(** **)** |
  72. +---------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  73. | :ref:`int<class_int>` | :ref:`get_16<class_FileAccess_method_get_16>` **(** **)** |const| |
  74. +---------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  75. | :ref:`int<class_int>` | :ref:`get_32<class_FileAccess_method_get_32>` **(** **)** |const| |
  76. +---------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  77. | :ref:`int<class_int>` | :ref:`get_64<class_FileAccess_method_get_64>` **(** **)** |const| |
  78. +---------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  79. | :ref:`int<class_int>` | :ref:`get_8<class_FileAccess_method_get_8>` **(** **)** |const| |
  80. +---------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  81. | :ref:`String<class_String>` | :ref:`get_as_text<class_FileAccess_method_get_as_text>` **(** :ref:`bool<class_bool>` skip_cr=false **)** |const| |
  82. +---------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  83. | :ref:`PackedByteArray<class_PackedByteArray>` | :ref:`get_buffer<class_FileAccess_method_get_buffer>` **(** :ref:`int<class_int>` length **)** |const| |
  84. +---------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  85. | :ref:`PackedStringArray<class_PackedStringArray>` | :ref:`get_csv_line<class_FileAccess_method_get_csv_line>` **(** :ref:`String<class_String>` delim="," **)** |const| |
  86. +---------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  87. | :ref:`float<class_float>` | :ref:`get_double<class_FileAccess_method_get_double>` **(** **)** |const| |
  88. +---------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  89. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`get_error<class_FileAccess_method_get_error>` **(** **)** |const| |
  90. +---------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  91. | :ref:`PackedByteArray<class_PackedByteArray>` | :ref:`get_file_as_bytes<class_FileAccess_method_get_file_as_bytes>` **(** :ref:`String<class_String>` path **)** |static| |
  92. +---------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  93. | :ref:`String<class_String>` | :ref:`get_file_as_string<class_FileAccess_method_get_file_as_string>` **(** :ref:`String<class_String>` path **)** |static| |
  94. +---------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  95. | :ref:`float<class_float>` | :ref:`get_float<class_FileAccess_method_get_float>` **(** **)** |const| |
  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>` **(** :ref:`String<class_String>` path **)** |static| |
  102. +---------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  103. | :ref:`int<class_int>` | :ref:`get_modified_time<class_FileAccess_method_get_modified_time>` **(** :ref:`String<class_String>` file **)** |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:`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. | :ref:`Variant<class_Variant>` | :ref:`get_var<class_FileAccess_method_get_var>` **(** :ref:`bool<class_bool>` allow_objects=false **)** |const| |
  120. +---------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  121. | :ref:`bool<class_bool>` | :ref:`is_open<class_FileAccess_method_is_open>` **(** **)** |const| |
  122. +---------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  123. | :ref:`FileAccess<class_FileAccess>` | :ref:`open<class_FileAccess_method_open>` **(** :ref:`String<class_String>` path, :ref:`ModeFlags<enum_FileAccess_ModeFlags>` flags **)** |static| |
  124. +---------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  125. | :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| |
  126. +---------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  127. | :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| |
  128. +---------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  129. | :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| |
  130. +---------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  131. | void | :ref:`seek<class_FileAccess_method_seek>` **(** :ref:`int<class_int>` position **)** |
  132. +---------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  133. | void | :ref:`seek_end<class_FileAccess_method_seek_end>` **(** :ref:`int<class_int>` position=0 **)** |
  134. +---------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  135. | void | :ref:`store_16<class_FileAccess_method_store_16>` **(** :ref:`int<class_int>` value **)** |
  136. +---------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  137. | void | :ref:`store_32<class_FileAccess_method_store_32>` **(** :ref:`int<class_int>` value **)** |
  138. +---------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  139. | void | :ref:`store_64<class_FileAccess_method_store_64>` **(** :ref:`int<class_int>` value **)** |
  140. +---------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  141. | void | :ref:`store_8<class_FileAccess_method_store_8>` **(** :ref:`int<class_int>` value **)** |
  142. +---------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  143. | void | :ref:`store_buffer<class_FileAccess_method_store_buffer>` **(** :ref:`PackedByteArray<class_PackedByteArray>` buffer **)** |
  144. +---------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  145. | void | :ref:`store_csv_line<class_FileAccess_method_store_csv_line>` **(** :ref:`PackedStringArray<class_PackedStringArray>` values, :ref:`String<class_String>` delim="," **)** |
  146. +---------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  147. | void | :ref:`store_double<class_FileAccess_method_store_double>` **(** :ref:`float<class_float>` value **)** |
  148. +---------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  149. | void | :ref:`store_float<class_FileAccess_method_store_float>` **(** :ref:`float<class_float>` value **)** |
  150. +---------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  151. | void | :ref:`store_line<class_FileAccess_method_store_line>` **(** :ref:`String<class_String>` line **)** |
  152. +---------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  153. | void | :ref:`store_pascal_string<class_FileAccess_method_store_pascal_string>` **(** :ref:`String<class_String>` string **)** |
  154. +---------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  155. | void | :ref:`store_real<class_FileAccess_method_store_real>` **(** :ref:`float<class_float>` value **)** |
  156. +---------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  157. | void | :ref:`store_string<class_FileAccess_method_store_string>` **(** :ref:`String<class_String>` string **)** |
  158. +---------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  159. | void | :ref:`store_var<class_FileAccess_method_store_var>` **(** :ref:`Variant<class_Variant>` value, :ref:`bool<class_bool>` full_objects=false **)** |
  160. +---------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  161. .. rst-class:: classref-section-separator
  162. ----
  163. .. rst-class:: classref-descriptions-group
  164. Enumerations
  165. ------------
  166. .. _enum_FileAccess_ModeFlags:
  167. .. rst-class:: classref-enumeration
  168. enum **ModeFlags**:
  169. .. _class_FileAccess_constant_READ:
  170. .. rst-class:: classref-enumeration-constant
  171. :ref:`ModeFlags<enum_FileAccess_ModeFlags>` **READ** = ``1``
  172. Opens the file for read operations. The cursor is positioned at the beginning of the file.
  173. .. _class_FileAccess_constant_WRITE:
  174. .. rst-class:: classref-enumeration-constant
  175. :ref:`ModeFlags<enum_FileAccess_ModeFlags>` **WRITE** = ``2``
  176. Opens the file for write operations. The file is created if it does not exist, and truncated if it does.
  177. .. _class_FileAccess_constant_READ_WRITE:
  178. .. rst-class:: classref-enumeration-constant
  179. :ref:`ModeFlags<enum_FileAccess_ModeFlags>` **READ_WRITE** = ``3``
  180. Opens the file for read and write operations. Does not truncate the file. The cursor is positioned at the beginning of the file.
  181. .. _class_FileAccess_constant_WRITE_READ:
  182. .. rst-class:: classref-enumeration-constant
  183. :ref:`ModeFlags<enum_FileAccess_ModeFlags>` **WRITE_READ** = ``7``
  184. 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.
  185. .. rst-class:: classref-item-separator
  186. ----
  187. .. _enum_FileAccess_CompressionMode:
  188. .. rst-class:: classref-enumeration
  189. enum **CompressionMode**:
  190. .. _class_FileAccess_constant_COMPRESSION_FASTLZ:
  191. .. rst-class:: classref-enumeration-constant
  192. :ref:`CompressionMode<enum_FileAccess_CompressionMode>` **COMPRESSION_FASTLZ** = ``0``
  193. Uses the `FastLZ <https://fastlz.org/>`__ compression method.
  194. .. _class_FileAccess_constant_COMPRESSION_DEFLATE:
  195. .. rst-class:: classref-enumeration-constant
  196. :ref:`CompressionMode<enum_FileAccess_CompressionMode>` **COMPRESSION_DEFLATE** = ``1``
  197. Uses the `DEFLATE <https://en.wikipedia.org/wiki/DEFLATE>`__ compression method.
  198. .. _class_FileAccess_constant_COMPRESSION_ZSTD:
  199. .. rst-class:: classref-enumeration-constant
  200. :ref:`CompressionMode<enum_FileAccess_CompressionMode>` **COMPRESSION_ZSTD** = ``2``
  201. Uses the `Zstandard <https://facebook.github.io/zstd/>`__ compression method.
  202. .. _class_FileAccess_constant_COMPRESSION_GZIP:
  203. .. rst-class:: classref-enumeration-constant
  204. :ref:`CompressionMode<enum_FileAccess_CompressionMode>` **COMPRESSION_GZIP** = ``3``
  205. Uses the `gzip <https://www.gzip.org/>`__ compression method.
  206. .. rst-class:: classref-section-separator
  207. ----
  208. .. rst-class:: classref-descriptions-group
  209. Property Descriptions
  210. ---------------------
  211. .. _class_FileAccess_property_big_endian:
  212. .. rst-class:: classref-property
  213. :ref:`bool<class_bool>` **big_endian**
  214. .. rst-class:: classref-property-setget
  215. - void **set_big_endian** **(** :ref:`bool<class_bool>` value **)**
  216. - :ref:`bool<class_bool>` **is_big_endian** **(** **)**
  217. 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.
  218. \ **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.
  219. \ **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.
  220. .. rst-class:: classref-section-separator
  221. ----
  222. .. rst-class:: classref-descriptions-group
  223. Method Descriptions
  224. -------------------
  225. .. _class_FileAccess_method_eof_reached:
  226. .. rst-class:: classref-method
  227. :ref:`bool<class_bool>` **eof_reached** **(** **)** |const|
  228. Returns ``true`` if the file cursor has already read past the end of the file.
  229. \ **Note:** ``eof_reached() == false`` cannot be used to check whether there is more data available. To loop while there is more data available, use:
  230. .. tabs::
  231. .. code-tab:: gdscript
  232. while file.get_position() < file.get_length():
  233. # Read data
  234. .. code-tab:: csharp
  235. while (file.GetPosition() < file.GetLength())
  236. {
  237. // Read data
  238. }
  239. .. rst-class:: classref-item-separator
  240. ----
  241. .. _class_FileAccess_method_file_exists:
  242. .. rst-class:: classref-method
  243. :ref:`bool<class_bool>` **file_exists** **(** :ref:`String<class_String>` path **)** |static|
  244. Returns ``true`` if the file exists in the given path.
  245. \ **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.
  246. For a non-static, relative equivalent, use :ref:`DirAccess.file_exists<class_DirAccess_method_file_exists>`.
  247. .. rst-class:: classref-item-separator
  248. ----
  249. .. _class_FileAccess_method_flush:
  250. .. rst-class:: classref-method
  251. void **flush** **(** **)**
  252. 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.
  253. \ **Note:** Only call :ref:`flush<class_FileAccess_method_flush>` when you actually need it. Otherwise, it will decrease performance due to constant disk writes.
  254. .. rst-class:: classref-item-separator
  255. ----
  256. .. _class_FileAccess_method_get_16:
  257. .. rst-class:: classref-method
  258. :ref:`int<class_int>` **get_16** **(** **)** |const|
  259. 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.
  260. .. rst-class:: classref-item-separator
  261. ----
  262. .. _class_FileAccess_method_get_32:
  263. .. rst-class:: classref-method
  264. :ref:`int<class_int>` **get_32** **(** **)** |const|
  265. 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.
  266. .. rst-class:: classref-item-separator
  267. ----
  268. .. _class_FileAccess_method_get_64:
  269. .. rst-class:: classref-method
  270. :ref:`int<class_int>` **get_64** **(** **)** |const|
  271. 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.
  272. .. rst-class:: classref-item-separator
  273. ----
  274. .. _class_FileAccess_method_get_8:
  275. .. rst-class:: classref-method
  276. :ref:`int<class_int>` **get_8** **(** **)** |const|
  277. 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.
  278. .. rst-class:: classref-item-separator
  279. ----
  280. .. _class_FileAccess_method_get_as_text:
  281. .. rst-class:: classref-method
  282. :ref:`String<class_String>` **get_as_text** **(** :ref:`bool<class_bool>` skip_cr=false **)** |const|
  283. Returns the whole file as a :ref:`String<class_String>`. Text is interpreted as being UTF-8 encoded.
  284. 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).
  285. .. rst-class:: classref-item-separator
  286. ----
  287. .. _class_FileAccess_method_get_buffer:
  288. .. rst-class:: classref-method
  289. :ref:`PackedByteArray<class_PackedByteArray>` **get_buffer** **(** :ref:`int<class_int>` length **)** |const|
  290. Returns next ``length`` bytes of the file as a :ref:`PackedByteArray<class_PackedByteArray>`.
  291. .. rst-class:: classref-item-separator
  292. ----
  293. .. _class_FileAccess_method_get_csv_line:
  294. .. rst-class:: classref-method
  295. :ref:`PackedStringArray<class_PackedStringArray>` **get_csv_line** **(** :ref:`String<class_String>` delim="," **)** |const|
  296. 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.
  297. 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.
  298. For example, the following CSV lines are valid and will be properly parsed as two strings each:
  299. ::
  300. Alice,"Hello, Bob!"
  301. Bob,Alice! What a surprise!
  302. Alice,"I thought you'd reply with ""Hello, world""."
  303. 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.
  304. .. rst-class:: classref-item-separator
  305. ----
  306. .. _class_FileAccess_method_get_double:
  307. .. rst-class:: classref-method
  308. :ref:`float<class_float>` **get_double** **(** **)** |const|
  309. Returns the next 64 bits from the file as a floating-point number.
  310. .. rst-class:: classref-item-separator
  311. ----
  312. .. _class_FileAccess_method_get_error:
  313. .. rst-class:: classref-method
  314. :ref:`Error<enum_@GlobalScope_Error>` **get_error** **(** **)** |const|
  315. Returns the last error that happened when trying to perform operations. Compare with the ``ERR_FILE_*`` constants from :ref:`Error<enum_@GlobalScope_Error>`.
  316. .. rst-class:: classref-item-separator
  317. ----
  318. .. _class_FileAccess_method_get_file_as_bytes:
  319. .. rst-class:: classref-method
  320. :ref:`PackedByteArray<class_PackedByteArray>` **get_file_as_bytes** **(** :ref:`String<class_String>` path **)** |static|
  321. Returns the whole ``path`` file contents as a :ref:`PackedByteArray<class_PackedByteArray>` without any decoding.
  322. .. rst-class:: classref-item-separator
  323. ----
  324. .. _class_FileAccess_method_get_file_as_string:
  325. .. rst-class:: classref-method
  326. :ref:`String<class_String>` **get_file_as_string** **(** :ref:`String<class_String>` path **)** |static|
  327. Returns the whole ``path`` file contents as a :ref:`String<class_String>`. Text is interpreted as being UTF-8 encoded.
  328. .. rst-class:: classref-item-separator
  329. ----
  330. .. _class_FileAccess_method_get_float:
  331. .. rst-class:: classref-method
  332. :ref:`float<class_float>` **get_float** **(** **)** |const|
  333. Returns the next 32 bits from the file as a floating-point number.
  334. .. rst-class:: classref-item-separator
  335. ----
  336. .. _class_FileAccess_method_get_length:
  337. .. rst-class:: classref-method
  338. :ref:`int<class_int>` **get_length** **(** **)** |const|
  339. Returns the size of the file in bytes.
  340. .. rst-class:: classref-item-separator
  341. ----
  342. .. _class_FileAccess_method_get_line:
  343. .. rst-class:: classref-method
  344. :ref:`String<class_String>` **get_line** **(** **)** |const|
  345. Returns the next line of the file as a :ref:`String<class_String>`.
  346. Text is interpreted as being UTF-8 encoded.
  347. .. rst-class:: classref-item-separator
  348. ----
  349. .. _class_FileAccess_method_get_md5:
  350. .. rst-class:: classref-method
  351. :ref:`String<class_String>` **get_md5** **(** :ref:`String<class_String>` path **)** |static|
  352. Returns an MD5 String representing the file at the given path or an empty :ref:`String<class_String>` on failure.
  353. .. rst-class:: classref-item-separator
  354. ----
  355. .. _class_FileAccess_method_get_modified_time:
  356. .. rst-class:: classref-method
  357. :ref:`int<class_int>` **get_modified_time** **(** :ref:`String<class_String>` file **)** |static|
  358. Returns the last time the ``file`` was modified in Unix timestamp format or returns a :ref:`String<class_String>` "ERROR IN ``file``". This Unix timestamp can be converted to another format using the :ref:`Time<class_Time>` singleton.
  359. .. rst-class:: classref-item-separator
  360. ----
  361. .. _class_FileAccess_method_get_open_error:
  362. .. rst-class:: classref-method
  363. :ref:`Error<enum_@GlobalScope_Error>` **get_open_error** **(** **)** |static|
  364. Returns the result of the last :ref:`open<class_FileAccess_method_open>` call in the current thread.
  365. .. rst-class:: classref-item-separator
  366. ----
  367. .. _class_FileAccess_method_get_pascal_string:
  368. .. rst-class:: classref-method
  369. :ref:`String<class_String>` **get_pascal_string** **(** **)**
  370. Returns a :ref:`String<class_String>` saved in Pascal format from the file.
  371. Text is interpreted as being UTF-8 encoded.
  372. .. rst-class:: classref-item-separator
  373. ----
  374. .. _class_FileAccess_method_get_path:
  375. .. rst-class:: classref-method
  376. :ref:`String<class_String>` **get_path** **(** **)** |const|
  377. Returns the path as a :ref:`String<class_String>` for the current open file.
  378. .. rst-class:: classref-item-separator
  379. ----
  380. .. _class_FileAccess_method_get_path_absolute:
  381. .. rst-class:: classref-method
  382. :ref:`String<class_String>` **get_path_absolute** **(** **)** |const|
  383. Returns the absolute path as a :ref:`String<class_String>` for the current open file.
  384. .. rst-class:: classref-item-separator
  385. ----
  386. .. _class_FileAccess_method_get_position:
  387. .. rst-class:: classref-method
  388. :ref:`int<class_int>` **get_position** **(** **)** |const|
  389. Returns the file cursor's position.
  390. .. rst-class:: classref-item-separator
  391. ----
  392. .. _class_FileAccess_method_get_real:
  393. .. rst-class:: classref-method
  394. :ref:`float<class_float>` **get_real** **(** **)** |const|
  395. Returns the next bits from the file as a floating-point number.
  396. .. rst-class:: classref-item-separator
  397. ----
  398. .. _class_FileAccess_method_get_sha256:
  399. .. rst-class:: classref-method
  400. :ref:`String<class_String>` **get_sha256** **(** :ref:`String<class_String>` path **)** |static|
  401. Returns a SHA-256 :ref:`String<class_String>` representing the file at the given path or an empty :ref:`String<class_String>` on failure.
  402. .. rst-class:: classref-item-separator
  403. ----
  404. .. _class_FileAccess_method_get_var:
  405. .. rst-class:: classref-method
  406. :ref:`Variant<class_Variant>` **get_var** **(** :ref:`bool<class_bool>` allow_objects=false **)** |const|
  407. Returns the next :ref:`Variant<class_Variant>` value from the file. If ``allow_objects`` is ``true``, decoding objects is allowed.
  408. Internally, this uses the same decoding mechanism as the :ref:`@GlobalScope.bytes_to_var<class_@GlobalScope_method_bytes_to_var>` method.
  409. \ **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.
  410. .. rst-class:: classref-item-separator
  411. ----
  412. .. _class_FileAccess_method_is_open:
  413. .. rst-class:: classref-method
  414. :ref:`bool<class_bool>` **is_open** **(** **)** |const|
  415. Returns ``true`` if the file is currently opened.
  416. .. rst-class:: classref-item-separator
  417. ----
  418. .. _class_FileAccess_method_open:
  419. .. rst-class:: classref-method
  420. :ref:`FileAccess<class_FileAccess>` **open** **(** :ref:`String<class_String>` path, :ref:`ModeFlags<enum_FileAccess_ModeFlags>` flags **)** |static|
  421. Creates a new **FileAccess** object and opens the file for writing or reading, depending on the flags.
  422. 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.
  423. .. rst-class:: classref-item-separator
  424. ----
  425. .. _class_FileAccess_method_open_compressed:
  426. .. rst-class:: classref-method
  427. :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|
  428. Creates a new **FileAccess** object and opens a compressed file for reading or writing.
  429. \ **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.
  430. 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.
  431. .. rst-class:: classref-item-separator
  432. ----
  433. .. _class_FileAccess_method_open_encrypted:
  434. .. rst-class:: classref-method
  435. :ref:`FileAccess<class_FileAccess>` **open_encrypted** **(** :ref:`String<class_String>` path, :ref:`ModeFlags<enum_FileAccess_ModeFlags>` mode_flags, :ref:`PackedByteArray<class_PackedByteArray>` key **)** |static|
  436. 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.
  437. \ **Note:** The provided key must be 32 bytes long.
  438. 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.
  439. .. rst-class:: classref-item-separator
  440. ----
  441. .. _class_FileAccess_method_open_encrypted_with_pass:
  442. .. rst-class:: classref-method
  443. :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|
  444. 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.
  445. 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.
  446. .. rst-class:: classref-item-separator
  447. ----
  448. .. _class_FileAccess_method_seek:
  449. .. rst-class:: classref-method
  450. void **seek** **(** :ref:`int<class_int>` position **)**
  451. Changes the file reading/writing cursor to the specified position (in bytes from the beginning of the file).
  452. .. rst-class:: classref-item-separator
  453. ----
  454. .. _class_FileAccess_method_seek_end:
  455. .. rst-class:: classref-method
  456. void **seek_end** **(** :ref:`int<class_int>` position=0 **)**
  457. Changes the file reading/writing cursor to the specified position (in bytes from the end of the file).
  458. \ **Note:** This is an offset, so you should use negative numbers or the cursor will be at the end of the file.
  459. .. rst-class:: classref-item-separator
  460. ----
  461. .. _class_FileAccess_method_store_16:
  462. .. rst-class:: classref-method
  463. void **store_16** **(** :ref:`int<class_int>` value **)**
  464. Stores an integer as 16 bits in the file.
  465. \ **Note:** The ``value`` should lie in the interval ``[0, 2^16 - 1]``. Any other value will overflow and wrap around.
  466. 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:
  467. .. tabs::
  468. .. code-tab:: gdscript
  469. const MAX_15B = 1 << 15
  470. const MAX_16B = 1 << 16
  471. func unsigned16_to_signed(unsigned):
  472. return (unsigned + MAX_15B) % MAX_16B - MAX_15B
  473. func _ready():
  474. var f = FileAccess.open("user://file.dat", FileAccess.WRITE_READ)
  475. f.store_16(-42) # This wraps around and stores 65494 (2^16 - 42).
  476. f.store_16(121) # In bounds, will store 121.
  477. f.seek(0) # Go back to start to read the stored value.
  478. var read1 = f.get_16() # 65494
  479. var read2 = f.get_16() # 121
  480. var converted1 = unsigned16_to_signed(read1) # -42
  481. var converted2 = unsigned16_to_signed(read2) # 121
  482. .. code-tab:: csharp
  483. public override void _Ready()
  484. {
  485. using var f = FileAccess.Open("user://file.dat", FileAccess.ModeFlags.WriteRead);
  486. f.Store16(unchecked((ushort)-42)); // This wraps around and stores 65494 (2^16 - 42).
  487. f.Store16(121); // In bounds, will store 121.
  488. f.Seek(0); // Go back to start to read the stored value.
  489. ushort read1 = f.Get16(); // 65494
  490. ushort read2 = f.Get16(); // 121
  491. short converted1 = (short)read1; // -42
  492. short converted2 = (short)read2; // 121
  493. }
  494. .. rst-class:: classref-item-separator
  495. ----
  496. .. _class_FileAccess_method_store_32:
  497. .. rst-class:: classref-method
  498. void **store_32** **(** :ref:`int<class_int>` value **)**
  499. Stores an integer as 32 bits in the file.
  500. \ **Note:** The ``value`` should lie in the interval ``[0, 2^32 - 1]``. Any other value will overflow and wrap around.
  501. 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).
  502. .. rst-class:: classref-item-separator
  503. ----
  504. .. _class_FileAccess_method_store_64:
  505. .. rst-class:: classref-method
  506. void **store_64** **(** :ref:`int<class_int>` value **)**
  507. Stores an integer as 64 bits in the file.
  508. \ **Note:** The ``value`` must lie in the interval ``[-2^63, 2^63 - 1]`` (i.e. be a valid :ref:`int<class_int>` value).
  509. .. rst-class:: classref-item-separator
  510. ----
  511. .. _class_FileAccess_method_store_8:
  512. .. rst-class:: classref-method
  513. void **store_8** **(** :ref:`int<class_int>` value **)**
  514. Stores an integer as 8 bits in the file.
  515. \ **Note:** The ``value`` should lie in the interval ``[0, 255]``. Any other value will overflow and wrap around.
  516. 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).
  517. .. rst-class:: classref-item-separator
  518. ----
  519. .. _class_FileAccess_method_store_buffer:
  520. .. rst-class:: classref-method
  521. void **store_buffer** **(** :ref:`PackedByteArray<class_PackedByteArray>` buffer **)**
  522. Stores the given array of bytes in the file.
  523. .. rst-class:: classref-item-separator
  524. ----
  525. .. _class_FileAccess_method_store_csv_line:
  526. .. rst-class:: classref-method
  527. void **store_csv_line** **(** :ref:`PackedStringArray<class_PackedStringArray>` values, :ref:`String<class_String>` delim="," **)**
  528. 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.
  529. Text will be encoded as UTF-8.
  530. .. rst-class:: classref-item-separator
  531. ----
  532. .. _class_FileAccess_method_store_double:
  533. .. rst-class:: classref-method
  534. void **store_double** **(** :ref:`float<class_float>` value **)**
  535. Stores a floating-point number as 64 bits in the file.
  536. .. rst-class:: classref-item-separator
  537. ----
  538. .. _class_FileAccess_method_store_float:
  539. .. rst-class:: classref-method
  540. void **store_float** **(** :ref:`float<class_float>` value **)**
  541. Stores a floating-point number as 32 bits in the file.
  542. .. rst-class:: classref-item-separator
  543. ----
  544. .. _class_FileAccess_method_store_line:
  545. .. rst-class:: classref-method
  546. void **store_line** **(** :ref:`String<class_String>` line **)**
  547. Appends ``line`` to the file followed by a line return character (``\n``), encoding the text as UTF-8.
  548. .. rst-class:: classref-item-separator
  549. ----
  550. .. _class_FileAccess_method_store_pascal_string:
  551. .. rst-class:: classref-method
  552. void **store_pascal_string** **(** :ref:`String<class_String>` string **)**
  553. 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).
  554. Text will be encoded as UTF-8.
  555. .. rst-class:: classref-item-separator
  556. ----
  557. .. _class_FileAccess_method_store_real:
  558. .. rst-class:: classref-method
  559. void **store_real** **(** :ref:`float<class_float>` value **)**
  560. Stores a floating-point number in the file.
  561. .. rst-class:: classref-item-separator
  562. ----
  563. .. _class_FileAccess_method_store_string:
  564. .. rst-class:: classref-method
  565. void **store_string** **(** :ref:`String<class_String>` string **)**
  566. Appends ``string`` to the file without a line return, encoding the text as UTF-8.
  567. \ **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>`.
  568. .. rst-class:: classref-item-separator
  569. ----
  570. .. _class_FileAccess_method_store_var:
  571. .. rst-class:: classref-method
  572. void **store_var** **(** :ref:`Variant<class_Variant>` value, :ref:`bool<class_bool>` full_objects=false **)**
  573. Stores any Variant value in the file. If ``full_objects`` is ``true``, encoding objects is allowed (and can potentially include code).
  574. Internally, this uses the same encoding mechanism as the :ref:`@GlobalScope.var_to_bytes<class_@GlobalScope_method_var_to_bytes>` method.
  575. \ **Note:** Not all properties are included. Only properties that are configured with the :ref:`@GlobalScope.PROPERTY_USAGE_STORAGE<class_@GlobalScope_constant_PROPERTY_USAGE_STORAGE>` flag set will be serialized. You can add a new usage flag to a property by overriding the :ref:`Object._get_property_list<class_Object_method__get_property_list>` method in your class. You can also check how property usage is configured by calling :ref:`Object._get_property_list<class_Object_method__get_property_list>`. See :ref:`PropertyUsageFlags<enum_@GlobalScope_PropertyUsageFlags>` for the possible usage flags.
  576. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  577. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  578. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  579. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  580. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  581. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`