[Tutorial] Creating a boundaries system
#1

Hi,
I thought this tutorial might be useful for some people who work on a TDM/DM servers, this tutorial will 'probably' teach you how to keep players within the allowed area only, getting out will kill them(how sad).

Anyway, here we begin:
pawn Code:
new MapZone[MAX_PLAYERS]; // we need this to identify the area/zone or whatever.
Now, this:
pawn Code:
new Float:Max[2] = {569.5944,2742.9941};
new Float:Min[2] = {-838.9635,1163.5237};
Max should ALWAYS be the bigger number, if not, everywhere will be out of boundaries! So be careful.
Replace them with your preferred co-ordinates. These co-ordinates are from the abandoned airport to that big bridge.

Now under OnGameModeInit() :
pawn Code:
MapZone = GangZoneCreate(Min[0],Min[1],Max[0],Max[1]);
We have created the zone, but it's not visible YET.
Let me explain, as you've seen above, I've set it to Float:Max[2], 2 means the two numbers between the brackets, 569.5944 and 2742.9941, so by using Max[0], it means that we have selected the first number. I guess you understand now. But you always should start with 0.

Under OnPlayerDisconnect :
pawn Code:
GangZoneHideForPlayer(playerid, MapZone); // hides the zone/area when the player leaves.
Now under OnPlayerSpawn :
pawn Code:
GangZoneShowForPlayer(playerid, MapZone, 0xBBBBBB88); // Shows the zone to the player when he spawns, '0xBBBBBB88' is the color GREY.
Now let's create a timer that loops every 1 second. (DO NOT USE OnPlayerUpdate! It's NOT per second!).
pawn Code:
new maintimer; // our timer <3
You can rename that to whatever you want.
___
Go back to OnGameModeInit()
pawn Code:
maintimer = SetTimer("MainTimer", 1000, true); // 1000 = one second. true = loops/repeats every 1 second.
You can now go anywhere you like, and forward your MainTimer and add your functions:

pawn Code:
forward MainTimer();
public MainTimer()
{
        new Float:HP; // player health
        for(new i=0; i < MAX_PLAYERS; i++) // loops through players
        {
                if(IsPlayerConnected(i) && (GetPVarInt(i, "Spectate") == 0)) //You can add more exceptions here, where you don't want boundaries e.g. dms, duels, etc.
                {
                        if(IsPlayerInArea(i,Min[0], Max[0], Min[1], Max[1]) != 1) // check if the player is not in the area/zone.
                        {
                                GetPlayerHealth(i, HP); // gets player health
                                SetPlayerHealth(i, HP-5); // reduces -5%, every 1 second.
                                GameTextForPlayer(i, "~r~Stay inside boundaries~n~or you will be killed!", 3000, 3); // Shows a game text for the player who got out of the boundaries, 3000 = 3 seconds, 3 = font style.
                        }
                }
        }
}
Now go under OnGameModeExit() :
pawn Code:
KillTimer(maintimer); // kill the timer, just in case.
Full code:
pawn Code:
new MapZone[MAX_PLAYERS]; // we need this to identify the area/zone or whatever.
new Float:Max[2] = {569.5944,2742.9941};
new Float:Min[2] = {-838.9635,1163.5237};
public OnGameModeInit()
{
    MapZone = GangZoneCreate(Min[0],Min[1],Max[0],Max[1]);
    maintimer = SetTimer("MainTimer", 1000, true); // 1000 = one second. true = loops/repeats every 1 second.
    return 1;
}
public OnGameModeExit()
{
    KillTimer(maintimer); // kill the timer, just in case.
    return 1;
}
forward MainTimer();
public MainTimer()
{
        new Float:HP; // player health
        for(new i=0; i < MAX_PLAYERS; i++) // loops through players
        {
                if(IsPlayerConnected(i) && (GetPVarInt(i, "Spectate") == 0)) //You can add more exceptions here, where you don't want boundaries e.g. dms, duels, etc.
                {
                        if(IsPlayerInArea(i,Min[0], Max[0], Min[1], Max[1]) != 1) // check if the player is not in the area/zone.
                        {
                                GetPlayerHealth(i, HP); // gets player health
                                SetPlayerHealth(i, HP-5); // reduces -5%, every 1 second.
                                GameTextForPlayer(i, "~r~Stay inside boundaries~n~or you will be killed!", 3000, 3); // Shows a game text for the player who got out of the boundaries, 3000 = 3 seconds, 3 = font style.
                        }
                }
        }
}
Thank you for taking time to read this, and hope you got some useful information.
Feedback would be appreciated.
Reply


Messages In This Thread
Creating a boundaries system - by MythicalMarauder - 28.03.2014, 10:03
Re: Creating a boundaries system - by Lordzy - 28.03.2014, 10:20
Re: Creating a boundaries system - by MythicalMarauder - 28.03.2014, 10:50
Re: Creating a boundaries system - by Niko_boy - 28.03.2014, 11:53
Re: Creating a boundaries system - by MythicalMarauder - 28.03.2014, 15:47
Re: Creating a boundaries system - by Niko_boy - 29.03.2014, 16:17
Re: Creating a boundaries system - by RajatPawar - 30.03.2014, 12:54

Forum Jump:


Users browsing this thread: 2 Guest(s)