Speedometer

From Valve Developer Community
Jump to: navigation, search
Wikipedia - Letter.png
This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these template messages)
Underlinked - Logo.png
This article needs more Wikipedia icon links to other articles to help Wikipedia icon integrate it into the encyclopedia. Please help improve this article by adding links Wikipedia icon that are relevant to the context within the existing text.
January 2024

Simple Speedometer

This Tutorial trys to explain how to use the existing Variables to display a HUD with the current Speed of the Vehicle.

Adding the Speedometer

Basically Valve imeplemented this feature already, you just can't access the member because its Private. And the best solution to access the member from the HUD is to create a new member.

Open "game/client/c_prop_vehicle.h" and search for:

 virtual void ClientThink( void );

Add after:

virtual int Speed();

Open "game/client/c_prop_vehicle.cpp" and search for:

#define        TRIANGULATED_CROSSHAIR 1

Add after:

int C_PropVehicleDriveable::Speed()
{
       return m_nSpeed;
}

Now you got the basic implementation for the Client, all you have to do now is editing the abstract class so that you can call this Member of any Vehicle. You can create now a new file called for example hud_speed.cpp and add following code:

void CHudSpeed::OnThink()
{
	C_BasePlayer *player = C_BasePlayer::GetLocalPlayer();
	IClientVehicle *pVehicle = player ? player->GetVehicle() : NULL;
	if ( pVehicle )
	{
		CBaseEntity *pVehicleEnt = pVehicle->GetVehicleEnt();
		if ( !pVehicleEnt )
		{
			// If there is no vehicle we do not need the Speed HUD
			SetPaintEnabled(false);
			SetPaintBackgroundEnabled(false);
			SetShouldDisplayValue(false);
			return;
		}
		m_hCurrentVehicle = pVehicleEnt;
		SetShouldDisplayValue(true);
 		SetPaintEnabled(true);
		SetPaintBackgroundEnabled(true);
		SetDisplayValue( (int)((float)pVehicle->Speed() * 1.4) ); // Must be improved.
	}
 	else
	{
		SetPaintEnabled(false);
 		SetPaintBackgroundEnabled(false);
		SetShouldDisplayValue(false);
 		return;
	}
}

If you are not really sure what to do with this code, just copy the hud_ammo.cpp and replace the OnThink function. Do also not forget to edit the part on hud_ammo.cpp where it shows the Ammo of the Vehicle.

HUD Layout

Do also not forget to add the HUD Layout to display it correctly. Open "SourceMods/<yourmodname>/scripts/HudLayout.res" add after the HudAmmo block:

	HudSpeed
	{
		"fieldName" "HudSpeed"
		"xpos"	"r150"
		"ypos"	"432"
		"wide"	"136"
		"tall"  "36"
		"visible" "1"
		"enabled" "1"

		"PaintBackgroundType"	"2"

		"text_xpos" "8"
		"text_ypos" "20"
		"digit_xpos" "44"
		"digit_ypos" "2"
		"digit2_xpos" "98"
		"digit2_ypos" "16"
	}

Patch

You can also use the patch file: http://pastebin.com/f53cbf308

Warning.pngWarning:This code was only tested on my machine.

Code by Matthew M.

See also