0 votes

So, I want to load random level data using json files rather than try and figure out completely in engine level generation. Problem is that I'm relatively new to GDScript (and programming in general) and don't know much about JSON compatibility. My level file JSONs look like this:


  "ogmoVersion": "3.4.0",
  "width": 80,
  "height": 60,
  "offsetX": 0,
  "offsetY": 0,
  "layers": [
      "name": "collision",
      "_eid": "02443013",
      "offsetX": 0,
      "offsetY": 0,
      "gridCellWidth": 20,
      "gridCellHeight": 20,
      "gridCellsX": 4,
      "gridCellsY": 3,
      "tileset": "Placeholder Tiles",
      "data": [-1, -1, -1, -1, 2, -1, -1, 2, -1, -1, -1, -1],
      "exportMode": 0,
      "arrayMode": 0

And the GDScript I have right now is a bit like this:


extends TileMap

func _ready():
    var toPrint = _grabTileArray()

func _grabTileArray():
    var data_file = File.new()
    if data_file.open("res://roomData/testRoom1.json", File.READ) != OK:
        return null
    var data_text = data_file.get_as_text()
    var data_parse = JSON.parse(data_text)
    if data_parse.error != OK:
    var data = data_parse.result
    var tileArray = data["layers"]["data"]
    return tileArray

Any help would be appreciated.

Godot version 3.4.4
in Engine by (17 points)

2 Answers

+2 votes
Best answer

Since thedata array not direct child of layers but the element of list, you need to write

var tileArray = data["layers"][0]["data"]

This way you're getting data of 0 (first) element of layers of .json-file.

by (654 points)
selected by

Thanks for the help!

0 votes

There is really no point in using a JSON unless you intend on sharing the file with other applications. Just make a new script and put

class_name = LevelInfo #or whatever you want to call it

const levels := {
"level1" : {ALL THE INFO}

Then you can access the info from any script by doing LevelInfo.levels.get("level1")

by (616 points)
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 Frequently asked questions and 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 [email protected] with your username.