getplayerhealth problem?
#1

Well i was making for restaurant if the player health is 100 they cannot eat but for some reason people could eat with any health,
on top
pawn Код:
new Float:health;
the code
pawn Код:
if(GetPlayerHealth(playerid,health) == 100) return SendClientMessage(playerid,lightyellow,"I think you are bit too full for eating");
Reply
#2

pawn Код:
GetPlayerHealth(playerid,health);
if(health == 100) {}
Reply
#3

pawn Код:
new health[MAX_PLAYERS];
pawn Код:
if(GetPlayerHealth(playerid,health) == 100) return SendClientMessage(playerid,lightyellow,"I think you are bit too full for eating");
Not tested, Vince his code could also work.
Reply
#4

https://sampwiki.blast.hk/wiki/GetPlayerHealth
Quote:

Returns The function itself doesn't return a specific value. The player's health is stored in the specified variable.

If you do
pawn Код:
if(GetPlayerHealth(playerid,health) == 100) return SendClientMessage(playerid,lightyellow,"I think you are bit too full for eating");
You're trying to retrieve the players health, directly.
That wouldn't work.
Reply
#5

Also, the health is a float.
Never use
pawn Код:
GetPlayerHealth(playerid,health);
if(health == 100) return SendClientMessage(playerid,lightyellow,"I think you are bit too full for eating");
Floats tend to be inaccurate for checking directly.
Your health could be 100.000001 or so (because of the way computers work with floats and they have a limited amount of bits to represent a floating value), the == will never succeed.
The 100.000001 could be stored this way in the computer's memory, but when printing it with fewer digits after the comma, it's rounded down to 100.00 for example. But the real value is still 100.000001.

Use this instead:
pawn Код:
GetPlayerHealth(playerid,health);
if(health > 99.9) return SendClientMessage(playerid,lightyellow,"I think you are bit too full for eating");
Reply
#6

PowerPC is correct which is why I usually round off floats if i want to match them
Reply
#7

You could use a function like this:
pawn Код:
// This function can be used to check if a float matches the other one
CheckFloat(Float:Value1, Float:Value2)
{
    // Check if Value1 is nearly the same as Value2
    if (((Value2 - 0.1) < Value1) && (Value1 < (Value2 + 0.1)))
        return 1;
    else
        return 0;
}


// Compare if 2 floats are the same
GetPlayerHealth(playerid, health);
if (CheckFloat(health, 100.0) == 1) return SendClientMessage(playerid,lightyellow,"I think you are bit too full for eating");
Reply
#8

Or you can use this too:
Код:
if (floatround(health, floatround_round) == 100)
Might be easier than the CheckFloat function.
Reply
#9

Lots of reply and thank you all guys, repped
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)