Questions regarding Items, Unity C#

Started by
4 comments, last by elmar1498 4 years, 11 months ago

So, i'm working on an Item system and I have a quick question in regards to Items.

I typically use a set of Json files to hold my data which is fine except for Items which can different variables as they do different things. The issue is that i'm using JSON.net which when deserializing a file will crash if the file doesn't match the structure it's trying to serialize. This means that if I want to define an item that affects speed I have to define several variables in that object that it's not even going to use making the whole thing a big mess.

My question is, is there a better file type to handle that data? or is there a better way to parse the JSON?

Kenshen112

Advertisement

If you have different classes for each different set of variables, you can just tell JSON.net to deserialize a different class.  What I do is put JSON files for each different class in a separate folder, loop over all files in each folder and have JSON.net deserialize using the type which is appropriate for every file in that folder.

Along those lines, I'm trying to make a basic consumables class. So I have things like HPbuffer speed buffer strength buffer etc it's quite the list 

If I wanted to make a haste consumable then I have to list health else wise JSON.Net will have an issue that's what I'm trying to avoid

Kenshen112

What about writing your own JSON implementation? I did this mostly for performance reasons and portability from C++ to C# but it just took me only 3 files and a small ammount of lines to achieve the goal.

The other alternative is using ScriptableObject and create a single file "Database" of items that you instanciate at runtime from the file directly

You could use a built-in solution to serialize data JSON.

This topic is closed to new replies.

Advertisement