+1 vote

Hi all,

I'm working on my next game. Breakout / Arkanoid.

I've created my first ever tool. A level editor. I can draw out the level, choose which tiles I want, etc etc. Looks great. Works great.

Now I want to be able to save out the data, so I can load it into my game.

I want to write out this kind of info:

[Level]
    [Number = 1]
    [Brick]
        [Location = 0,0]
            [Texture = "greenBrick.png"]
            [NumHits = "1"]
        [Location = 0,1]
            [Texture = "greenBrick.png"]
            [NumHits = "1"]
        [Location = 0,2]
            [Texture = "greenBrick2.png"]
            [NumHits = "2"]
        [Location = 0,3]
            [Texture = "greenBrick2.png"]
            [NumHits = "2"]
    [/Brick]
    [Brick]
        [Location = 1,0]
            [Texture = "redBrick.png"]
            [NumHits = "5"]
        [Location = 1,1]
            [Texture = "redBrick.png"]
            [NumHits = "5"]
        [Location = 1,2]
            [Texture = "yellowBrick.png"]
            [NumHits = "1"]
        [Location = 1,3]
            [Texture = "yellowBrick.png"]
            [NumHits = "1"]
    [/Brick]
[/Level]

Can anyone offer some advice on what methods I should be following? I looked at CSV at first but can find basically no info RE this in Godot. Then I realised I need better nesting of info anyway so I looked into XML. Looked good but then I noted a lot of reference that JSON format may be best. So then I started looking into that but found not as much info as I'd hoped.

This is basically my first foray into writing files with any real structure and beyond a simple score saving so I'm very new at this.

I wonder, can anyone nudge me in the right direction? Help would be greatly appreciated.

asked Apr 17, 2017 in Engine by Robster (808 points)

1 Answer

+2 votes

JSON is the easiest format in Godot, because you can save a dictionary into a JSON string and the reverse in just one function call:

var data = the_dictionary.to_json()
file.store_string(data)
#...
data = file.get_as_text()
the_dictionary.parse_json(data)

If you took the time to make a tool in Godot however, you could also look into how the engine itself saves Resources, which is not JSON, but TSCN/SCN.
Your tool would basically edit a custom Resource type that has a script (extends Resource) and all variables exported in this script will be saved if Godot saves that resource: http://docs.godotengine.org/en/stable/tutorials/making_plugins.html (only explained for Nodes but I guess Resources work similar).

answered Apr 17, 2017 by Zylann (27,004 points)

Adding this for reference where I found a better answer (for those also arriving through Google)
https://godotengine.org/qa/23534/need-help-reading-data-from-a-json-file

Is this still valid for Godot 3? It doesn't seem to work (parse_json is red, as in not being recognised)

This was an answer for Godot 2 indeed. In Godot 3 you should do something like this:

var data = to_json(the_dictionary)
file.store_string(data)
#...
data = file.get_as_text()
the_dictionary = parse_json(data)
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.