GetVehicleHealth? [!+rep!] :)
#1

hi,

how can i check what player is driving the vehicle with the most health atm?
(vehicle health NOT player health!)

Would be happy about help, cause this code is not working well:

pawn Код:
//im using this in a timer:
forward Detector();
public Detector()
{
    new Float:MostHealth, Float:vHealth, vDriver;
    foreach(Player, i)
    {
        if(GetPlayerVehicleSeat(i) != 0) continue; //Ignores passengers and players that aren't in a vehicle
        GetVehicleHealth(GetPlayerVehicleID(i), vHealth);
        if(floatcmp(MostHealth, vHealth) == -1) //Checks if the health is higher than the one saved previously
        {
            MostHealth = vHealth;
            vDriver = i;
        }
    }
    new name[MAX_PLAYER_NAME];
    GetPlayerName(vDriver, name, MAX_PLAYER_NAME);
    //here i want to get the name of the player that is driving the vehicle with the most health...
    //...
    return 1;
}
thx
Reply
#2

That is not really hard.
This is the final product:

pawn Код:
new __vHealth, Float:_vHealth, Float:vHealth_update = 0;

for(new iii; iii < MAX_PLAYERS; iii++) {
    __vHealth = GetVehicleHealth(iii, _vHealth);
    if(__vHealth > vHealth_update) {
        vHealth_update = __vHealth;
    }
}
What does it actually do? It first creates 3 vars. Vehicle Health (Float:__vHealth), varname for the vehicle health of the player (_vHealth) and one which retrieves the most health. Because var "vHealth_update" starts at 0, the var will change immediatly after the loop. It loops through all players, but while looping, variable "vHealth_update" gets updated each time someone else has a higher health. See it as an estafette. If you run first, and you have become a bit tired, you have not alot energy. However, the guy in front of you has more energy, so you pass the stick to him (vHealth_update). I hope I explained it good enough
Reply
#3

tx alot!

so i could just do:

pawn Код:
new name[MAX_PLAYER_NAME];
    GetPlayerName(__vHealth, name, MAX_PLAYER_NAME);
to get the name of the player with the most vehicle health?
Reply
#4

Nono, __vHealth is used to pass the health into that variable
Reply
#5

so how could i then get the name of the player with the most vehicle health?
Please help im kinda new to it
Reply
#6

Then you have to place the
pawn Код:
GetPlayerName(playerid, name, sizeof(name));
inside the loop, and replace "playerid" with "iii"
Reply
#7

mhh thx, but it says the health of the vehicle of the best player would be 0.0 in the SendClientMessage.
Any ideas where the bug might be?

would be most appreciated.
Reply
#8

You need to format the string.

pawn Код:
new __vHealth, Float:_vHealth, Float:vHealth_update = 0, string[128];

for(new iii; iii < MAX_PLAYERS; iii++) {
    __vHealth = GetVehicleHealth(iii, _vHealth);
    if(__vHealth > vHealth_update) {
        vHealth_update = __vHealth;
        GetPlayerName(iii, name, sizeof(name));
        format(string, sizeof(string), "%s has the vehicle with the most health: %0.2f", name, __vHealth);
        SendClientMessage(playerid, -1, string);
        return 1;
    }
}
Reply
#9

ofc i did format the string but it still shows 0.0 in the message for the player whos vehicle got the most health...
any ideas?
Reply
#10

Thanks for fixing it Devran;
@PawnoQ, you could try debugging the changes
pawn Код:
new __vHealth, Float:_vHealth, Float:vHealth_update = 0, string[128];

for(new iii; iii < MAX_PLAYERS; iii++) {
    __vHealth = GetVehicleHealth(iii, _vHealth);
    printf("[debug #0] current health: %f", __vHealth);
    printf("[debug #0.5] current health (test 2): %f", _vHealth);
    printf("[debug #1] vehicle health: %f", vHealth_update);
    if(__vHealth > vHealth_update) {
        printf("[debug #2] vehicle health: %f", vHealth_update);
        vHealth_update = __vHealth;
        printf("[debug #3] vehicle health: %f", vHealth_update);
        GetPlayerName(iii, name, sizeof(name));
        printf("[debug #4] player: %s, vehicle health: %f", name, vHealth_update);
        format(string, sizeof(string), "%s has the vehicle with the most health: %0.2f", name, __vHealth);
        SendClientMessage(playerid, -1, string);
        return 1;
    }
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)