What are various ways that I can store data?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Will Nations
:warning: Old Version Published before Godot 3 was released.

I would like a clear listing of my options when it comes to storing information for Godot projects / ways of organizing information.

:bust_in_silhouette: Reply From: Will Nations

Basically, there are 3 ways of doing things.

  1. The most common way is to store information in JSON which can easily be loaded to and from Dictionary objects using the JSON global (in 3.0) or the json methods on the Dictionary class (in 2.x). JSON is especially useful when the data is being transferred between Godot and 3rd party apps (since the structure of the JSON is flexible enough to be shared). The steps here are fairly simple: Create a .json file, make changes to it, save it, and then use the appropriate parse_json function from the object mentioned previously depending on your version.

  2. An equally useful method is to define your own Resource types to be saved as .tres/.res files. This means that the data is strictly structured (the object is guaranteed to have the properties accessible by name). Compared with JSON, this means you won’t have to keep checking whether the properties you are looking for actually exist. Loading them is also faster since all you have to do is load("res://name.res") and you’ve got your resource object. In addition, Godot automatically exports .tres files as binary .res files when making an executable, so this will ensure that the final file size is much smaller. On the downside, 3rd parties don’t use the .tres/.res file type, so it’s only usable within Godot itself. If you wish to change the data structure, you will have to change the script

The steps here are a bit more involved:

  1. create a non-built-in script that extends Resource
  2. add export variables to the script
  3. save the script
  4. create a new Resource in the Inspector
  5. attach the new script as the script for the resource (you may have to reload the Resource in the Inspector by clicking away and clicking back to it in order to see the exported variables appear)
  6. edit the properties in the Inspector as you see fit
  7. save the resource in the Inspector or, if doing it from script, use the ResourceSaver.save(...) method.
  8. load the resource (as described above) to get the object in code and use it.

The 3rd option is to use a database of some kind. Here is a GDScript powered SQLite repository with instructions on how to use it. With the C# scripting added in 3.0, it is now simpler to use many other types of databases, such as SQL Server or Neo4j’s Graph Database since many multi-platform databases publish C# drivers out-of-the-box. (note that at the time of the 3.0 release, tool-mode and consequently EditorPlugin scripts written in C# do not work, so these must be setup on a per-project basis).

There’s also the ConfigFIle class, which is intended to store user configuration settings.

Calinou | 2020-02-28 09:34

Thanks, it gave me some understanding of options.

If somebody needs some basic example, here is the thing I managed to get working to read and write names and stuff using .res file. So for this to work there has to be file chars.res in the said path. Uncommented part is the part that writes variable to file, and the commented part is the part that recreates the variable we stored.

After you run the uncommented part, you can comment or remove it, and then after running second part, you will get variable to use it like getting name or something in the end of the code:

	var obj2 = {"thing": "exists", "nothing": 0}
	var obj = {"name": "Character", "something": 12345, "inner": obj2}

	var file = File.new()
	file.open("res://chars.res", File.WRITE)

#	var file = File.new()
#	file.open("res://chars.res", File.READ)
#	var read_dict = str2var(file.get_as_text())
#	file.close()
#	var name = read_dict["name"] 
#	var something = read_dict["inner"]["thing"]

Ingeniou5 | 2021-12-11 18:26

Great example, thank you

Hazelrah | 2022-10-20 02:05