Speedometer
January 2024
You can help by adding links to this article from other relevant articles.
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
Code by Matthew M.