I’m making simple inventory system. I got a class for an Item
[System.Serializable]
public class Item
{
public string itemName;
public int itemId;
//some more variables
public Effect itemEffect;
//some more code
public void Use(Entity x)
{
itemEffect.UseEffect(x);
}
}
All the items are stored in the list inside ScriptableObject with a custom editor.
The best way of implementing item effect I came up with is this. Every item has method “Use” which is calling “UseEffect” method of itemEffect variable which is an instance of Effect class
This is the Effect class.
[Serializable]
public class Effect
{
public string effectName;
public Effect ()
{
this.effectName = "BaseEffect";
}
public virtual void UseEffect (SomeParameter x)
{
Debug.Log("This is base effect"+ x.Something.ToString());
}
}
This is my Test Effect derived from Effect class
public class TestEffect: Effect
{
public TestEffect()
{
this.effectName = "TestEffect";
}
public override void UseEffect(SomeParameter x)
{
Debug.Log("This is TEST EFFECT");
x.something = x.something+1;
Debug.Log(x.something.ToStirng());
}
}
This is how i assign TestEffect object to Item variable i custom editor.
if (GUI.Button(//someRect,"Apply test effect to test item"))
{
//Item_Database_Asset is the ScriptableObject with Items_Database_List of all items
Item_Database_Asset.Items_Database_List[1].itemEffect = new TestEffect();;
EditorUtility.SetDirty(Item_Database_Asset);
}
So the problem is after i click “Apply test effect to test item” when i press play and using this item it’s calling UseEffect of BaseEffect. Although in inspector i see that item[1] itemEffect.effectName is TestEffect. So is basically working but it’s still calling BaseEffect.
But if i click “Apply test effect to test item” on runtime in play mode it works fine.
It’s a bit long and convoluted.I hope it’s readable.
Thank you.