I am I handling data correctly? (from json to singleton)

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

So basically to keep this readable and understandable. I have a JSON file with all of my item data. The items are dictionaries, and they are saved in an array inside a JSON file.
So JSON contains an array of dictionaries. Which I load up into my singleton that holds this data in a variable (it’s passed by reference so I use it across my game and change the values of the stuff the amount of items and such), and after loading i split that array(not really split just append to different arrays items based on rarity). And when the game exists I save variable that held the items back into JSON. It seemed like a great solution, but now thinking about is, is having a variable with 100 dictionaries a smart idea? It works great but idk how performant is it.

So array of dictionaries inside JSON → loaded up into a variable called inventory → from here another singleton splits the items based on rarity into different arrays (common rare epic etc). I use these arrays for my drop system, reward system, inventory and so on.

 func seperate_items():
for item in all_items:
	match(item["type"]):
		"plush":
			all_plush_items.append(item)
		"wood":
			all_wood_items.append(item)
		"plastic":
			all_plastic_items.append(item)
	match(item["rarity"]):
		"common":
			common.append(item)
		"uncommon":
			uncommon.append(item)
		"rare":
			rare.append(item)
		"epic":
			epic.append(item)

all_items is a variable that is set equal to the actual variable that holds all values. (so it should be pointing to the same thing)

:bust_in_silhouette: Reply From: nonchip

so your question boils down to “is a JSON with lots of things performant”? that can only really be answered by testing it, but it’s probably gonna be just fine for your number of things. i doubt you want to be reinventing the wheel here unless you have to.

then again, i don’t really get why you’re sorting them into various boxes to begin with? do you need them to be split up like that? most often an inventory is just a single list of things (which can then be ordered according to how the player wants it, or maybe auto sorted according to stuff like rarity, price, etc while looking at it), so why not just store and reload that thing as-is?

I know that JSON’s are pretty performant. I was just unsure of having them in a variable was the way to go. I am splitting them up for various calculations for drops/ selling and stuff. (this isnt exactly inventory but all possible items as well)

Nikola-Milovic | 2020-04-24 17:22