#define METRES_TO_FEET(%0) \
( ( %0 ) / 0.3047999995367040007042099189296 )
public OnPlayerUpdate(playerid)
{
for(new i = 0; i < MAX_PLAYERS; i++)
{
if(IsPlayerConnected(i))
{
if(IsPlayerInAnyVehicle(playerid))
{
// Getting the vehicles speed in km/h
new vspeed[25];
new Float: x;
new Float: y;
new Float: z;
new altstring[25];
format(vspeed, sizeof(vspeed), "~b~KMH: ~w~%d", GetKMHSpeed(i));
PlayerTextDrawSetString(i, vehKMH[i], vspeed);
// Getting the vehicles health (shown as percentage)
new vhealthtd[32], Float:vHealth;
GetVehicleHealth(GetPlayerVehicleID(playerid), vHealth);
format(vhealthtd, sizeof(vhealthtd), "~b~Health: ~w~%0.0f", vHealth);
PlayerTextDrawSetString(i, vehHealth[i], vhealthtd);
new vid;
vid = GetPlayerVehicleID(playerid);
GetVehiclePos(vid, x, y, z);
format(altstring, sizeof(altstring), "~b~Altitude: ~w~ %d M", floatround( METRES_TO_FEET( z ) ));
PlayerTextDrawSetString(i, vehAlt[i], altstring);
}
}
}
return 1;
}
// Along with other defines
#define METRES_TO_FEET(%0) \
((%0) / 0.3047999995367040007042099189296)
// Under OnGameModeInit
SetTimer("updateVehicleTD", 1000, true);
// Somewhere in the script
forward updateVehicleTD();
public updateVehicleTD()
{
new string[50], Float: vehX, Float: vehY, Float: vehZ, vehHealth;
foreach(new i : Player)
{
if(IsPlayerInAnyVehicle(i))
{
format(string, sizeof string, "~b~KM/H: ~w~%d", GetKMHSpeed(i));
PlayerTextDrawSetString(i, vehKMH[i], string);
GetVehicleHealth(GetPlayerVehicleID(playerid), vHealth);
format(string, sizeof string, "~b~Health: ~w~%0.0f", vHealth);
PlayerTextDrawSetString(i, vehHealth[i], string);
GetVehiclePos(GetPlayerVehicleID(playerid), vehX, vehY, vehZ);
format(string, sizeof string, "~b~Altitude: ~w~%d M", floatround(METRES_TO_FEET(z)));
PlayerTextDrawSetString(i, vehAlt[i], string);
}
}
return 1;
}

|
In need of the basic logics of scripting? |
|
There's nothing wrong with using OnPlayerUpdate if you know how to use it. This callback's main purpose is to provide a system to create your own custom callbacks like OnPlayerHealthChange, OnPlayerWeaponChange and things like that. Don't use it for things that the server controls (score and such) and don't use it as a general purpose timer.
I would probably move the "health" thingy to a separate function and call it from OnPlayerStateChange, OnVehicleDamageStatusUpdate and OnPlayerWeaponShot because most vehicle health changes occur due to collisions or bullets. For a smooth experience you need at least 24 updates per second or the equivalent of a 40-ish milliseconds timer so speed and altitude can probably stay in OPU. They're only 5 lines of code which shouldn't cause any serious issues. Just make sure that your "get speed" function is optimized, i.e. use the VectorSize version, not the outdated and slow floatsqroot version. |