Optimal use of .json files for characters data sheet

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Rensae

Hi, I’m still doing my multiplayer game, and I seek your advices for how to optimally use json files to retrieve characters skills (or maybe some other things, since I’m new to this I welcome every help). I’m not a total newbie, but there’s still a lot of things I don’t know since I’m not a full time developper.

I’ll have (for now) 2 characters which have their own individual skills, animations, sounds, passives etc…

Each character will have 6 skills, and 4-9 more when under certain conditions, but also 1 to 3 passive skills.

I started by making a sheet data on google sheet that I’ll export as a .json file.

I’ll have 4 types of skills : SingleTarget ranged and Melee, and AOE Ranged and Melee

I suppose it’s not possible to make passive into a .json files as it’ll modify variable inside the player script itself. (But if it’s possible, I’d like to hear how to do it !!)

If I understood correctly every things I read from a lot of sources, the sheet data should look like that :

First of all, I’d like to know if it’ll be good enough.

Second, I’d like to know if anyone knew how to use the name of the node for when it’s instanced on func _ready():, retrieve the data on a dictionnary / a .json file ?

Like, Player spawn, how do I use “Player” in, for exemple :

Singleton.variable[self.name]["subvalue to retrieve"]

Since self.name doesn’t work, I don’t know how to do it.

I’d like to use that for when the player choose a character on a lobby, I store the name of the character after they hit a Confirm button on a variable on a Singleton, so that when “Player1” is instanced, it’ll take the texture, animation, skill data sheet and everyhing else from the dictionnary “Player1”. Then it’ll do the same with “Player2”

Thanks in advance to everyone who’ll want to help me, even if it isn’t that simple (at least in my eyes)

:bust_in_silhouette: Reply From: r.bailey

Just some general things I have figured out while using the built in Json class, is that it expects data to be a certain way in the Json to be able to parse it. My assumption is this is the issue you are having with the loading it. I will post a possible implementation that shows you how to read from a file that might lead you in the right direction.

Singleton class Named : JsonLoader

extends Node

var json_data: Dictionary

func _ready():
	var found = ReadJsonFile()
	if(found):
		ParseJson()

func ReadJsonFile():
	var json_file = File.new()
      if not json_file.file_exists("res:/thejsonfile.json"):
     #this is if you put the json file in the user path, look it up if you don't know what 
     #this means. 
	 #if not json_file.file_exists("user:/thejsonfile.json"):
		print("Can't Find the .json file")
		return false
	#json_file.open("user:/thejsonfile.json", json_file.READ)
    json_file.open("res:/thejsonfile.json", json_file.READ)
	var data_json = JSON.parse(json_file.get_as_text())
	json_file.close()
	json_data = data_json.result
	return true

func ParseJson() -> void:
	if JsonLoader.json_data.keys() != null:
		for i in JsonLoader.json_data.keys():
				var skill_name = JsonLoader.json_data[i]["SkillName"]
				var skill_type = JsonLoader.json_data[i]["SkillType"]
				#etc, and whatever else you want to load. and do whatever with it. 

How the Json should look with this code, of course you can add more fields and what not to it. If you are using some excel type program to create all these with first column should be keys(and just autoincrement them so they are unique numbers). Something like this should be what it outputs like to be used with the code above.
thejsonfile.json

{
	"1": {
      "SkillName": "Blah",
      "SkillType": "BlahType"
    },
	"2": {
      "SkillName": "Blah2",
      "SkillType": "BlahType2"
    },
    "3": {
       "SkillName": "Blah3",
        "SkillType": "BlahType3"
      }
}

Hope this helps.

Thanks, it helped me understand better how to use a json file !

I’ll just need to figure a way out of using the name of the node which want to retrieve the data and it should work now. Thanks a bunch for explaining that to me !

I’ll ask again if there’s something I don’t understand while doing so !

Rensae | 2021-09-30 00:35