Search Unity

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

  2. Unity 2022.1 is now available as the latest Tech release.
    Dismiss Notice
  3. Improve your project's performance with our new guide on profiling in Unity.
    Dismiss Notice

Ammo script help

Discussion in 'Scripting' started by Mattyerial, Jan 15, 2022.

  1. Mattyerial

    Mattyerial

    Joined:
    Oct 5, 2018
    Posts:
    46
    Hello i`m trying to figure out how would i tell my playerscript to solve how many clips i have.

    In my inventory lets say i have 2 ammo, both 25 bullets each = 50 ammo.

    My code would be currentAmmo = inventory.GetQuantity("Ammo");
    It would be equal to 50, how would i make current ammo, lets say 25 and the other 25 is a clip.
    Is there a easy way to do this.
     
  2. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    634
    You could do something like this for example:

    Code (CSharp):
    1. [Serializable]
    2. public class Inventory
    3. {
    4.     public Item[] items;
    5.    
    6.     public int GetBulletCount()
    7.     {
    8.         int bulletCount = 0;
    9.        
    10.         foreach(var item in items)
    11.         {
    12.             if(item is Clip clip)
    13.             {
    14.                 bulletCount += clip.bullets;
    15.             }
    16.         }
    17.  
    18.         return bulletCount;
    19.     }
    20. }
    21.  
    22. [Serializable]
    23. public class Item { }
    24.  
    25. [Serializable]
    26. public class Clip : Item
    27. {
    28.     public int bullets = 25;
    29. }
     
  3. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    634
    Or if all items in the inventory have an amount member, you could avoid the need for casting from Item to Clip and create a more generalizable count method.

    Code (CSharp):
    1.  
    2. public class Item
    3. {
    4.     public string type;
    5.     public int amount;
    6. }
    7.  
    8. public class Inventory
    9. {
    10.     private Item[] items;
    11.    
    12.     public int GetQuantity(string type)
    13.     {
    14.         int count = 0;
    15.        
    16.         foreach(var item in items)
    17.         {
    18.             if(item.type == type)
    19.             {
    20.                 count += item.amount;
    21.             }
    22.         }
    23.        
    24.         return count;
    25.     }
    26. }
     
  4. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    556
    If it’s open ended like your player could have 99 clips or 3 clips aka there is no max clip limit - then you could set up a list of list to represent Ammo[ClipNumber][BulletToRemove]

    this is to say maybe some bullets jam the gun using a list of list you can talk about each bullet in the clip as an individual instance, for example; fire a bullet, change clips for a fresh clip and eventually cycle back around to your original clip without having to have components or others methods of the like to remember your clips ammo count. As the list to list you fire one and remove an index from that list until that list is 0.

    this could also be the way to go if you are not throwing away clips but keeping and reloading them.
    Idk though

    just a alternative route to look into
     
unityunity