So the hardest part might be creating and storing all these items. However, the BinaryFormatter gives you an easy way to serialize Data of derived classes. You can create a method to write out all your items as Item, and read them back in. However, if an Item is actually a MaceWeapon, it will store all that data. When you read it back in, it will read it back in as a MaceWeapon even if you’ve cast it as Item.
Here is some Write and Read methods. Probably best to make these static in some script somewhere:
private void WriteItemToFile<T>(string path, T item)
{
// create a new formatter instance
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter =
new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
// open a filestream
using (FileStream stream = new FileStream(path, FileMode.Create, FileAccess.Write))
{
formatter.Serialize(stream, item);
stream.Close();
}
}
private T ReadItemFromFile<T>(string path, ref long position)
{
// create a new formatter instance
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter =
new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
// read the item as position back
T item = default(T);
using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
if (position < stream.Length)
{
stream.Seek(position, SeekOrigin.Begin);
item = (T)formatter.Deserialize(stream);
position = stream.Position;
}
}
return item;
}
Now to actually use these here is a sample code, but not necessarily how you’d want to do this:
MeleeWeapon weapon;
void Awake()
{
MeleeInit meleeData = new MeleeInit();
meleeData.twohanded = false;
meleeData.numberDice = 3;
meleeData.typeDice = 7;
meleeData.flatDamage = 5;
ItemInit data = new ItemInit();
data.name = "sword";
data.type = ItemType.sword;
data.rarity = ItemRarity.common;
data.value = 10;
Type type = Type.GetType("MeleeWeapon");
weapon = new MeleeWeapon(meleeData, data);
WriteItemToFile<Item>(Path.Combine(Application.dataPath, "ItemList"),weapon);
long position = 0;
Item testItem = ReadItemFromFile<Item>(Path.Combine(Application.dataPath, "ItemList"),ref position);
IMeleeWeapon testWeapon = testItem as IMeleeWeapon;
if (testItem is MeleeWeapon)
Debug.Log("its a melee weapon");
if (testItem is MaceWeapon)
Debug.Log("its a mace weapon");
Debug.Log(testWeapon.AutoAttack(gameObject));
}
Ideally what you would do. Is once you’ve written up all your Interfaces, and made all your classes, for example you might have something like this:
public class Potion: Item, IDrinkable
{
}
You will need to create a separate Item creation program. This is probably easiest done in .net on Visual Studio… but you could use Unity or whatever you like. This program will have all your Interface and Item Class definitions. Then you can create a series of buttons/dropdowns whatever you like to create a new item. And of course InputFields to input the correct data. You’ll have to play around a bit with a few items, to get the hang of how to read in data, and store it, without overwriting data you’ve already done. Once you’ve got that done, you Item File can just be read by your main Game program with the ReadFunction. Then you just read in all the items your game will have. You can sort them into Lists based on class type if you’d like (using if (item is ClassType). Or whatever structure you need to handle all the potential items in the game.
Note: In the Writer Method I listed, it has FileMode.Create. This means it will create a brand new file everytime its called. You’ll probably want to use this sometimes if your just going to dump the full data into the file and want to override changes you’ve made live into the file. But if your just adding new data, you can change it to FileMode.Append. Maybe add a bool flag to the function to switch between types.
In general when working with serialized data, its fairly difficult to seek to a specific item and load just it. (thought its possible). Unless your working with Tens of thousands of items, you item creation program would work easiest just loading every Item you’ve created. Then its easy to create UI elements to search through existing items as well as create new ones. When your done just save the entire thing back out into the file with Create mode
Last note: as you may have started to realize, a good inventory system for an RPG game can be a lot of work if you want to do it right. It will be a major chunk of the time you spend on the game. Once you get the actual items sorted out and how your game will handle them, the original question (sorting them into tabs) will be cinch.