Adding Headlights to the Buggy: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
m (Use "YouTube" template for link)
m (Cleanup, better template/tag usage, added section for custom headlight sounds)
 
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.
Line 10: Line 10:
=== vehicle_jeep.h ===
=== vehicle_jeep.h ===


First, open '''vehicle_jeep.h'''.
First, open {{path|src/game/server/hl2/vehicle_jeep|h}}.


Find:
Find:
<source lang=cpp>
<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; }
</source>
</syntaxhighlight>
and replace them with:
and replace them with:
<source lang=cpp>
<syntaxhighlight lang=cpp>
bool HeadlightIsOn( void ) { return m_bHeadlightIsOn; }
bool HeadlightIsOn( void ) { return m_bHeadlightIsOn; }
void HeadlightTurnOn( void );
void HeadlightTurnOn();
void HeadlightTurnOff( void );
void HeadlightTurnOff();
</source>
</syntaxhighlight>


=== vehicle_jeep.cpp ===
=== vehicle_jeep.cpp ===


Now for '''vehicle_jeep.cpp'''.
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:
<source lang=cpp>
<syntaxhighlight lang=cpp>
PrecacheScriptSound( "Airboat_headlight_on" );
PrecacheScriptSound( "Airboat_headlight_on" );
PrecacheScriptSound( "Airboat_headlight_off" );
PrecacheScriptSound( "Airboat_headlight_off" );
</source>
</syntaxhighlight>


Then in <code>void CPropJeep::DriveVehicle( ... )</code> uncomment the following block of code:
Then in <code>void CPropJeep::DriveVehicle( ... )</code> uncomment the following block of code:
<source lang=cpp>
<syntaxhighlight lang=cpp>
/*if ( ucmd->impulse == 100 )
/*if ( ucmd->impulse == 100 )
{
{
Line 48: Line 48:
}
}
}*/
}*/
</source>
</syntaxhighlight>


At the end of the file add:
At the end of the file add:
<source lang=cpp>
<syntaxhighlight lang=cpp>
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Purpose:
// Purpose:
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CPropJeep::HeadlightTurnOn( void )
void CPropJeep::HeadlightTurnOn()
{
{
EmitSound( "Airboat_headlight_on" );
EmitSound( "Airboat_headlight_on" );
Line 64: Line 64:
// Purpose:
// Purpose:
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CPropJeep::HeadlightTurnOff( void )
void CPropJeep::HeadlightTurnOff()
{
{
EmitSound( "Airboat_headlight_off" );
EmitSound( "Airboat_headlight_off" );
m_bHeadlightIsOn = false;
m_bHeadlightIsOn = false;
}
}
</source>
</syntaxhighlight>


Next, in <code>void CPropJeep::ExitVehicle( int nRole )</code> replace:
Next, in <code>void CPropJeep::ExitVehicle( int nRole )</code> replace:
<source lang=cpp>
<syntaxhighlight lang=cpp>
HeadlightTurnOff();
HeadlightTurnOff();
</source>
</syntaxhighlight>
with:
with:
<source lang=cpp>
<syntaxhighlight lang=cpp>
if ( HeadlightIsOn() )
if ( HeadlightIsOn() )
{
‎‎‎‎HeadlightTurnOff();
‎‎‎‎HeadlightTurnOff();
}
</syntaxhighlight>
</source>
 
== 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.
Now when you enter the car you can turn the headlights on and off by pressing your assigned flashlight key (default: {{key|F}}).
 
 
{{Todo|Add a section on how to change the headlight on and off sounds.}}


[[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 YouTube logo 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:

  1. Add your sound files (.wav format recommended) to the appropriate 🖿sound/ directory
  2. Create or modify a sound script file to reference your new sounds (e.g. 🖿scripts/game_sounds_vehicles.txt)
  3. 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).