getplayerhealth problem? -
Tanush123 - 03.01.2012
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
the code
pawn Код:
if(GetPlayerHealth(playerid,health) == 100) return SendClientMessage(playerid,lightyellow,"I think you are bit too full for eating");
Re: getplayerhealth problem? -
Vince - 03.01.2012
pawn Код:
GetPlayerHealth(playerid,health);
if(health == 100) {}
Re: getplayerhealth problem? -
geerdinho8 - 03.01.2012
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.
Re: getplayerhealth problem? -
FireCat - 03.01.2012
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.
Re: getplayerhealth problem? -
PowerPC603 - 03.01.2012
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");
Re: getplayerhealth problem? -
cessil - 03.01.2012
PowerPC is correct which is why I usually round off floats if i want to match them
Re: getplayerhealth problem? -
PowerPC603 - 03.01.2012
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");
Re: getplayerhealth problem? -
Basssiiie - 03.01.2012
Or you can use this too:
Код:
if (floatround(health, floatround_round) == 100)
Might be easier than the CheckFloat function.
Re: getplayerhealth problem? -
Tanush123 - 04.01.2012
Lots of reply and thank you all guys, repped