#Json Character info not loading Help please:(
97 messages · Page 1 of 1 (latest)
yep thats correct
Actually, I don't see where your storing the data in your json loading script
So your calling load_data, but all that does is return the data
Also helps if you copy and paste the code
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)```
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
did i do that right
🙂
So yeah, your load_data function is returning the data
so how do I fix it
well let me show u this first
this is a json for an inventory script that loads correct
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
and this is the json script connected to my character selection
Try that
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
;3;
What does the print statement output?
yep
does that mean the name Character is not in the json
Ignore that for now
ok
Can you paste in the JsonData1.gd script?
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```
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
so do I start labelling them from character, character1 etc
or just change the script to character0
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?
yeah
so they each have an individual description when theyre scrolled to using the buttons o:
Ok, paste your json
"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
}
}
}```
{
"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
i just learned abt json yesterday i didnt even know u could use notepad this way o:
Now there is a single key called Character it's value is an array containing all your characters
Yeah, but now your json is returning correctly
oh okay
So you can check this by using the debugger and breakpoints
this is so confusing cuz i had another json that worked and each item was labelled
and didnt have a key at the top
Right, but you were probably loading that differently
oh
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
which way is better
Whichever way works lol
Change your json back to the way you had it originally
And then use the json loader I pasted above
Good luck
Godot doesn't like mixed tabs/spaces
Do you stil have var character_info = {} at the top of the file?
lol
;3;
ok ;3;