0 votes

I've been trying to make a clicker game that has survival and fighting mechanics. I store data about the enemies in a JSON file like this:

{
  "0": {
    "Name": "Tree",
    "Area": "Tree",
    "AmountKilled": 0,
    "Damage": 0,
    "BaseHP": 100,
    "Fatigue": 20,
    "DroppedItem": "Wood"
  }
}

What i tried to do in a script is get the name and dropped item of the enemy by having:

var EnemyId = 0
var EnemyName = JsonLoader.EnemyData[str(EnemyId)]["Name"]
var DroppedItem = JsonLoader.EnemyData[str(EnemyId)]["DroppedItem"]

this however gives "Invalid get index '0' (on base: 'Dictionary')." as an error when i try to run it.

Here's the JsonLoader too in case it's causing the problem:

extends Node

var EnemyData: Dictionary

func _ready():
    EnemyData = LoadData("res://Data/EnemyData.json")
    print(EnemyData)

func LoadData(FilePath):
    var JSONData
    var FileData = File.new()

    FileData.open(FilePath, File.READ)
    JSONData = JSON.parse(FileData.get_as_text())
    FileData.close()
    return JSONData.result
Godot version v3.2.3
in Engine by (12 points)

1 Answer

0 votes

Usually an "invalid get index" means you're not calling the file correctly, which is often an issue with the filepath or node spelling.

So, it could be that in EnemyData = LoadData("res://Data/EnemyData.json") something is in the wrong folder, or that there's a misspelling somewhere in your code routing to the Dictionary - often something as simple as a capitalization error (Json for JSON, or vice versa).

Hope this helps!

by (127 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 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.