Adding Headlights to the Buggy: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
(Initial article)
 
m (Use "YouTube" template for link)
 
(4 intermediate revisions by the same user not shown)
Line 4: Line 4:


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 ==
Line 33: Line 35:
</source>
</source>


Then in <code>void CPropJeep::DriveVehicle( ... )</code> uncomment the following code like so:
Then in <code>void CPropJeep::DriveVehicle( ... )</code> uncomment the following block of code:
<source lang=cpp highlight=1,13>
<source lang=cpp>
/*
/*if ( ucmd->impulse == 100 )
if ( ucmd->impulse == 100 )
{
{
if ( HeadlightIsOn() )
if ( HeadlightIsOn() )
Line 46: Line 47:
HeadlightTurnOn();
HeadlightTurnOn();
}
}
}
}*/
*/
</source>
</source>


Line 85: Line 85:
== 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.
{{Todo|Add a section on how to change the headlight on and off sounds.}}


[[Category:Programming]]
[[Category:Programming]]

Latest revision as of 13:09, 25 June 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 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 );
	void HeadlightTurnOff( void );

vehicle_jeep.cpp

Now for 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( void )
{
	EmitSound( "Airboat_headlight_on" );
	m_bHeadlightIsOn = true;
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPropJeep::HeadlightTurnOff( void )
{
	EmitSound( "Airboat_headlight_off" );
	m_bHeadlightIsOn = false;
}

Next, in void CPropJeep::ExitVehicle( int nRole ) replace:

	HeadlightTurnOff();

with:

	if ( HeadlightIsOn() )
	{
		‎‎‎‎HeadlightTurnOff();
	}

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.


Todo: Add a section on how to change the headlight on and off sounds.