Float health and altitude won't change.
#1

Topic says all. The health lowers down in a milisecond, the next thing you know, it's back up to 1000, same as the altitude

Code:
PHP код:
#define METRES_TO_FEET(%0) \
    
( ( %) / 0.3047999995367040007042099189296 )
public 
OnPlayerUpdate(playerid)
{
    for(new 
0MAX_PLAYERSi++)
    {
        if(
IsPlayerConnected(i))
        {
            if(
IsPlayerInAnyVehicle(playerid))
            {
                
// Getting the vehicles speed in km/h
                 
new vspeed[25];
                  new 
Floatx;
                new 
Floaty;
                new 
Floatz;
                  new 
altstring[25];
                
format(vspeedsizeof(vspeed), "~b~KMH: ~w~%d"GetKMHSpeed(i));
                
PlayerTextDrawSetString(ivehKMH[i], vspeed);
                
// Getting the vehicles health (shown as percentage)
                
new vhealthtd[32], Float:vHealth;
                
GetVehicleHealth(GetPlayerVehicleID(playerid), vHealth);
                
format(vhealthtdsizeof(vhealthtd), "~b~Health: ~w~%0.0f"vHealth);
                
PlayerTextDrawSetString(ivehHealth[i], vhealthtd);
                
                new 
vid;
                
vid GetPlayerVehicleID(playerid);
                
GetVehiclePos(vidxyz);
                
format(altstringsizeof(altstring), "~b~Altitude: ~w~ %d M"floatroundMETRES_TO_FEET) ));
                
PlayerTextDrawSetString(ivehAlt[i], altstring);
            }
        }
    }
    return 
1;

Reply
#2

Why are you using OnPlayerUpdate? and why there's a loop in this callback?

OnPlayerUpdate, also called OPU, is called atleast 30 times per second, and may be called more than that depending on what update that the player is sending. OPU with such a code can cause lag and errors (bugs). You can use Foreach or pool-size loop added in 0.3.7. You can move the whole code to a timer and call the timer every second.
Reply
#3

Where do you suggest I would move this
Reply
#4

^^ Re-read my post.

+ What are you trying to state in...
"Topic says all. The health lowers down in a milisecond, the next thing you know, it's back up to 1000, same as the altitude."
There's no code related to this... Plus, you can also use one same string to format some text.
Reply
#5

Trying out a timer for 300 ms
Reply
#6

Not trying to offend you, but you're offering to teach scripting where you've none to less scripting and actual knowledge of SA-MP. Some stuff that I'd suggest you is, that you read the SA-MP wiki, it has almost everything you need.

Use 1000 ms (1 second) timer, and call it under OnGameModeInit, kill it under OnGameModeExit. Use foreach or player-pool size loop instead of MAX_PLAYERS loop, I'd recommend foreach though, but it's totally up to you that which one you choose to use.

EDIT: Example given below:
PHP код:
// Along with other defines
#define METRES_TO_FEET(%0) \
    
((%0) / 0.3047999995367040007042099189296)
// Under OnGameModeInit
SetTimer("updateVehicleTD"1000true);
// Somewhere in the script
forward    updateVehicleTD();
public    
updateVehicleTD()
{
    new 
string[50], FloatvehXFloatvehYFloatvehZvehHealth;
    foreach(new 
Player)
    {
        if(
IsPlayerInAnyVehicle(i))
        {
            
format(stringsizeof string"~b~KM/H: ~w~%d"GetKMHSpeed(i));
            
PlayerTextDrawSetString(ivehKMH[i], string);
            
GetVehicleHealth(GetPlayerVehicleID(playerid), vHealth);
            
format(stringsizeof string"~b~Health: ~w~%0.0f"vHealth);
            
PlayerTextDrawSetString(ivehHealth[i], string);
            
GetVehiclePos(GetPlayerVehicleID(playerid), vehXvehYvehZ);
            
format(stringsizeof string"~b~Altitude: ~w~%d M"floatround(METRES_TO_FEET(z)));
            
PlayerTextDrawSetString(ivehAlt[i], string);
        }
    }
    return 
1;

Reply
#7

You might have to carefully read my signature.
Quote:

In need of the basic logics of scripting?

PS: Yeah, that thing really offended me. Bruh, try to read the signature carefully. By basic logics I mean the introduction of PAWN, variables, functions, callbacks, etc. That sht is advanced. And, btw, I was the one who came up with the timer, I didn't use any of the code given from you. The only info I got from you was not to add it on OnPlayerUpdate. That's all you need.

EDIT: Keep your opinions to yourself, you don't know how a 13-year-old would feel for someone who's trying to pull him down. Especially a man with a Leo trait
Reply
#8

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.
Reply
#9

Quote:
Originally Posted by Vince
Посмотреть сообщение
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.
There goes. Lesson learned. It won't be necessary to offend.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)