I have a server set up for my game as it will be online. On registration, the server creates 15 objects (think something like “trading cards” with a lot of stats and info) and sends them to the client. I can retrieve the data just fine, tested that by printing to console.
What I can’t figure out is how I would store all that information In-game to be able to use it in methods, etc.
All objects have about 30 different stats each which come as a JSON from the server. The key is a string but the values are either strings or integers.
I need to have 15 different objects In-game to use them in various operations. They need to have the ability to be referred to separately. The user will always only have 15 objects. Not more, not less. The stats of each will obviously change with game play, but the number of objects and properties will always be the same.
I considered dictionaries, but the different data type for values kinda put a lid on that. I’m now in the process of considering 15 classes, but I’m worried about the performance impact of 15 different classes with ~30 variables each. I could also create one class since all the keys are the same across all objects, and instantiate that class 15 times with 15 different names. But because I’m still beginner-intermediate, I’m not quite sure where I would instantiate them (the object class or the class that has the method retrieving the server data…or other?).
What would be the best way forward with this. I don’t necessarily need to be spoon-fed the answer. References will also do just fine if you don’t have the time.
Do you have a sample of the JSON you retrieve from the server? I guess that in your case inheritance might be a good way to go, but I’d need to see what data you are working with exactly to be sure.
Here’s a sample of one card JSON format. 15 of these get sent from the server after the user succesfully register. Then they can use them in game, sell them, upgrade, etc.
It’s not 15 classes. It’s 1 class that will have 15 instances. And it hardly amounts to anything in the grand scheme of things.
I would define a card class, and make the json be a list of 15 objects that deserialize into a list of these instances. I would give the fields more meaningful names and make Physical, etc be either sub-objects or lists.
Not a bad idea. I’ll start to put together something and see how it comes up. On paper they all have meaningful names, but that was the prototype card I copy-pasted as it was already open in the background and saved me having to connect to the server.
As a side note, how would I get the Physical,etc as sub-objects?
I can’t help but point out that on a modern machine you could deserialise these on the fly from the raw data strings every time you accessed it and still not notice a performance dip.
Build this as @andymads suggested. But remember in most cases developer performance is more important then software performance.