- Home /
How to create a variable, that can take instances of different classes?
I got class Weapon and class Shield (both inherited from Item class)
public class Weapon : Item
{
public int Damage;
public int Range;
}
public class Shield : Item
{
public int DamageAbsorb;
}
And I wand Unit to have variable RightHandItem and LeftHandItem, so that he can take weapon and a shield, two weapons (or even two shields)
How should I do that, as I can't define RightHandItem as Weapon (because then he won't be able to take a shield), as well as I can't define it as Shield ((because then he won't be able to take two weapons). Also I can't define it as Item, because then I won't be able to use Damege or DamageAbsorb properties.
So basically, I don't know beforehead, whether it will be Weapon or Shield, what should I do?
Answer by JVene · Aug 04, 2018 at 02:13 AM
When you say, " Also I can't define it as Item, because then I won't be able to use Damege or DamageAbsorb properties", that isn't true. You can.
Since both Weapon and Shield are Items, the combination of all public (and protected ) parent variables and methods are available to these derived classes. Within the methods of Weapon, for example, any public member of Item is just as available as Damage and Range.
What might puzzle you is what to do when what you have is a reference to an Item, without knowing if that item is a Weapon or a Shield. From that perspective, the perspective of the Item, you don't have immediate access to the members specific to Shields or Weapons. There are many answers, but among them you should resist any tendency to try to give Item knowledge of Weapons or Shields. That violates the notion of making Item a base class that operates only that which is appropriate to both Weapons and Shields, which is, in fact, handedness.
So, if you want to act as a Shield from within a method of Item, use an abstract method in Item that provides a specific override in Shield and Weapon. Look into abstract classes and methods to learn how, it is the magic you're looking for.
You can resort to casting, but that's considered naive and generally ignores what abstract methods do for you.
But then I will get basically Item class, that will have all Damage, Range and DamageAbsorb properties. So I will use unnessesary memory. In this case some Sword will have DamageAbsorb property, that will neve be used.
You misunderstand.
An item that has only bool RightHanded does not combine weapons and shields.
A Shield, derived from Item, only has DamageAbsorb and RightHanded (inherited from Item).
A Weapon, derived from Item, only has Damage, Range and RightHanded (inherited from Item).
$$anonymous$$aybe this shold be a reply to another thread?
P.S.: Really thanks for trying to help me. But I still don't understand. Can you wrie an example of code for Item, Weapon and Shield and a line, where I can take access to RightHand Item, like this:
public class Item
{
...
...
}
public class Weapon : Item
{
public int Damage;
public int Range;
}
public class Shield : Item
{
public int DamageAbsorb;
}
...
public ??? RightHandItem;//Item?
Something = Unit.RightHandItem.Damage;//<--How should I get access to this???
Answer by Cynikal · Aug 04, 2018 at 02:07 AM
Honestly, what I would personally do is:
Make a single class for the weapons/shields. Call it something along the lines of "WeaponShield" for something simple.
Then like on a void OnPickUp, pull to see what it is from the code. Is it a weapon? or is it a shield?
If it's a shield, my range would be 0, and my damage would be lower and be 'blunt' damage for example.
I'd add the fields "ShieldHealth", which obviously would be 0 for a weapon, but would be used for the shield.
Then, i'd private WeaponShield _rightHand; private WeaponShield _leftHand;
i'd be using enums to set it as a shield or a sword...bow..whatever.
As, you're going to want to put this class with the stats of the item on the game object.
I can do that, but then some Sword will have DamageAbsorb property, that will neve be used. And I'm trying to avoid this.
No, it won't.
A Shield, derived from Item, looks like this:
public bool RightHanded; // inherited from Item
public int DamageAbsorb;
And nothing else, while a Weapon looks like this:
public bool RightHanded:// inherited from item
public int Damage;
public int Range;
And nothing else.
There is nothing that combines or confuses a Weapon and a Shield as you are thinking.
So RightHandItem should be wich class?
Item?
Then how should I get access to Unit.RightHandItem.Damage for example?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Creating class instances 1 Answer
Accessing class variables from a definition 1 Answer
Get list of all "Action" classes 1 Answer