Adding Headlights to the Buggy

From Valve Developer Community
Jump to: navigation, search

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.

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.


Blank image.pngTodo: Add a section on how to change the headlight on and off sounds.