- Home /
Store game objects in a JRPG game
I've come up with this approach so far:
I have hundreds of items(let's say guns) which i store in a JSON file. Whenever the game needs to use them they are called. So my question is: Is this a good way to go? A JSON file with all the guns data? Or maybe I should serialize them and store them in the JSON file better? If you think there's a completely different and better approach I'd like to know it. I'm looking for best practise and most consistent way to do this.
I'd say it depends on what you are using the JSON file for.
If you're using this JSON file as a sort of non-editable "database" of every possible item type in the game, I would consider creating a Item
class with many sub-classes, each representing a different item. There are multiple ways to instantiate the individual gun classes, like having a method that returns a certain item instance based on an ID of some kind (use a big switch statement?), or you could maybe create instances based on the class name as a string. Whatever makes sense for your game and is more efficient.
If the JSON file represents the player's inventory or some collection of items (that isn't a complete list of every item in the game), you could create a serializable class that simply stores a list of items and serialize that, or simply continue to parse JSON data to load the collection. Both would work fine. (but it would be harder for a user to edit the serialized file)
That's the way I've done it so far. I have a baseItem class. Then i derive this class for the different types of guns, etc. And I store each gun's info in a JSON file. And, to save, for example the current inventory, i save the item's ID and then i retrieve the data and load it into the different type of classes at the end of the scene when needed.
But I'm not sure this is the proper way to go.
Thanks for your answer @Epicwolf.
If you have classes deriving from BaseItem that represent different item types, why do you need to store each gun type to a file? It seems to me that using a JSON file would be only necessary when loading saved character data (like you do with the saved IDs) or something similar. If you are saving the current values of each item's fields in the player's inventory to one file, and saving the IDs of the items in another file, (which it sounds like you are doing) serializing all the items in the inventory (or the inventory itself) would make loading data much simpler. From what a few quick Google searches and some Unity Answers questions tell me, this class-oriented approach (with serializing of items/inventories) seems to be the popular choice, and it sure seems to be the simplest.
I think i didn't explain myself really well, sorry. I have a baseItem class and then i derive it to weaponItem, ringItem, bootItem, etc. And from each of these general types, more types are derived. But, let's talk about weapons for example. I have 40 different swords, let's say. Each sword deffinition is stored in a JSON. weaponsJSON consists of different arrays one being all the swords deffinition for every sword in my game. And when i save data i save the ID and then when needed i instantiate every weapon filling the objects with JSON data called by ID. I don't use serialization since I've done this approach on saving just the id. Should I store the objects serialized ins$$anonymous$$d of just keeping track of the id? Thanks
Your answer

Follow this Question
Related Questions
is LitJson still best for storing Json data for unity? 0 Answers
Modifying Json file values 2 Answers
How To Separate Json Parameter Function to get values i need ? 0 Answers
Is there something wrong with Application.persistentDataPath (on Samsung s6 running Android 5.1.1) ? 1 Answer
Android App stopped asking permission for internal storage 0 Answers