Question about variable field and abstraction

Hey guys, I’ve a question about a certain implementation I want to attempt. I have an abstract item as such,

public abstract class AbstractItem : PunBehaviour {

    [Tooltip("Durability of the item. Destroys itself upon reaching 0 durability.")]
    [SerializeField]
    private int durability;

    public int Durability
    {
        get { return durability; }
    }

}

So any class that inherits from AbstractItem will have durability, so something like a weapon. But there are some items that shouldn’t have durability, such as a prop. Both prop and weapon inherits AbstractItem because of several functionalities that are shared. Structs and interfaces are not usable (structs can’t be shared with abstract? and interfaces do not allow fields) so besides putting durability in each class that uses it, is there a way to go about having durability available only to certain classes?

Thank you for any advice,
Andy

Well the easy answer is to remove durability from AbstractItem, and create two child classes of AbstractItem, one with durability, one without.

You could also let props have durability, either set to infinity or -1 and do a check for “breakable” or something.

1 Like

They don’t allow fields but they do allow properties. So you could make a breakable interface if you wanted

interface IBreakable
{
    int Durability { get; }

    void TakeDamage(int damage);
}
3 Likes

That is what I’m looking for! Thanks so much! :smile: