+3 votes

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

asked Dec 15, 2017 in Engine by Will Nations (173 points)

1 Answer

+8 votes
Best answer

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).

answered Dec 15, 2017 by Will Nations (173 points)
edited Jan 4, 2018 by Will Nations

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.