CSV, XML, JSON, Arrays, Dictionaries?

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

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.

:bust_in_silhouette: Reply From: Zylann

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: Making plugins — Godot Engine (stable) documentation in English (only explained for Nodes but I guess Resources work similar).

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

Paintbox | 2018-07-16 20:38

Adding this for reference where I found a better answer (for those also arriving through Google)
Need help reading data from a json file? - Archive - Godot Forum

Paintbox | 2018-07-16 20:38

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)

Zylann | 2018-07-16 21:17