@Sangemdoko
Hi, I'm still working on my CSV import stuff, I'm almost done. When I figure it out, I'll post the whole solution in the thread.
For now, I'm encountering another problem:
I created a subclass named ModernItem, it has a static method to create ModernItem type of Item.
I did it for 2 reasons:
0. The ModernItem is created from a CSV file at runtime, then it is stored in a ScriptableObject.
1. I don't want to mess with the Item class (which is part of the framework).
2. I need the subclass type to distinguish it from other Items (e.g: BookItem, FoodItem).
But when I tried to call InventorySystemManager.CreateItem for one of stored ModernItem, it actually creates an Item class.
As you can see, the method only return Item type, how do I make it generic (to return subclass type)?
I really don't want to mess with the InventoryManager class (or related) unless there's no other way.
Below is the code of ModernItem class.
public class ModernItem : Item {
public static T Create<T>(string type) where T : ModernItem, new() {
var itemDefinition = InventorySystemManager.GetItemDefinition(type);
if (itemDefinition == null) {
Debug.LogWarning("Cannot create Item with null Item Definition.");
return null;
}
var manager = itemDefinition.Manager;
//If the category is immutable check if item alreadyExists in Inventory System Manager.
if (itemDefinition.IsMutable == false && itemDefinition.IsUnique == false && manager != null) {
var registeredItem = manager.Register.ItemRegister.FindItemWithOverrides(itemDefinition, null);
if (registeredItem != null) { return registeredItem as T; }
}
//Construct the new Item.
var createdItem = new T();
createdItem.m_ItemDefinition = itemDefinition;
createdItem.m_ItemDefinitionID = itemDefinition?.ID ?? 0;
createdItem.m_Name = itemDefinition?.name ?? "NULL";
//Initialize does not register because register may need to replace this item by an existing equivalent one if the item is immutable.
createdItem.Initialize(true);
//Register (item can be replaced by an existing registered item).
if (InterfaceUtility.IsNotNull(manager)) {
var item = createdItem as Item;
manager.Register.ItemRegister.Register(ref item);
}
return createdItem;
}
public string DisplayName;
public Attribute<T> GetAttr<T>(string name) {
var attr = GetAttribute<Attribute<T>>(name);
return attr;
}
public T TryGetAttr<T>(string attr) {
if (TryGetAttributeValue(attr, out T value)) {
return value;
}
return default(T);
}
public override void Initialize(bool force, bool updateAttributes = true) {
if (m_ItemObjects == null) {
m_ItemObjects = new ResizableArray<ItemObject>();
}
base.Initialize(force, updateAttributes);
}
}