Flashlight that Requires Batteries

Tutorial collection, comprehensive listings on main site.

Flashlight that Requires Batteries

Postby Tutorial on Sun Mar 28, 2010 8:37 pm

category
Miscellaneous

description
Altering the flashlight to require batteries.

keywords
flashlight, battery, batteries, charge, time, limit.

Index
1. Introduction
2. Prerequisites
3. Tutorial
4. Additional Tutorial

Introduction
Another coding tutorial by me (Corewarp). I checked with you guys (Interlopers coding forum) to see if you wanted a tutorial on this, and you did so here it is.

Hope you can use it in your mods and what not.

NB: This tutorial is based on Orange Box code. EP1 code might not work.

Prerequisites
If you've programmed for source before, or are doing it now you can pretty much skip this step.

You will need:
1. A compiler - Link: http://www.microsoft.com/express/download/
You want the Visual C++ 2008 Express.

2. Already setup solution. Pretty all you need to do to get your code ready is described here:
http://developer.valvesoftware.com/wiki ... ource_Code
http://developer.valvesoftware.com/wiki/My_First_Mod

3. Common sense. (More important than people think!)

Tutorial
Okay, in this tutorial i will show you how to make it so that the player's flashlight doesn't recharge, which will make the player have to find batteries for it. And additionally in the second tutorial below this one i'll show you how to set up a logic_flashlightcontrol entity to control whether the player can use the flashlight or not.

Let's begin:
Open the following files:
hl2_player.cpp (Located in: Server episodic->Source Files->HL2 DLL)
hl2_player.h (Same as above)

Create a file called: item_flashbattery.cpp
Can can create the file anywhere in the server episodic area. I however made a new directory for it as depicted below.

Image

Okay! Now, we're going to stop the flashlight from recharging.
Go into the hl2_player.cpp file and locate:
Code: Select all
#define   FLASH_DRAIN_TIME   
#define   FLASH_CHARGE_TIME   


What mine looks like(Note: I've changed the values):

Image

You'll want to set FLASH_CHARGE_TIME to 0, to make it stop recharging.

Now we need to create the function to 'recharge' the battery.
Go into hl2_player.h and look for:

Code: Select all
   // Flashlight Device
   void            CheckFlashlight( void );
   int               FlashlightIsOn( void );
   void            FlashlightTurnOn( void );
   void            FlashlightTurnOff( void );
   bool            IsIlluminatedByFlashlight( CBaseEntity *pEntity, float *flReturnDot );
   void            SetFlashlightPowerDrainScale( float flScale ) { m_flFlashlightPowerDrainScale = flScale; }


Image

As you can see in the picture i've added

Code: Select all
 void            ApplyFlashlightBattery( void );


So add that to yours aswell.

Now go back into hl2_player.cpp and search for
Code: Select all
int CHL2_Player::FlashlightIsOn( void )

Then after that function you add a new function:

Code: Select all
void CHL2_Player::ApplyFlashlightBattery( void ) // m_HL2Local.m_flFlashBattery
{
   if ( m_HL2Local.m_flFlashBattery != 100 )
   {
      m_HL2Local.m_flFlashBattery = 100;
   }
}


What it looks like on my end:

Image

Okay! Now we've setup all the things for the player. Time to create our new flashbattery entity!

Open up item_flashbattery.cpp and add the following code:

Code: Select all
//=============================================================================//
// Purpose: Insta Recharge of the Flashlight; Based on item_battery.cpp
//=============================================================================//

#include "cbase.h"
#include "hl2_player.h"
#include "basecombatweapon.h"
#include "gamerules.h"
#include "items.h"
#include "engine/IEngineSound.h"

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

class CItemFlashBattery : public CItem
{
public:
   DECLARE_CLASS( CItemFlashBattery, CItem );

   void Spawn( void )
   {
      Precache( );
      SetModel( "models/items/battery.mdl" ); //Might aswell use the default battery model for now.
      BaseClass::Spawn( );
   }
   void Precache( void )
   {
      PrecacheModel ("models/items/battery.mdl");

      PrecacheScriptSound( "ItemBattery.Touch" );

   }
   bool MyTouch( CBasePlayer *pPlayer ) // OnTouch recharge flashlight battery for the player
   {
      EmitSound( "ItemBattery.Touch" );
      CHL2_Player *pHL2Player = dynamic_cast<CHL2_Player *>( pPlayer );
      pHL2Player->ApplyFlashlightBattery();
      return true;
   }
};

LINK_ENTITY_TO_CLASS(item_flashbattery, CItemFlashBattery);
PRECACHE_REGISTER(item_flashbattery);


Great success!

Now compile and test it out!
If you go back to the hl2_player.cpp you can also change the drain rate to make the flashlight last longer, like i have in mine. (Lower the rate, slower it runs out of battery).

Note: To get it in hammer you need to make a custom fgd(or just add it to an existing one) and add this code:
Code: Select all
@include "base.fgd"
@PointClass base(Targetname, Angles) studio("models/items/battery.mdl") = item_flashbattery : "battery" []

^Courtesy of Bekey^

Additional Tutorial
To disable the player's flashlight all you need is a point_servercommand entity.
Then to disable/enable the flashlight do this:

Image

The above is just an EXAMPLE. What you need is just to use the point_servercommand(Named server in the picture).
And then have it do the command ent_fire !player disableflashlight or enableflashlight. Simple.

(No coding need as valve already did the work for this)

Corewarp
- Don't send PM's to this user -
Tutorial
Not A Real User
 
Joined: Sun Mar 06, 2005 11:00 pm

Re: Flashlight that Requires Batteries

Postby Sentura on Wed Jul 21, 2010 9:44 pm

what happens if the fgd's dont match between computers? as in, what if a player uses a 'custom' fgd where he/she just added those extra lines? i'm counting on some mismatch exception here...
User avatar
Sentura
Regular
Regular
 
Joined: Mon Jun 09, 2008 10:19 am

Re: Flashlight that Requires Batteries

Postby stoopdapoop on Wed Jul 21, 2010 9:52 pm

Sentura wrote:what happens if the fgd's dont match between computers? as in, what if a player uses a 'custom' fgd where he/she just added those extra lines? i'm counting on some mismatch exception here...


aren't FGD's only used by hammer? after the map is compiled I'm pretty sure the FGD doesn't matter for shit.
I'm Brown
Image
User avatar
stoopdapoop
Veteran
Veteran
 
Joined: Sun Aug 21, 2005 2:14 am
Location: Ann Arbor, MI

Re: Flashlight that Requires Batteries

Postby Sentura on Wed Jul 21, 2010 10:06 pm

maybe so, they are only references. but the underlying code still needs to be transferred.
User avatar
Sentura
Regular
Regular
 
Joined: Mon Jun 09, 2008 10:19 am

Return to Tutorials

Who is online

Users browsing this forum: No registered users