summaryrefslogtreecommitdiff
path: root/doc/classes/File.xml
diff options
context:
space:
mode:
Diffstat (limited to 'doc/classes/File.xml')
-rw-r--r--doc/classes/File.xml60
1 files changed, 53 insertions, 7 deletions
diff --git a/doc/classes/File.xml b/doc/classes/File.xml
index ada57a8114..e0781e807f 100644
--- a/doc/classes/File.xml
+++ b/doc/classes/File.xml
@@ -6,7 +6,8 @@
<description>
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.
Here's a sample on how to write and read from a file:
- [codeblock]
+ [codeblocks]
+ [gdscript]
func save(content):
var file = File.new()
file.open("user://save_game.dat", File.WRITE)
@@ -19,9 +20,29 @@
var content = file.get_as_text()
file.close()
return content
- [/codeblock]
+ [/gdscript]
+ [csharp]
+ public void Save(string content)
+ {
+ var file = new File();
+ file.Open("user://save_game.dat", File.ModeFlags.Write);
+ file.StoreString(content);
+ file.Close();
+ }
+
+ public string Load()
+ {
+ var file = new File();
+ file.Open("user://save_game.dat", File.ModeFlags.Read);
+ string content = file.GetAsText();
+ file.Close();
+ return content;
+ }
+ [/csharp]
+ [/codeblocks]
In the example above, the file will be saved in the user data folder as specified in the [url=https://docs.godotengine.org/en/latest/tutorials/io/data_paths.html]Data paths[/url] documentation.
[b]Note:[/b] To access project resources once exported, it is recommended to use [ResourceLoader] instead of the [File] API, as some files are converted to engine-specific formats and their original source files might not be present in the exported PCK package.
+ [b]Note:[/b] Files are automatically closed only if the process exits "normally" (such as by clicking the window manager's close button or pressing [b]Alt + F4[/b]). If you stop the project execution by pressing [b]F8[/b] 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 [method flush] at regular intervals.
</description>
<tutorials>
<link title="File system">https://docs.godotengine.org/en/latest/getting_started/step_by_step/filesystem.html</link>
@@ -32,7 +53,7 @@
<return type="void">
</return>
<description>
- Closes the currently opened file.
+ Closes the currently opened file and prevents subsequent read/write operations. Use [method flush] to persist the data to disk without closing the file.
</description>
</method>
<method name="eof_reached" qualifiers="const">
@@ -53,6 +74,14 @@
[b]Note:[/b] 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 [method ResourceLoader.exists] for an alternative approach that takes resource remapping into account.
</description>
</method>
+ <method name="flush">
+ <return type="void">
+ </return>
+ <description>
+ Writes the file's buffer to disk. Flushing is automatically performed when the file is closed. This means you don't need to call [method flush] manually before closing a file using [method close]. Still, calling [method flush] can be used to ensure the data is safe even if the project crashes instead of being closed gracefully.
+ [b]Note:[/b] Only call [method flush] when you actually need it. Otherwise, it will decrease performance due to constant disk writes.
+ </description>
+ </method>
<method name="get_16" qualifiers="const">
<return type="int">
</return>
@@ -303,7 +332,8 @@
Stores an integer as 16 bits in the file.
[b]Note:[/b] The [code]value[/code] should lie in the interval [code][0, 2^16 - 1][/code]. Any other value will overflow and wrap around.
To store a signed integer, use [method store_64] or store a signed integer from the interval [code][-2^15, 2^15 - 1][/code] (i.e. keeping one bit for the signedness) and compute its sign manually when reading. For example:
- [codeblock]
+ [codeblocks]
+ [gdscript]
const MAX_15B = 1 &lt;&lt; 15
const MAX_16B = 1 &lt;&lt; 16
@@ -320,7 +350,22 @@
var read2 = f.get_16() # 121
var converted1 = unsigned16_to_signed(read1) # -42
var converted2 = unsigned16_to_signed(read2) # 121
- [/codeblock]
+ [/gdscript]
+ [csharp]
+ public override void _Ready()
+ {
+ var f = new File();
+ f.Open("user://file.dat", File.ModeFlags.WriteRead);
+ f.Store16(unchecked((ushort)-42)); // This wraps around and stores 65494 (2^16 - 42).
+ f.Store16(121); // In bounds, will store 121.
+ f.Seek(0); // Go back to start to read the stored value.
+ ushort read1 = f.Get16(); // 65494
+ ushort read2 = f.Get16(); // 121
+ short converted1 = BitConverter.ToInt16(BitConverter.GetBytes(read1), 0); // -42
+ short converted2 = BitConverter.ToInt16(BitConverter.GetBytes(read2), 0); // 121
+ }
+ [/csharp]
+ [/codeblocks]
</description>
</method>
<method name="store_32">
@@ -445,8 +490,9 @@
</methods>
<members>
<member name="endian_swap" type="bool" setter="set_endian_swap" getter="get_endian_swap" default="false">
- If [code]true[/code], the file's endianness is swapped. Use this if you're dealing with files written on big-endian machines.
- [b]Note:[/b] This is about the file format, not CPU type. This is always reset to [code]false[/code] whenever you open the file.
+ If [code]true[/code], the file is read with big-endian [url=https://en.wikipedia.org/wiki/Endianness]endianness[/url]. If [code]false[/code], the file is read with little-endian endianness. If in doubt, leave this to [code]false[/code] as most files are written with little-endian endianness.
+ [b]Note:[/b] [member endian_swap] is only about the file format, not the CPU type. The CPU endianness doesn't affect the default endianness for files written.
+ [b]Note:[/b] This is always reset to [code]false[/code] whenever you open the file. Therefore, you must set [member endian_swap] [i]after[/i] opening the file, not before.
</member>
</members>
<constants>