#Json Character info not loading Help please:(

97 messages · Page 1 of 1 (latest)

soft gazelle
#

Just checking, you have a Data folder with character_info.json in it?

soft gazelle
#

Actually, I don't see where your storing the data in your json loading script

soft gazelle
#

So your calling load_data, but all that does is return the data

#

Also helps if you copy and paste the code

slender silo
#

oh okay ill do that

#

@export var character_name: String = ""  # Ensure this variable is declared and settable in the Inspector

var character_info = {}
var json_data

func _ready():
    json_data = load("res://Scripts/JsonData1.gd").new()
    if json_data.load_data("res://Data/character_info.json"):
        character_info = json_data.data
        print(character_info)  # Debug print to ensure the data is loaded correctly
        show_character_info("Character0")  # Show initial character info
    else:
        print("Failed to load character info")

func _process(_delta):
    match Game.PlayerSelect:
        0:
            get_node("PlayerSelect").play("Character0")
        1:
            get_node("PlayerSelect").play("Character1")
        2:
            get_node("PlayerSelect").play("Character2")
        3:
            get_node("PlayerSelect").play("Character3")
        4:
            get_node("PlayerSelect").play("Character4")
        5:
            get_node("PlayerSelect").play("Character5")

func show_character_info(character_id):
    print("Character ID: ", character_id)
    print("Available keys: ", character_info.keys())
    if character_id in character_info:
        var info = character_info[character_id]
        var info_text = "Name: %s\nDescription: %s\nStrength: %d\nDexterity: %d\nIntelligence: %d" % [info["name"], info["description"], info["stats"]["strength"], info["stats"]["dexterity"], info["stats"]["intelligence"]]
        get_node("CharacterInfoLabel").text = info_text
    else:
        get_node("CharacterInfoLabel").text = "Character info not found"

func _on_left_pressed():
    if Game.PlayerSelect > 0:
        Game.PlayerSelect -= 1
        show_character_info("Character%d" % Game.PlayerSelect)

func _on_right_pressed():
    if Game.PlayerSelect < 5:
        Game.PlayerSelect += 1
        show_character_info("Character%d" % Game.PlayerSelect)```
soft gazelle
#

Wrap it in tilde characters `

#

Put 3 ` at the start

#
then it gets formatted
#

if json_data.load_data("res://Data/character_info.json"):
So that's calling load_data, load_data isn't returning true or false, it's returning your character data

#

At least that's what it looks like

slender silo
#

did i do that right

soft gazelle
#

You can wrap a body of text in 3 `s
Eg:
```
foo
bar
```

#

Perfect

slender silo
#

🙂

soft gazelle
#

So yeah, your load_data function is returning the data

slender silo
#

so how do I fix it

#

well let me show u this first

#

this is a json for an inventory script that loads correct

soft gazelle
#
func _ready():
    character_info = json_data.load_data("res://Data/character_info.json")
    print(character_info)  # Debug print to ensure the data is loaded correctly
    show_character_info("Character0")  # Show initial character info
slender silo
#

and this is the json script connected to my character selection

soft gazelle
#

Try that

slender silo
#

ok

#

i get this error, not sure what that one means

#

Nil

soft gazelle
#

Errr

#

one moment...

#
func _ready():
    json_data = load("res://Scripts/JsonData1.gd").new()
    character_info = json_data.load_data("res://Data/character_info.json")
    print(character_info)  # Debug print to ensure the data is loaded correctly
    show_character_info("Character0")  # Show initial character info
#

Forgot to load the script

slender silo
soft gazelle
#

What does the print statement output?

slender silo
#

thats the gray writing at bottom right

soft gazelle
#

yep

slender silo
#

does that mean the name Character is not in the json

soft gazelle
#

Ignore that for now

slender silo
#

ok

soft gazelle
#

Can you paste in the JsonData1.gd script?

slender silo
#

var item_data: Dictionary

func _ready():
    item_data = load_data("res://Data/character_info.json")

func load_data(file_path: String):
    var file = FileAccess.open(file_path, FileAccess.READ)
    if file:
        var json_text = file.get_as_text()
        var json_parser = JSON.new()
        json_parser.parse(json_text)
        var json_data = json_parser.get_data()
        file.close()
        if json_data.has("Character"): # Assuming your character data is under the key "Character"
            return json_data["Character"]
        else:
            print("Error: 'Character' key not found in JSON")
            return {} # Return an empty dictionary on error
    else:
        print("Error: Failed to open file: ", file_path)
        return {} # Return an empty dictionary on error```
soft gazelle
#

Ah

#

if json_data.has("Character"): # Assuming your character data is under the key "Character"

#

That's looking for a json key called Character

#

But your json just has: Character0 Character1 etc

slender silo
#

so do I start labelling them from character, character1 etc

