Announcement

Collapse
No announcement yet.

Is there a way to count the number of bullets fired

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Is there a way to count the number of bullets fired

    I have been awake too many hours and cant seem to wrap my head around this or find any info on google.

    So I have a pretty simple question.

    Is there a line of code/function I can add to my custom weaponbase that extends UTWeapon that will check if the weapon has been fired and
    increment up an integer variable that is stored in the Player Controller?


    I know it seems silly, and I am feeling really silly for asking this question as it seems it must be very simple..


    I.E. WeaponBase.uc
    Code:
    //If player fired this weapon,increment M1911BulletsFired by 1
    
    M1911BulletsFired++;
    The purpose is for counting total shots with each weapon and keeping each BulletsFired integer variable unique to each weapon class to be used for stats tracking. Regardless if the player switched weapons and has equipped the weapon again to fire more shots.The effect will still increment the same "total" value over the entire game

    PlayerController.uc

    Code:
    var int M1911BulletsFired;
    var int AK47BulletsFired;
    var int ShotgunBulletsFired;
    
    `log("Number of M1911 Bullets Fired:    " @ M1911BulletsFired);
    `log("Number of AK47 Bullets Fired:      " @ AK47BulletsFired);
    `log("Number of Shotgun Bullets Fired: " @ ShotgunBulletsFired);
    Is there a better way to do this?

    Any help would be greatly appreciated.
    -Chris

    #2
    You don't need an extra counter per weapon that your game is going to have. Just needs one in your weaponbase that is incremented each time the weapon fires. All spawned weapons that are subclasses of weaponbase will automatically have their own counter inherited from weaponbase. That means if you spawn two M1911's those are two different counters.

    If that's not the desired behaviour you could have a dynamic int array for example in your gameinfo/playercontrolller to store the number of bullets fired per weapontype/weaponclass (not per instance like above). The weaponbase only needs the index it is allowed to increment for that array which also happens in the weaponbase each time the weapon is fired. This index must then be set in each weaponbase subclasses defaultproperties and should be unique.

    The function could be something like StartFire/InstantFire/ProjectileFire/CustomFire/etc.


    Edit: not sure if this is what you want, I somehow missed the line where you say the int is in your playercontroller the above assumes it is in the weaponclass, but the array solution might could still work

    Comment


      #3
      But each weapon has its own base class due to the weapon reload function logic is different from each other.
      I.E.
      M1911Weapon.uc
      Code:
      class M1911Weapon extends M1911WeaponBase;
      AK47Weapon.uc
      Code:
      class AK47Weapon extends AK47WeaponBase;
      and i can understand code somewhat, but what you explained to me makes no sense to me.
      is there a simpler way you could explain this?

      -Chris

      Comment


        #4
        is there something simple i could add to UTweapon.uc function "ConsumeAmmo"?
        Or would that break something?
        (Iknow modding the default UT Classes is a no no..)
        UTWeapon.uc Line: 1966
        Code:
        class UTWeapon extends UDKWeapon;
        ...
        
        /**
         * Consumes some of the ammo
         */
        function ConsumeAmmo( byte FireModeNum )
        {
        	// Subtract the Ammo
        	AddAmmo(-ShotCost[FireModeNum]);
        
               // get weapon being used and increment that weapon's TotalBulletsFired.
        
        }
        Or could I add a few lines to the AK47weapon like:

        AK47Weapon.uc
        Code:
        class AK47Weapon extends AK47WeaponBase;
        
        ...
        if(FireModeNum == 0)
        	{
        	  AK47BulletsFired++;
                }
        Would this automatically update the integer variable in the player controller?

        -Chris

        Comment


          #5
          Ok so having the number of fired bullets in a playercontroller:
          Code:
          class YourPlayerController extends SomeOtherPlayerController;
          
          struct ClassIntStruct
          {
          	var int i;
          	var class<BaseClassOfAllYourWeapons> c;
          };
          
          var array<ClassIntStruct> NumberOfBulletsFiredPerWeaponClass;
          
          //more stuff
          
          //defaultproperties
          Code:
          class BaseClassOfAllYourWeapons extends UTWeapon
          	DependsOn(YourPlayerController);
          
          //stuff
          
          //or different function
          function ConsumeAmmo(byte FireModeNum)
          {
          	local int index;
          	local ClassIntStruct cis;
          
          	super.ConsumeAmmo(FireModeNum);
          
          	if (Instigator != none && YourPlayerController(Instigator.Controller) != none)
          	{
          		index = YourPlayerController(Instigator.Controller).NumberOfBulletsFiredPerWeaponClass.Find('c', self.class);
          		if (index == INDEX_NONE) //weapon is not in the array
          		{
          			cis.c = self.class;
          			cis.i = 1;
          			YourPlayerController(Instigator.Controller).NumberOfBulletsFiredPerWeaponClass.AddItem(cis);
          		}
          		else
          		{
          			YourPlayerController(Instigator.Controller).NumberOfBulletsFiredPerWeaponClass[index].i++;
          		}
          	}
          }
          
          //stuff
          
          //defaultproperties
          Instead of adding/checking wheter the weapon is in the array in the playercontroller or not you could add each weapon in postbeginplay automatically (and store the index) so you could save the Find function and the check as well.

          The above hasn't been tested nor compiled so typos/minor mistakes might be included

          The idea is basically that you have 1 baseclass and then extend and override the things in subclasses. The baseclass could be abstract as well. Which would mean AK47Weapon extends AK47WeaponBase, AK47WeaponBase extends BaseClassOfAllYourWeapons, BaseClassOfAllYourWeapons extends UTWeapon.

          UTWeapon should be safe for editing as it's not a native class or anything, but as you said it's a big no no and usually not necessary anyways.


          Hope that makes it more understandable now

          Comment


            #6
            So i did the compiling for you
            Compiler:
            Code:
            Error, Unrecognized type 'ClassIntStruct'
            and it seems to not find the
            BaseClassOfAllYourWeapons.uc
            Code:
            local ClassIntStruct cis;
            do I have to add the DependsOn(YourPlayerController) class declaration? (am going to try this)

            (yes i have replaced the class names)

            I am coming ever so closer to understanding this, but wont fully understand until i can get it compiled and throw some `log()'s in there
            Thank you for you help so far.

            -Chris

            Comment


              #7
              Adding the BaseClassOfAllYourWeapons.uc
              Code:
              DependsOn(YouplayerController)
              Declaration compiled correctly.

              Now How would i log this??

              Like this?
              Code:
              `log(c, i);
              // would display: ClassNameForWeapon, NumberOfBulletsFiredForThatWeapon?
              how would i get the per array values? (i havent played with them much)
              I will reference some books for this info

              -Chris

              Comment


                #8
                If you want to log all elements in the NumberOfBulletsFiredPerWeaponClass array:
                Code:
                //add to YourPlayerController 
                
                //can be called through consolecommand
                exec function LogArrayItems()
                {
                	local int i;
                
                	for (i = 0; i < NumberOfBulletsFiredPerWeaponClass.length; i++)
                	{
                		`log("Weapon" @NumberOfBulletsFiredPerWeaponClass[i].c @"has been fired" @NumberOfBulletsFiredPerWeaponClass[i].i @"times.");
                	}
                }
                If you want to display these values in a menu or something like that you could add a string property to the ClassIntStruct and use that string instead of the class of the weapon.

                Comment

                Working...
                X