How to store and load JSON data?

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

I have a world map that is generated from a PNG file. A script analyzes the picture and generates an autotile TileMap based on the color of the pixels. The key point there is that the map is generated.

Now, I want to add some labels on it (place names and level entries). My first idea was to create a JSON file in which to store objects that look like this:

{
    "tutorial": {
        "name": "Tutorial Island",
        "description": "Your journey starts here!",
        "coordinates": {
            "x": 94,
            "y": 45
        }
    },
   "beach": {
        "name": "A hard way out of the sea",
        "description": "After the shipwreck, it's time to find food and water",
        "coordinates": {
            "x": 117,
            "y": 51
        }
    },
    ...
}

My problem is: how do I store those data?

  1. I tried putting them in a .json file, but it doesn’t seem to be recognized by Godot. I don’t want to open this file with an external program. If I have to, that means Godot is not intended to do that.
  2. I then tried to put it in a .tres file, but Godot tries to read the content and it doesn’t match the expected format.

How can I store my custom data in a way that can be read by the script?

Not that it’s OK if I have to change the format, I don’t have shares in the JSON company ;-). I barely started filling the data and retyping a few words won’t be a problem.

:bust_in_silhouette: Reply From: exuin

You can make your own custom resource Resources — Godot Engine (stable) documentation in English

:bust_in_silhouette: Reply From: bloodsign
var file_data = {
    "tutorial" : {
         "name": "..."
     }    
}

func save():
    var file = File.new()
    file.open("user://file_data.json", File.WRITE)
    file.store_line(to_json(file_data))
    file.close()

func load():
    var file = File.new()
    if not file.fileExists("user://file_data.json", File.READ)
             save()
             return
    file.open("user://file_data.json", File.READ)
    var data = parse_json(file.get_as_text())
    file_data = data

func _ready():
    load()
    if file_data.tutorial.has("name"):
         print(file_data.tutorial.name)

user location can be found by going to Project>Open Project Data Folder,
Although I would highly suggest to learn custom resource as well. And also this:

It appears some changes are needed for Godot4 beta.
Adapt as follow:

var file_data = {
    "tutorial" : {
         "name": "..."
     }    
}

func save():
    var file = FileAccess.open("user://file_data.json", FileAccess.WRITE)
    file.store_line(JSON.stringify(file_data))

func load():
    if not FileAccess.file_exists("user://file_data.json", FileAccess.READ)
             save()
             return
    var file = FileAccess.open("user://file_data.json", FileAccess.READ)
    var data = JSON.parse_string(file.get_as_text())
    file_data = data

func _ready():
    load()
    if file_data.tutorial.has("name"):
         print(file_data.tutorial.name)

a0kami | 2022-12-30 02:12

1 Like