Instantiation Of Abstract Classes
Hi Guys,
I'm a little confused on how to create instances based on my abstract class. My Inheritance structure looks like this:
Gun > Rifle > Specific Weapon
I've declared the abstract classes in a specific C# script (below). I want to be able to create specific guns at runtime with randomised statistics and then attach them to an array in my players inventory. I just wanted to know what the syntax for creating a Specific Weapon would be from inside a method for example a loot method.
Gun Class
public abstract class Gun
{
public string Name;
public int Strength;
public float DistanceFallOff;
public bool CanScope;
//public GameObject gunModel;
public abstract void Shoot();
}
Rifle Class
public abstract class Rifle : Gun
{
public int MaxAmmo;
public int CurrentAmmo;
public float FireRate;
public float ReloadRate;
public Rifle(string name, int maxAmmo, int strength, float fireRate, float reloadRate, bool canScope)
{
Name = name;
MaxAmmo = maxAmmo;
Strength = strength;
FireRate = fireRate;
ReloadRate = reloadRate;
CanScope = canScope;
CurrentAmmo = maxAmmo;
}
public IEnumerator Reload()
{
yield return new WaitForSeconds(ReloadRate);
CurrentAmmo = MaxAmmo;
}
}
Answer by A_Li_N · Dec 30, 2015 at 05:18 AM
The point of abstract classes is to give all derived classes the same starting point; So your first level is correct:
Gun is an abstract concept; it contains basic information that is the same for all types of guns; rifles, pistols, etc.
However, your Rifle being abstract is not overly correct, unless you never want an actual 'Rifle' instance.
Your stated structure says 'Specific Weapon', but I think you defined that too deep and are actually attempting to mean 'Rifle' has multiple instances. In this situation, you do not want Rifle to be abstract as you actually want instances of them to be created. The only reason to have Rifle be abstract is if you are always planning on instantiating derived classes (ie public class MySuperRifle : Rifle, public class TheLimpRifle : Rifle)
Changing Rifle from abstract doesn't mean it can't be anonymous or random; it just means you can create a Rifle directly, which is what your own answer shows you are wanting to do. It also doesn't inhibit you from inheriting further, meaning you can still do public class MyUniqueRifle : Rifle, but you can also do Rifle r = new Rifle(...);
That is so appreciated! Thank you for clarifying this for me, yes I was clearly confused partly about the issue and partly in terms of my structure. I do have one more question though, since my abstract class isn't a monobehaviour, how should I go about creating a method to reload. I instinctively want to create a coroutine and use WaitForSeconds to toggle a boolean. I don't really know what the alternative to this is.
Reload as in reload the weapon's ammo?
Since all Guns reload, I would put a public void Reload() method in the Guns class. This method should do exactly what it says it's going to, which is reload the gun. If the gun has a delay between the start and finish of the Reload, yes you could use WaitForSeconds before actually adding the ammo capacity. If there is an animation, the method could play it, etc, etc.
Then, in the Update method of your input handling class (which would be a $$anonymous$$onoBehaviour), you would check if the reload button was pressed and reload the current gun, ie: currentGun.Reload();
(I'll leave it to you to handle finding/tracking reference to the 'currentGun')
To further this, you could mark the Reload function as virtual, which would allow 'Rifle' (or any other derived class) to override the behavior if it is different (plays a different animation/sound, etc).
Your answer
Follow this Question
Related Questions
Need help with specifics in Random.Range 1 Answer
Spawn the next room by opening a door. 3 Answers
How to make RANDOM COLOR on shader 1 Answer
Instantiate GameOBject with changing size in relation to another GameObject in the scene C# 0 Answers
Randonly Losing reference to GameObject from prefab 1 Answer