Coded kill counter

Grab your favourite IDE and tinker with the innards of game engines

Coded kill counter

Postby Mapster on Tue Jan 01, 2013 8:14 am

Hello there Interlopers, I have started working on a kill counter for hl2 in the source code using a queue of events that add an additional count to the metre everytime something is killed. Although this is still in alpha, the bug isn't showing itself even when I do a bug report, but when i try loading a map using my killcounter entity, the map crashes.

Code: Select all
#include "cbase.h"
#include "entityinput.h"
#include "entityoutput.h"
#include "mathlib/mathlib.h"
#include "utlqueue.h"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

CUtlVector<int> intQueue;

class CKillCounterEntity : public CBaseEntity
{

public:
   DECLARE_CLASS( CKillCounterEntity, CBaseEntity );
   
   void Spawn(void)
   {
      SetNextThink( gpGlobals->curtime + 0.2 );
   };

   void Increment(void)
   {
      AddtoQueue(1);
   };

   void Reset(void)
   {
      iValue = 0;
   };


   int GetValue(void)
   {
      return iValue;
   };

   void Think(void)
   {
      iValue += intQueue[0];
      intQueue.Remove(0);

      DevMsg("Killcount: %i",iValue);

      SetNextThink( gpGlobals->curtime + 0.2 );
   }

   void AddtoQueue( int Amount )
   {
      if( Amount > 0 )
      {
         intQueue.AddToTail(Amount);
         DevMsg("+1 Killcount.");
      }
   };
   
   void InputIncrement(inputdata_t &inputdata) { Increment(); }
   void InputReset(inputdata_t &inputdata) { Reset(); }
   
   DECLARE_DATADESC();
   
private:
   int iValue;

};

LINK_ENTITY_TO_CLASS(lux_killcounter, CKillCounterEntity);


BEGIN_DATADESC( CKillCounterEntity )

   DEFINE_FIELD( iValue, FIELD_INTEGER ),

   DEFINE_INPUTFUNC( FIELD_VOID, "Increment", InputIncrement ),
   DEFINE_INPUTFUNC( FIELD_VOID, "Reset", InputReset ),

END_DATADESC()


Any help would be greatly appreciated :)

EDIT:
Okay i did some more bug testing and found that
Code: Select all
intQueue.Remove(1);
is what's crashing it

EDIT2:
Fixed using this code:

Code: Select all
   void Think(void)
   {
      for (int i = 0; i < intQueue.Count();)
      {
         if( intQueue[i] > 0 )
         {
            iValue += intQueue[i];
            intQueue.Remove(i);
         }
      }
         DevMsg("Killcount: %i\n",iValue);

         SetNextThink( gpGlobals->curtime + 3.0 );
   }
Image
User avatar
Mapster
Member
Member
 
Joined: Thu Mar 01, 2012 4:07 am

Return to Programming

Who is online

Users browsing this forum: No registered users