These file modes reflect how your OS works. Files can be open for reading stuff only, or to also write stuff. It makes a difference in the way the OS will handle it. For example you can have multiple apps or threads reading the same file, if it is open for Read. But only one can open it for Write.
"Truncate" means "removing all previous contents of the file". Past this missing definition, the doc is pretty much self-explanatory...
If you only want to read the file, open it with File.READ
.
If you want to write stuff into the file, there are several possible scenarios:
- If you want the file to be created if it doesn't exist, and if you don't care about previous contents because you ONLY write the whole data (which is majority of cases), use
File.WRITE
.
- If you want to add extra information to an existing file without erasing its previous contents, use
File.READ_WRITE
. However that doesn't mean you can easily replace one line of text by inserting another, files don't actually work like text editors (instead, they work in "overwrite" mode) so you'll be better off writing the whole data anyways.
- The last mode sounds a bit redundant. Looks like it's the same as
File.WRITE
, except you have also permission to read.
Most of the time, you'll only be using File.READ
or File.WRITE
to read a full file and save it fully. Other modes are suited for more specific situations, such as logging, or altering binary files.
About JSON:
In short, it's a simple handy format which can easily transfer across applications. However, it does not support some Godot-specific data types (Vector2
, Color
, separate notion of int
vs float
...).
So the idea is, if you want to interact with a web service or another application that needs JSON, then it's a good choice.
If you only want to use it for a savegame, you can still use it but it will be easier to just use store_var
in the File
API, or use the ConfigFile
class.
If you only want to read it and not even modify it, then you'd be better off using a custom resource, export
or maybe even a GDScript with const
declarations in it.