I have a base class Item, from which I have a Weapon class derived.
The weapon class has a variable _isEquipped, however when I loop through my list I can’t access and query this. What is the correct way of doing this? What I currently do is simply:
for (int i = 0; i < itemList.Count; i++)
{
if (someVar == someOtherVar)
{
itemList[i]._isEquipped = true //Throws error CS1061: Type BaseItem does not contain a definition for '_isEquipped'...
}
}
Also, the variables in the derived classes are not exposed in the inspector. Is there also a way around this?
Unfortunately no. Unity’s serilisation system, which drives the inspector, does not support polymorphism.
You can always attempt to cast the base type to the derived type. But this is generally considered a bad idea. As a general rule a base class should have no knowledge of the classes that derive from it.
Consider what happens if itemList contains something that is not a BaseWeapon*.* In this case you will get an error. So you need to guard against this.
Why put the if check inside of the loop? Do it before the loop as an early out before you waste any cycles going through a loop that does nothing.
As lordofduct pointed out, if you expect the list to always contain BaseWeapons, make the list a list of BaseWeapons.
Otherwise, a safer way is to do this:
for (int i = 0; i < itemList.Count; ++i)
{
var eqpStatus = false;
var weapon = itemList[i] as BaseWeapon;
if (weapon != null)
eqpStatus = weapon.IsEquipped;
Debug.Log("Equipped: " + eqpStatus);
}
i.e. Check that weapon is not null before using it.
Out of sheer curiosity, is this code objectively any better or worse, or just different?
for (int i = 0; i < itemList.Count; ++i)
{
var eqpStatus = false;
if (itemList[i] is BaseWeapon){
eqpStatus = ((BaseWeapon)itemList[i]).IsEquipped;
}
Debug.Log("Equipped: " + eqpStatus);
}
In my own code that’s exactly what I’ve done. Just typed an example up quickly on here.
Thanks for all of the advice. I don’t null check enough, something I need to concentrate on a lot more and get into the habit of doing. I like the last example you gave @BlackPete , and it will more than likely be what I use as my list will contain many more classes than just weapons.