Adding Headlights to the Buggy: Difference between revisions
GamerDude27 (talk | contribs) (Initial article) |
GamerDude27 (talk | contribs) m (Cleanup, better template/tag usage, added section for custom headlight sounds) |
||
| (5 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
== The Tutorial == | == The Tutorial == | ||
This tutorial explains how to add headlights to the [[Prop_vehicle_jeep|Buggy]]. | This tutorial explains how to add headlights to the '''[[Prop_vehicle_jeep|Buggy]]'''. | ||
It requires a basic understanding of C++ but the actual code is very simple. | It requires a basic understanding of C++ but the actual code is very simple. | ||
For a video-based version of this tutorial, check out {{youtube|EWai2uiF-ss|page=watch|TheMaster974's YouTube video guide}}. | |||
== The Code == | == The Code == | ||
=== vehicle_jeep.h === | === vehicle_jeep.h === | ||
First, open | First, open {{path|src/game/server/hl2/vehicle_jeep|h}}. | ||
Find: | Find: | ||
< | <syntaxhighlight lang=cpp> | ||
bool HeadlightIsOn( void ) { return m_bHeadlightIsOn; } | bool HeadlightIsOn( void ) { return m_bHeadlightIsOn; } | ||
void HeadlightTurnOn( void ) { m_bHeadlightIsOn = true; } | void HeadlightTurnOn( void ) { m_bHeadlightIsOn = true; } | ||
void HeadlightTurnOff( void ) { m_bHeadlightIsOn = false; } | void HeadlightTurnOff( void ) { m_bHeadlightIsOn = false; } | ||
</ | </syntaxhighlight> | ||
and replace them with: | and replace them with: | ||
< | <syntaxhighlight lang=cpp> | ||
bool HeadlightIsOn( void ) { return m_bHeadlightIsOn; } | bool HeadlightIsOn( void ) { return m_bHeadlightIsOn; } | ||
void HeadlightTurnOn( | void HeadlightTurnOn(); | ||
void HeadlightTurnOff( | void HeadlightTurnOff(); | ||
</ | </syntaxhighlight> | ||
=== vehicle_jeep.cpp === | === vehicle_jeep.cpp === | ||
Now for | Now for {{path|src/game/server/hl2/vehicle_jeep|cpp}}. | ||
Go to <code>void CPropJeep::Precache( void )</code> and add these: | Go to <code>void CPropJeep::Precache( void )</code> and add these: | ||
< | <syntaxhighlight lang=cpp> | ||
PrecacheScriptSound( "Airboat_headlight_on" ); | PrecacheScriptSound( "Airboat_headlight_on" ); | ||
PrecacheScriptSound( "Airboat_headlight_off" ); | PrecacheScriptSound( "Airboat_headlight_off" ); | ||
</ | </syntaxhighlight> | ||
Then in <code>void CPropJeep::DriveVehicle( ... )</code> uncomment the following code | Then in <code>void CPropJeep::DriveVehicle( ... )</code> uncomment the following block of code: | ||
< | <syntaxhighlight lang=cpp> | ||
/* | /*if ( ucmd->impulse == 100 ) | ||
{ | { | ||
if ( HeadlightIsOn() ) | if ( HeadlightIsOn() ) | ||
| Line 46: | Line 47: | ||
HeadlightTurnOn(); | HeadlightTurnOn(); | ||
} | } | ||
} | }*/ | ||
*/ | </syntaxhighlight> | ||
</ | |||
At the end of the file add: | At the end of the file add: | ||
< | <syntaxhighlight lang=cpp> | ||
//----------------------------------------------------------------------------- | //----------------------------------------------------------------------------- | ||
// Purpose: | // Purpose: | ||
//----------------------------------------------------------------------------- | //----------------------------------------------------------------------------- | ||
void CPropJeep::HeadlightTurnOn( | void CPropJeep::HeadlightTurnOn() | ||
{ | { | ||
EmitSound( "Airboat_headlight_on" ); | EmitSound( "Airboat_headlight_on" ); | ||
| Line 64: | Line 64: | ||
// Purpose: | // Purpose: | ||
//----------------------------------------------------------------------------- | //----------------------------------------------------------------------------- | ||
void CPropJeep::HeadlightTurnOff( | void CPropJeep::HeadlightTurnOff() | ||
{ | { | ||
EmitSound( "Airboat_headlight_off" ); | EmitSound( "Airboat_headlight_off" ); | ||
m_bHeadlightIsOn = false; | m_bHeadlightIsOn = false; | ||
} | } | ||
</ | </syntaxhighlight> | ||
Next, in <code>void CPropJeep::ExitVehicle( int nRole )</code> replace: | Next, in <code>void CPropJeep::ExitVehicle( int nRole )</code> replace: | ||
< | <syntaxhighlight lang=cpp> | ||
HeadlightTurnOff(); | HeadlightTurnOff(); | ||
</ | </syntaxhighlight> | ||
with: | with: | ||
< | <syntaxhighlight lang=cpp> | ||
if ( HeadlightIsOn() ) | if ( HeadlightIsOn() ) | ||
HeadlightTurnOff(); | HeadlightTurnOff(); | ||
</syntaxhighlight> | |||
</ | |||
== Customizing Headlight Sounds == | |||
The tutorial uses the default '''[[Prop_vehicle_airboat|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 {{path|vehicle_jeep|cpp}}, find the <code>Precache</code> function and change: | |||
<syntaxhighlight lang=cpp> | |||
PrecacheScriptSound( "Airboat_headlight_on" ); | |||
PrecacheScriptSound( "Airboat_headlight_off" ); | |||
</syntaxhighlight> | |||
And in the <code>HeadlightTurnOn()</code> and <code>HeadlightTurnOff()</code> functions, change: | |||
<syntaxhighlight lang=cpp> | |||
EmitSound( "Airboat_headlight_on" ); | |||
EmitSound( "Airboat_headlight_off" ); | |||
</syntaxhighlight> | |||
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|.wav]] format recommended) to the appropriate {{path|sound/}} directory | |||
# Create or modify a sound script file to reference your new sounds (e.g. {{path|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|soundscripts]] article. | |||
== Conclusion == | == Conclusion == | ||
And we're done! All we needed to do was uncomment some code and add a sound to play. | 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: {{key|F}}). | |||
[[Category:Programming]] | [[Category:Programming]] | ||
Latest revision as of 06:13, 23 September 2025
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).