[Tutorial] Simple Hunger System
#1

Right, so. Here, I have possibly the most simple thing you'll ever make. Could be great for RP servers as well. So lets get started shall we?

so, we'll be using ZCMD for this tutorial. If you use string compare or dcmd, I honestly highly recommend you upgrade to ZCMD, it's the best command processor in my honest opinion. Although everyone's entitled to their own.

We'll start by adding the 2 includes that we'll need.
pawn Code:
#include <a_samp>//This is required
#include <zcmd>//CMD: ability
Next, we'll define how much HP you'll lose when you're hungry, and how long until you're hungry
pawn Code:
#undef MAX_PLAYERS       //                                                    |
#define MAX_PLAYERS 50 //Change this to your player slot amount               |
#define needFoodTimer 10000 //Edit this value to change the timer, now = 10 mins    |
#define losingHealth 5  //The HP the player will lose              
#define COLOR_LIGHTRED 0xFF6347AA //Lets not forget the colors!
#define COLOR_LIGHTGREEN 0x7CFC00AA
Next we'll create the forward for the timer.
pawn Code:
forward needFood(playerid);
As for the 2 array's we'll be using
pawn Code:
new firstSpawn[MAX_PLAYERS];
new Float:curHealth[MAX_PLAYERS];
Right, so next we'll want to create the timer, for when that person would be hungry, and all that lovely stuff. So to do that, we'll need to create a timer. You'll want to put it under OnPlayerSpawn, so that it starts the hunger counter everytime the player spawns.

pawn Code:
public OnPlayerSpawn(playerid) {
    firstSpawn[playerid] += 1;//This is for when they spawn
    new Float:Health;//This is the float we'll need to detect current health
    GetPlayerHealth(playerid, Health);//This is to get their health
    curHealth[playerid] = Health;//Same for above
    if(firstSpawn[playerid] == 1)//This is for when we start the timer
    SetTimerEx("needFood",needFoodTimer,1,"i", playerid);//Lets start the timer shall we?
    return 1;
}
Ok, so now we have the timer made, and going, now we need to create the result of when they get hungry.
pawn Code:
public needFood(playerid) {
    new Float:Health;
    GetPlayerHealth(playerid, Health);
    if(curHealth[playerid] >= health) {
        SendClientMessage(playerid, COLOR_LIGHTRED, "You start to feel hungry!");
        SetPlayerHealth(playerid,Health-losingHealth);
        curHealth[playerid] = Health-5;//This is to take the health away
    } else {
        curHealth[playerid] = health;
    }
}
Next up, we'll create the command for it. Normally I'd have it locked to food places, but for this tutorial, I'll just make it so that you can use it anywhere.

pawn Code:
CMD:eat(playerid) {
    SetPlayerHealth(playerid, 100);
    SetTimerEx("needFood",needFoodTimer,1,"i",playerid);//Now that the players eaten, we'll reset the timer
    SendClientMessage(playerid, COLOR_LIGHTGREEN, "You are no longer hungry");
    return 1;
}
And that's my tutorial for a simple hunger system. If you liked it feel free to comment and/or rep. Or tell me opinions and how I'd be able to improve for future tutorials, as this was my first tutorial ever so I realize it may not be the best, but who says there's no room for improvement right?
Reply
#2

Very nice and simple
Reply
#3

Quote:
Originally Posted by Zens
View Post
Very nice and simple
Thanks, exactly what I was aiming for =3
Reply
#4

You should use [.PAWN] [./PAWN] tags instead the CODE tags. (Remove the dots from the PAWN tags)
Reply
#5

Quote:
Originally Posted by Bible
View Post
You should use [.PAWN] [./PAWN] tags instead the CODE tags. (Remove the dots from the PAWN tags)
Yeah, I never knew about those tags =3

I'll add those now =]
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)