0 votes

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.

Godot version v3.2.3.stable.fedora
in Engine by (25 points)

2 Answers

+1 vote
Best answer
by (7,336 points)
selected by
+1 vote
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:
https://docs.godotengine.org/en/stable/classes/class_json.html

by (334 points)
edited by
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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.