Adding Headlights to the Buggy
The Tutorial
This tutorial explains how to add headlights to the Buggy.
It requires a basic understanding of C++ but the actual code is very simple.
For a video-based version of this tutorial, check out TheMaster974's YouTube video guide.
The Code
vehicle_jeep.h
First, open src/game/server/hl2/vehicle_jeep.h
.
Find:
bool HeadlightIsOn( void ) { return m_bHeadlightIsOn; }
void HeadlightTurnOn( void ) { m_bHeadlightIsOn = true; }
void HeadlightTurnOff( void ) { m_bHeadlightIsOn = false; }
and replace them with:
bool HeadlightIsOn( void ) { return m_bHeadlightIsOn; }
void HeadlightTurnOn();
void HeadlightTurnOff();
vehicle_jeep.cpp
Now for src/game/server/hl2/vehicle_jeep.cpp
.
Go to void CPropJeep::Precache( void )
and add these:
PrecacheScriptSound( "Airboat_headlight_on" );
PrecacheScriptSound( "Airboat_headlight_off" );
Then in void CPropJeep::DriveVehicle( ... )
uncomment the following block of code:
/*if ( ucmd->impulse == 100 )
{
if ( HeadlightIsOn() )
{
HeadlightTurnOff();
}
else
{
HeadlightTurnOn();
}
}*/
At the end of the file add:
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPropJeep::HeadlightTurnOn()
{
EmitSound( "Airboat_headlight_on" );
m_bHeadlightIsOn = true;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPropJeep::HeadlightTurnOff()
{
EmitSound( "Airboat_headlight_off" );
m_bHeadlightIsOn = false;
}
Next, in void CPropJeep::ExitVehicle( int nRole )
replace:
HeadlightTurnOff();
with:
if ( HeadlightIsOn() )
HeadlightTurnOff();
Customizing Headlight Sounds
The tutorial uses the default Airboat headlight sounds, but you can easily customize them to use different audio files.
Changing to Different Existing Sounds
To use different sounds that already exist in the game, simply replace the sound script names in two places:
In vehicle_jeep.cpp
, find the Precache
function and change:
PrecacheScriptSound( "Airboat_headlight_on" );
PrecacheScriptSound( "Airboat_headlight_off" );
And in the HeadlightTurnOn()
and HeadlightTurnOff()
functions, change:
EmitSound( "Airboat_headlight_on" );
EmitSound( "Airboat_headlight_off" );
Replace both sound names with any valid sound script from the game.
Adding Custom Sound Files
To use completely custom sounds:
- Add your sound files (.wav format recommended) to the appropriate
sound/
directory - Create or modify a sound script file to reference your new sounds (e.g.
scripts/game_sounds_vehicles.txt
) - Use the sound script name in the code as shown above
For more details on adding custom sounds to Source Engine, see the soundscripts article.
Conclusion
And we're done! All we needed to do was uncomment some code and add a sound to play.
Now when you enter the car you can turn the headlights on and off by pressing your assigned flashlight key (default: F).