Help with my Hunger
#1

Hey guys im a bit stuck, i am making a hunger system, but when i use /eat it puts my hunger over the MAX_HUNGER limit. could someone show me what i am doing wrong? i will +rep the person who can help fix this.

Here's my code (i have more to it but will only show if needed).
pawn Код:
#define MAX_HUNGER 100.0

enum PlayerInfo
{
    Float:Hunger
}
new pInfo[MAX_PLAYERS][PlayerInfo];

CMD:eat(playerid, params [])
{
    if(pInfo[playerid][Hunger] >= MAX_HUNGER) return SendClientMessage(playerid, -1, "You Are Full!");
    if(pInfo[playerid][Food] < 1) return SendClientMessage(playerid, -1, "You do not have enough food to eat, use /buyfood to buy some food");
    pInfo[playerid][Hunger] += 5;
    pInfo[playerid][Food] -= 1;
    SendClientMessage(playerid, -1, "You have Eaten and restored your hunger by 5");
    return 1;
}
Thank you guys in advance
Reply
#2

Let's say that their hunger is 99. That is not equal or greater than MAX_HUNGER so the script continues. Then you proceed to add 5: 99 + 5 = 104! You need to check if their hunger is greater than or equal to MAX_HUNGER - 5, or use clamp to ensure it never goes out of bounds.
Reply
#3

DELETED
Reply
#4

You could add this piece of code to limit the value to 100:

pawn Код:
pInfo[playerid][Hunger] += 5;
if(pInfo[playerid][Hunger] > 100)
{
    pInfo[playerid][Hunger] = 100;
}
Reply
#5

@Vince i have never used clamp before and to be honest wouldn't know where to start with putting it in my code.
any chance you could provide an example on the code i have provided?

@1fret i get a tag mismatch with that. thanks anyway.

@Schneider the code i have
pawn Код:
if(pInfo[playerid][Hunger] >= MAX_HUNGER) return SendClientMessage(playerid, -1, "You Are Full!");
works but if you have lets say 98.1 life and used the /eat it will set the Hunger to 103.1 which is what i dont want
Reply
#6

You didn't undrestand what Vince said !
i'll give you an example
my hunger id 99.
it's not more MAX_HUNGER !
so script continue and set my hunger 99+5 = 104!

you have to check if player hunger+5 not more MAX_HUNGER , increase it .

script:

pawn Код:
#define MAX_HUNGER 100

enum PlayerInfo
{
    Float:Hunger
}
new pInfo[MAX_PLAYERS][PlayerInfo];
CMD:eat(playerid, params [])
{
    if(pInfo[playerid][Food] < 1) return SendClientMessage(playerid, -1, "You do not have enough food to eat, use /buyfood to buy some food");
    if(pInfo[playerid][Hunger]+5 >= MAX_HUNGER) return SendClientMessage(playerid, -1, "You Are Full!");
    pInfo[playerid][Hunger] += 5;
    pInfo[playerid][Food] -= 1;
    SendClientMessage(playerid, -1, "You have Eaten and restored your hunger by 5");
    return 1;
}
or simply REset their hunger if was more than 100

pawn Код:
#define MAX_HUNGER 100

enum PlayerInfo
{
    Float:Hunger
}
new pInfo[MAX_PLAYERS][PlayerInfo];
CMD:eat(playerid, params [])
{
    if(pInfo[playerid][Hunger] >= MAX_HUNGER) return SendClientMessage(playerid, -1, "You Are Full!");
    if(pInfo[playerid][Food] < 1) return SendClientMessage(playerid, -1, "You do not have enough food to eat, use /buyfood to buy some food");
    pInfo[playerid][Hunger] += 5;
    if(pInfo[playerid][Hunger] > MAX_HUNGER) pInfo[playerid][Hunger] = 100; //<----- Here
    pInfo[playerid][Food] -= 1;
    SendClientMessage(playerid, -1, "You have Eaten and restored your hunger by 5");
    return 1;
}
Reply
#7

Thanks M4D i have rep'd you for you help and also rep'd Vince as he helped but i didnt understand him properly.
Reply
#8

Quote:
Originally Posted by Jakwob
Посмотреть сообщение
Thanks M4D i have rep'd you for you help and also rep'd Vince as he helped but i didnt understand him properly.
Lame... Read my post again, the code M4D posted is exactly what I wrote too...

Just check if the value exceeds 100, if so, set it back to 100... that's all!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)