- Edited
Hi there, I'm pretty new to Godot & GDScript. I'm trying to output some data in JSON from a GODOT app and I've not had any success without hand building the JSON encoding in strings. So I'm doing this...
var max_x = map.get_used_rect().end.x+1
var max_y = map.get_used_rect().end.y+1
var hexes = "{\"hexes\":{"
var json_object = JSON.new()
for x in max_x:
for y in max_y:
var id = map.get_cell_source_id((0), Vector2i(x,y))
if id > -1:
hexes+= "\""+str(x)+str(y)+"\":{"
hexes+="\"x\":"+str(x)+","
hexes+="\"y\":"+str(y)+","
hexes+="\"terrain\":"+str(id)+"},"
hexes += "}}"
print(hexes)
Which generates {"hexes":{"00":{"x":0,"y":0,"terrain":1},"01":{"x":0,"y":1,"terrain":1},"02":{"x":0,"y":2,"terrain":2},"10":{"x":1,"y":0,"terrain":0},"20":{"x":2,"y":0,"terrain":0},"22":{"x":2,"y":2,"terrain":2},"30":{"x":3,"y":0,"terrain":2},}}
And is basically correct, except for the extra ",".
When I tried to do this by creating a JSON Node object with the terrain, x & y fields and then called to_json() I just get what I suspect is the internal Node reference ID encapsulated in JSON format. Is there a way to auto generate JSON encoding on objects that walks through the fields correctly?