Having a bit of difficulty with my modified implementation of Inventory Master from the asset store. I decided to try and mix a little inheritance into the process, and everything went well right up until I noticed that the save data (particularly the list data) doesn’t seem to “stick” after play is pressed or Unity restarted. There’s some sort of serialization problem that I just can’t figure out. Here’s how I’ve set things up:
First, I have my item classes, abstract base class “item” with all sorts of little bits and pieces, and several classes that derive from it with one or two additions each.
namespace Lysander.Items
{
[System.Serializable]
public abstract class Item
{
public string itemName;
public int itemID;
//ETC
}
[System.Serializable]
public class Weapon : Item
{
public WeaponType weaponType = WeaponType.None;
//ETC
}
//
//ETC
//
}
You get the idea. I then have my scriptable object class, which looks something like this (you’ll notice the list data is for the abstract base class “item”, and not the derived classes that actually get held in the list- this may be relevant to the problem):
namespace Lysander.Items
{
public class ItemDatabaseList : ScriptableObject
{
[SerializeField]
public List<Item> itemList = new List<Item>();
[SerializeField]
public string itemType; //assembly qualified name of the derived type of "item" the list holds
//ETC
}
}
And the database creator, which uses generics both so that I don’t have to make 10 of them and so that I can cache the databases to keep more than one of each from existing:
namespace Lysander.Items
{
public class CreateItemDatabase
{
[SerializeField]
public static List<ItemDatabaseList> assets = new List<ItemDatabaseList>();
#if UNITY_EDITOR
public static ItemDatabaseList createItemDatabase<T>() where T : Item
{
ItemDatabaseList newList = assets.Find(t => t.itemType == typeof(T).AssemblyQualifiedName);
if (newList != null)
return newList;
newList = ScriptableObject.CreateInstance<ItemDatabaseList>();
newList.itemType = typeof(T).AssemblyQualifiedName;
assets.Add(newList);
AssetDatabase.CreateAsset(newList, string.Format("Assets/_TestCharacter/InventorySystem/Items/Databases/Resources/{0}ItemDatabase.asset", typeof(T).Name));
AssetDatabase.SaveAssets();
return newList;
}
#endif
}
}
Now, finally, the relevant portion of the editor script where I load and utilize the various databases.
namespace Lysander.Items
{
public class Lys_Manager : EditorWindow
{
[MenuItem("Lysander/Inventory Manager")]
static void Init()
{
EditorWindow.GetWindow(typeof(Lys_Manager));
AllMyBase = new List<DatabaseDetails>();
{ //FOR RESOURCE LIST
Object ItemDatabase = Resources.Load<ItemDatabaseList>("ResourceItemDatabase");
if (ItemDatabase == null)
AllMyBase.Add(new DatabaseDetails(CreateItemDatabase.createItemDatabase<Resource>()));
else
AllMyBase.Add(new DatabaseDetails(ItemDatabase as ItemDatabaseList));
AllMyBase[0].toolbarLabels = new string[] { "Create Resource", "Manage Resources" };
}
{ //FOR CONSUMABLE LIST
Object ItemDatabase = Resources.Load<ItemDatabaseList>("ConsumableItemDatabase");
if (ItemDatabase == null)
AllMyBase.Add(new DatabaseDetails(CreateItemDatabase.createItemDatabase<Consumable>()));
else
AllMyBase.Add(new DatabaseDetails(ItemDatabase as ItemDatabaseList));
AllMyBase[1].toolbarLabels = new string[] { "Create Consumable", "Manage Consumable" };
}
//
//ETC
//
foreach (DatabaseDetails details in AllMyBase)
{
//RESET MEMBERS
}
}
[System.Serializable]
public class DatabaseDetails
{
[SerializeField]
public ItemDatabaseList database;
[SerializeField]
public bool showDatabase;
[SerializeField]
public int toolbarSelected;
//
//ETC
//
public DatabaseDetails(ItemDatabaseList database)
{
this.database = database;
}
}
//
//ETC
//
}
}
I’m sincerely hoping that my problem will be instantly (and stupidly) apparent to one of you out there. I’m not getting any errors or anything- it’s just a serialization issue as far as I can tell, but I can’t tell where the problem is.