#

or just change the script to character0

soft gazelle
#

That won't work either

#

json_data.has("Character") is only going to return true if your json has a key called Character in it

#

And then the next line (return json_data["Character"]), is only going to return the value for the key Character

#

You want to return all Characters right?

slender silo
#

yeah

#

so they each have an individual description when theyre scrolled to using the buttons o:

soft gazelle
#

Ok, paste your json

slender silo
#
    "Character0": {
        "name": "Warrior",
        "description": "A strong and brave warrior.",
        "stats": {
            "strength": 10,
            "dexterity": 5,
            "intelligence": 3
        }
    },
    "Character1": {
        "name": "Mage",
        "description": "A wise and powerful mage.",
        "stats": {
            "strength": 3,
            "dexterity": 4,
            "intelligence": 10
        }
    },
    "Character2": {
        "name": "Rogue",
        "description": "A swift and cunning rogue.",
        "stats": {
            "strength": 6,
            "dexterity": 9,
            "intelligence": 5
        }
    },
    "Character3": {
        "name": "Archer",
        "description": "A precise and agile archer.",
        "stats": {
            "strength": 7,
            "dexterity": 8,
            "intelligence": 4
        }
    },
    "Character4": {
        "name": "Paladin",
        "description": "A holy and resilient paladin.",
        "stats": {
            "strength": 9,
            "dexterity": 6,
            "intelligence": 5
        }
    },
    "Character5": {
        "name": "Necromancer",
        "description": "A dark and sinister necromancer.",
        "stats": {
            "strength": 4,
            "dexterity": 5,
            "intelligence": 10
        }
    }
}```
soft gazelle
#
{
    "Character": [
        {
            "name": "Warrior",
            "description": "A strong and brave warrior.",
            "stats": {
                "strength": 10,
                "dexterity": 5,
                "intelligence": 3
            }
        },
        {
            "name": "Mage",
            "description": "A wise and powerful mage.",
            "stats": {
                "strength": 3,
                "dexterity": 4,
                "intelligence": 10
            }
        },
        {
            "name": "Rogue",
            "description": "A swift and cunning rogue.",
            "stats": {
                "strength": 6,
                "dexterity": 9,
                "intelligence": 5
            }
        },
        {
            "name": "Archer",
            "description": "A precise and agile archer.",
            "stats": {
                "strength": 7,
                "dexterity": 8,
                "intelligence": 4
            }
        },
        {
            "name": "Paladin",
            "description": "A holy and resilient paladin.",
            "stats": {
                "strength": 9,
                "dexterity": 6,
                "intelligence": 5
            }
        },
        {
            "name": "Necromancer",
            "description": "A dark and sinister necromancer.",
            "stats": {
                "strength": 4,
                "dexterity": 5,
                "intelligence": 10
            }
        }
    ]
}
#

wait

slender silo
#

i just learned abt json yesterday i didnt even know u could use notepad this way o:

soft gazelle
#

Now there is a single key called Character it's value is an array containing all your characters

slender silo
soft gazelle
#

Yeah, but now your json is returning correctly

slender silo
#

oh okay

soft gazelle
#

So you can check this by using the debugger and breakpoints

slender silo
#

this is so confusing cuz i had another json that worked and each item was labelled

#

and didnt have a key at the top

soft gazelle
#

Right, but you were probably loading that differently

slender silo
#

oh

soft gazelle
#

If you want to change it back to Characater0 etc, you need to change how you are loading it

#

Which would look like this...

#
extends Node

var item_data: Dictionary

func _ready():
    item_data = load_data("res://Data/character_info.json")

func load_data(file_path: String):
    var file = FileAccess.open(file_path, FileAccess.READ)
    if file:
        var json_text = file.get_as_text()
        var json_parser = JSON.new()
        json_parser.parse(json_text)
        var json_data = json_parser.get_data()
        file.close()
        return json_data
    else:
        print("Error: Failed to open file: ", file_path)
        return {} # Return an empty dictionary on error
#

That will return all of json

slender silo
#

which way is better

soft gazelle
#

Whichever way works lol

slender silo
#

okay

#

what should I do about this

soft gazelle
#

Change your json back to the way you had it originally

#

And then use the json loader I pasted above

#

Good luck

slender silo
#

uhh

#

that made the entire thing red ;3;

soft gazelle
#

Godot doesn't like mixed tabs/spaces

slender silo
#

oh ok

#

hmmm

#

what does this mean now

soft gazelle
#

Do you stil have var character_info = {} at the top of the file?

slender silo
#

yeah i just fixed that

#

but now im back here

#

fixed that actually

#

but now

soft gazelle
#

lol

slender silo
#

;3;

soft gazelle
#

put a breakpoint on line 40

#

Sorry, that's all the time I have

slender silo
#

ok ;3;