Search Unity

Custom Weapon Stats? Read for more info

Discussion in 'Scripting' started by KyleStank, Oct 29, 2014.

  1. KyleStank

    KyleStank

    Joined:
    Feb 9, 2014
    Posts:
    204
    I have a simple object question. At least, I think this is using objects because it uses the "new" keyword to reference things. But anyway, I am making a FPS game and there are guns in it, obviously, and I want each gun to have it's own stats and such. Here is the way I have it set now:

    Code (csharp):
    1. public class WeaponType
    2. {
    3.     public bool weaponCanAimIn = false;
    4.     public bool weaponIsAimingIn = false;
    5.  
    6.     public float weaponFireRate = 0.0f;
    7.     public float weaponDamage = 0.0f;
    8.     public int[] weaponAmmoMags = new int[5];
    9.     /*
    10.      * weaponDamge Array Indexs
    11.      * [0]: "Assault Rifle" Ammo Mags,
    12.      * [1]: "Sub-Machine Gun" Ammo Mags,
    13.      * [2]: "Handgun" Ammo Mags,
    14.      * [3]: "Sniper Rifle" Ammo Mags,
    15.      * [4]: "Shotgun" Ammo Mags
    16.     */
    17.     public int weaponAmmoInMag = 0;
    18.     public int weaponAmmoAllowedInMag = 0;
    19.     public float weaponAccuracy = 0.0f;
    20.     public float weaponHitRange = 0.0f;
    21.  
    22.     public string weaponShootType;
    23.     /*
    24.      * TYPES OF SHOOT TYPES TO CHOOSE FROM: (8 in all)
    25.      *
    26.      * FOR MELEE:
    27.      * "Melee"
    28.      *
    29.      * FOR ASSAULT RIFLES AND SUB-MACHINE GUNS:
    30.      * "Fully-Automatic",
    31.      * "Semi-Automatic"
    32.      *
    33.      * FOR HANDGUNS:
    34.      * "Semi-Automatic" (ALWAYS CHOOSE THIS FOR HANDGUNS)
    35.      *
    36.      * FOR SNIPER RIFLES:
    37.      * "Bolt-Action",
    38.      * "Semi-Automatic"
    39.      *
    40.      * FOR SHOTGUNS:
    41.      * "Pump-Action",
    42.      * "Semi-Automatic",
    43.      * "Fully-Automatic"
    44.     */
    45.     public string weaponType;
    46.     /*
    47.      * TYPES OF GUNS TO CHOOSE FROM:
    48.      *
    49.      * "Melee"
    50.      * "Assault Rifle",
    51.      * "Sub-Machine Gun",
    52.      * "Handgun",
    53.      * "Sniper Rifle",
    54.      * "Shotgun"
    55.     */
    56.     public string weaponName;
    57.  
    58.     public Transform weaponAttackPoint;
    59.     public Transform weaponTransform;
    60.  
    61.     public WeaponType (bool CanAimIn, float FireRate, float Damage, int AmmoAllowedInMag, float Accuracy, float HitRange, string ShootType, string WeaponType, string Name)
    62.     {
    63.         weaponCanAimIn = CanAimIn;
    64.         weaponFireRate = FireRate;
    65.         weaponDamage = Damage;
    66.         weaponAmmoAllowedInMag = AmmoAllowedInMag;
    67.         weaponAccuracy = Accuracy;
    68.         weaponHitRange = HitRange;
    69.         weaponShootType = ShootType;
    70.         weaponType = WeaponType;
    71.         weaponName = Name;
    72.     }
    73. }
    74.  
    This works for changing weapons and upgrading stats and all, but if I want to display the stats or add an attachment, I don't think this is the best way of doing it because I keep needing to do "weaponType = new WeaponType (stuff in here);"

    Here is the way I want to do it but I am not sure if I should:

    Code (csharp):
    1. public class CurrentWeapon
    2. {
    3.     //This is for switching weapons in player's hand.
    4. }
    5.  
    6. public class WeaponStats
    7. {
    8.     //Below I would declare a lot of arrays and stuff
    9.     public float[] weaponDamage = new float[35]; //I have 36 weapons
    10.     /*
    11.         Here is where I would list which array is to keep track of everything. Like this:
    12.         [0]: "M4A1" Weapon Damage
    13.         [1]: "M16A1" Weapon Damage... etc
    14.     */
    15. }
    16.  

    Is this the best way of doing things? What are some suggestions? And by the way, every class I showed is below the "main" class in the script that I created.
     
  2. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    I always create a GameObject empty, then I attach the "model" / script (in your case: "Weapon") on this G.O., then I save it in the Resources folder, inside a Subfolder (like: "Weapons"). So there is a prefab per weapon. Finally, I just load all prefabs with Resources.LoadAll<Weapon>("Weapons") : it loads all prefabs inside Resources/Weapons/ and returns an array.

    This way, you can edit your weapon easily and you just have to get the weapon you need with a getter (GetWeapon(string weaponName) or GetWeapon(int weaponID) or whatever).

    Sbizz.
     
  3. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    You should REALLY look into enums instead of having strings dictate all of your attributes.
     
  4. KyleStank

    KyleStank

    Joined:
    Feb 9, 2014
    Posts:
    204
    @Sbizz I have never done this before so what does the "<Weapon>" before "('Weapons')" do? Is the <Weapon> the class that "Resources.LoadAll ();" is return the value to?

    @Zaladur I will do this since I am thinking of reworking this. But strings are not ALL of the weapon's attributes. I used the strings for the name because that should be a string. Also the way it shoots, like Semi-Auto or Full-Auto, and the type of gun Like Assault Rifle or Sub Machine Gun although that could change. But everything else is int and float variables.
     
    Last edited: Oct 29, 2014
  5. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    Yea, the biggest culprits were weaponType and shootType. You shouldn't need comments to define the valid values of the strings. Enums restrict them nicely for you. WeaponName is fine to be left as a string. Most ints and floats are fine as is. WeaponAmmoMag is another one I might want to reorganize - you shouldn't need to remember that handguns = 2.
     
  6. KyleStank

    KyleStank

    Joined:
    Feb 9, 2014
    Posts:
    204
    @Zaladur Wouldn't the same thing be happening with enums though? Like in the switch statement, isn't that when you are supposed to tell Unity what to do when a certain enum is selected? This is what it showed in a video tutorial I watched a while back. Like, wouldn't I still need a lot of variables that would just be equaled (don't know the right word to use there) to a value. Like this for example:

    Code (csharp):
    1. switch (example)
    2. {
    3.     case ExampleGun:
    4.         exampleGunDamage[0] = 1.0f; //I would still need something to hold a lot of variables for each gun to have it's own stats.Is there a good way to do that? Sorry, I just don't have like years of
    5. experience.
    6.         example = ExampleEnum.Null; //If I don't make it change, I think, could be wrong, but I think it keeps executing this OVER and OVER. So if I assigned my stats here, it would be doing it OVER AND OVER which could be bad. Like this it just changes it to something with nothing assigned to it.
    7.         break;
    8. }
    9.  
     
  7. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    @Vlentful_Studios It's a bit hard to explain to someone who begins. But the simple way: you want the class Resources returns a "Weapon" variable. If you want to get the GameObject, you have to do "Resources.LoadAll<GameObject>("Weapons");". If you do that, you have to be sure that all loaded prefabs have the component. Because if one of them doesn't have the asked component, the prefab will not be loaded.

    And about the enums, it's easier to read "myWeapon.type == RIFLE" than "myWeapon.type == 1". Also, if you have 3 eWeapon (HANDSGUN, RIFLE, SHOTGUN), if you remove RIFLE, all references to RIFLE will throw an error (because it no longer exists). But if you just have 3 IDs.. it's a F***ing mess :D
     
  8. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    Ok so you have your enum:

    (I can't remember if my enum syntax is perfect)
    Code (csharp):
    1. public enum weaponType {
    2.     Handgun,
    3.     Shotgun,
    4.     AssaultRifle,
    5.     //etc
    6. }
    Yes you will still need to compare. The benefit of using the enums is twofold. On the one hand, I can't accidentally (or puposefully) put in an invalid value. What happens when someone types "weaponType = "Shootgun"? Or weaponType = "Godmode". At best, your code gracefully handles the error and you go back and fix it. Or you are stuck sitting there wondering why the hell your weapon isn't working right.

    Secondly, it is much safer to compare enums than strings, and easier to remember than comparing ints.
     
  9. KyleStank

    KyleStank

    Joined:
    Feb 9, 2014
    Posts:
    204
    @Sbizz Thank you SO much. This should help. I didn't get it at first but after a little research, I learned that the folders need to be in a "Resources" folder. Which makes sense, lol. But again, thank you as this will probably help with attachments too if I can do it correctly with them.

    @Zaladur Okay, thanks for clarifying there. I now know what you mean and can continue this game now.

    Thank you both! I should have what I need now!