How do I implement a weapon class system?

I need to implement a system into my game, and it requires some more advanced for of programming than I have dealt with before. I am hoping someone could explain to me how to create this system.

Here’s the final result: The game has different types of throwing weapons. Each type consumes a different amount of a universal ammo, and allows the player to throw a different projectile item. The player can only have only one type of throwing weapon at any given time.

So here’s what I’m getting stuck on: I need to program a system whereby the Player Character script can designate what type of throwing weapon the player has, and from that variable be able to access different values such as how much ammo to consume and what prefab to instantiate when called for.

Off the top of my head I could create an enumerator for the weapon type, have a variable saving that enum, and then go through a series of switches to determine the various effects of using this particular weapon. But that creates some very jumbled, ugly, and unprofessional code that only gets bigger and harder to keep track of the more types I create.

I know there is better way.

In my mind’s eye I can see some way to use a custom class for my variable. I simply create a class that designates what the variables and functions are, and then create extensions of that class that set those variables and functions.
Then in my player character’s script I can have a variable that saves what class it is using, and then calls variables from that class. Like I could say “WeaponType.AmmoCost” and it would grab the designated ammo cost from the weapon type that is currently set. Ideally I could call functions from that class too, so if one type uses a completely different method for how it handles throwing the weapon, like calling different animations or spawning multiple projectiles, that is still called from the same “WeaponType.ThrowWeapon()” command.

But I don’t know how to do this, neither do I know the correct terms, so I can’t even look it up.

I started making a WeaponType script and making some classes that extend that script with their own properties, but I don’t know how to change the default values of particular variables.
I also am unsure how I would declare the variable used in my player’s class. And I’ve never worked with scripts that are not attached to game objects, but intended to be accessed independently like I am thinking.

I think I know your problem, I came upon it myself recently. Your describing wanting inheritance in your script, which is where methods and properties(function and variables) are passed down to another class. Maybe you should research this concept a little more. Anyway inherited methods and properties can be overridden (changed). An example

public class smallDagger : WeaponMaster{
     ammoToUse = 5; 
     void OnEnable(){
          MasterAmmoToUse = ammoToUse;  //changing an inherited property
        }

 
      override public UseWeapon(){
           base.useWeapon(); //this runs whatever code is stored in your WeaponMaster, this code would be run even in not called but since
              // you are overriding you must include it
             Sound.play("DaggerSound"); // you get the idea extra code goes here.
}

Yes, I am thinking of using an inheritance system. But how can I set default values in a class, and override them in a child class?
I’m not that familiar with C#.

I hope this little example gets you on the right track.

// the weapon Base class
class Weapon : MonoBehavior
{
    public int ammoToUse;

    public void UseWeapon()
    {
        // should contain default behavior
    }
}

class MyAwsomeWeapon : Weapon
{
    void Awake()
    {
        // set the ammo to use
        ammoToUse = 10;
    }

    // overide default behavior
    public override void UseWeapon()
    {
        // do your weapon specific work
    }
}


class Player : Monobehavior
{
    public Weapon currentWeapon;
    int ammo;
    void Update()
    {
        if( someCondition  ( ammo - currentWeapon.ammoToUse ) >= 0 )
        {
            ammo -= currentWeapon.ammoToUse;
            currentWeapon.UseWeapon();
        }
            
    }
}

Thank you element; that sample code was very helpful!

I was just wondering how this turned out for you? As I would like to know if the moethod above worked efficiently.