Random beginner-question #15: File.READ/WRITE/READ_WRITE/WRITE_READ/JSON?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By pferft

Hi everyone,

where ever I look I can find the

File.READ: opens the file for read operations.

File.WRITE: opens the file for write operations. Create it if the file does not exist and truncate if it exists.

File.READ_WRITE: opens the file for read and write operations. Does not truncate the file.

File.WRITE_READ: opens the file for read and write operations. Create it if the file does not exist and truncate if it exists.

Could anybode please “explain the explanations” to me in simple words? What does that “truncate” do and why? What’s the difference between write_read and read_write and when would I want to choose which one? Why would I choose read (only) or write (only) when there is a way to do both “at once”?

And then some suggest JSON while others dismiss, so I suppose there there could be (dis-)advantages?

Any clarification would be much appreciated!

:bust_in_silhouette: Reply From: Zylann

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.

Thank you Zylann, that’s what I call understandable!

pferft | 2020-09-08 13:59