Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How do I implement a weapon class system?

Discussion in 'Scripting' started by Marscaleb, Apr 30, 2014.

  1. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    977
    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.
     
  2. sootie8

    sootie8

    Joined:
    Mar 25, 2014
    Posts:
    233
    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
    Code (csharp):
    1.  
    2. public class smallDagger : WeaponMaster{
    3.      ammoToUse = 5;
    4.      void OnEnable(){
    5.           MasterAmmoToUse = ammoToUse;  //changing an inherited property
    6.         }
    7.  
    8.  
    9.       override public UseWeapon(){
    10.            base.useWeapon(); //this runs whatever code is stored in your WeaponMaster, this code would be run even in not called but since
    11.               // you are overriding you must include it
    12.              Sound.play("DaggerSound"); // you get the idea extra code goes here.
    13. }
    14.  
    15.  
     
    Last edited: Apr 30, 2014
  3. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    977
    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#.
     
  4. Deleted User

    Deleted User

    Guest

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

    Code (csharp):
    1.  
    2.  
    3. // the weapon Base class
    4. class Weapon : MonoBehavior
    5. {
    6.     public int ammoToUse;
    7.  
    8.     public void UseWeapon()
    9.     {
    10.         // should contain default behavior
    11.     }
    12. }
    13.  
    14. class MyAwsomeWeapon : Weapon
    15. {
    16.     void Awake()
    17.     {
    18.         // set the ammo to use
    19.         ammoToUse = 10;
    20.     }
    21.  
    22.     // overide default behavior
    23.     public override void UseWeapon()
    24.     {
    25.         // do your weapon specific work
    26.     }
    27. }
    28.  
    29.  
    30. class Player : Monobehavior
    31. {
    32.     public Weapon currentWeapon;
    33.     int ammo;
    34.     void Update()
    35.     {
    36.         if( someCondition  ( ammo - currentWeapon.ammoToUse ) >= 0 )
    37.         {
    38.             ammo -= currentWeapon.ammoToUse;
    39.             currentWeapon.UseWeapon();
    40.         }
    41.            
    42.     }
    43. }
    44.  
    45.  
     
  5. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    977
    Thank you element; that sample code was very helpful!
     
  6. djfunkey

    djfunkey

    Joined:
    Jul 16, 2012
    Posts:
    201
    I was just wondering how this turned out for you? As I would like to know if the moethod above worked efficiently.