Timer problem
#1

Hello, I got a problem with some timers. When you enter a specific area where you are not allowed, you start to loose health. You start to loose health, but it wont stop, even if you go away.

Here is the code:

Код:
new bool:IsInArea[MAX_PLAYERS];
new HealthTimer[MAX_PLAYERS];

forward areacheck();
forward health(playerid);

public OnGameModeInit()
{
SetTimer("areacheck", 200, 1);
}

public areacheck()
{
    for (new i=0; i<MAX_PLAYERS; i++)
    {
        if (IsPlayerConnected(i))
        {
            if (IsPlayerInRangeOfPoint(put things here) && IsInArea[i]==false)
            {
                HealthTimer[i]=SetTimerEx("health", 400, 1, "i", i);
                IsInArea[i]=true;
            }
            else
            {
                if (IsInArea[i]==true)
                {
                    IsInArea[i]=false;
                    KillTimer(HealthTimer[i]);
                }
            }
        }
    }
}

public health(playerid)
{
    new Float:h;
    GetPlayerHealth(playerid, h);
    SetPlayerHealth(playerid, h-5)
}
I cant figure out whats wrong.

Any help would be appreciated
Reply
#2

You dont need 2 arrays

pawn Код:
new MaxPlayers;
new HealthTimer[MAX_PLAYERS] = {-1, ...};

forward areacheck();
forward health(playerid);

public OnGameModeInit()
{
    MaxPlayers = GetMaxPlayers();
    SetTimer("areacheck", 500, 1);
    return 1;
}
OnPlayerDisconnect
pawn Код:
if(HealthTimer[playerid] != -1)
{
    KillTimer(HealthTimer[playerid]);
    HealthTimer[playerid] = -1;
}
pawn Код:
public health(playerid)
{
    new Float:h;
    GetPlayerHealth(playerid, h);
    SetPlayerHealth(playerid, h-5);
    return 1;
}

public areacheck()
{
    static State;
    for(new i = 0; i != MaxPlayers; i++)
        if(IsPlayerConnected(i))
        {
            State = GetPlayerState(i);
            if(PLAYER_STATE_ONFOOT <= State <= PLAYER_STATE_PASSENGER && IsPlayerInRangeOfPoint(put things here))
            {
                if(HealthTimer[i] < 0)
                    HealthTimer[i] = SetTimerEx("health", 400, true, "i", i);
            }
            else
            {
                if(HealthTimer[i] > -1)
                {
                    KillTimer(HealthTimer[i]);
                    HealthTimer[i] = -1;
                }
            }
        }

    return 1;
}
Reply
#3

Not working now either, It worked before, You started to loose health, but when you got away, it wouldnt stop.
Reply
#4

Works good, try to increase the disctance in IsPlayerInRangeOfPoint

//EDIT
Use better version up
Reply
#5

Why wouldn't you just use OnPlayerEnterDynamicArea to start a timer and OnPlayerLeaveDynamicArea to stop it?
The callbacks are a part of the Streamer Plugin.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)