Tutorials
Flashlight that Requires Batteries
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_Codehttp://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.

Okay! Now, we're going to stop the flashlight from recharging.
Go into the hl2_player.cpp file and locate:
#define FLASH_DRAIN_TIME #define FLASH_CHARGE_TIME
What mine looks like(Note: I've changed the values):

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:
// 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; }

As you can see in the picture i've added
void ApplyFlashlightBattery( void );
So add that to yours aswell.
Now go back into hl2_player.cpp and search for
int CHL2_Player::FlashlightIsOn( void )
Then after that function you add a new function:
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:

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:
//=============================================================================//
// 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:
@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:

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
