SA-MP Forums Archive
Killing a timer on repeat. - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Killing a timer on repeat. (/showthread.php?tid=322288)



Killing a timer on repeat. - Robert_Crawford - 01.03.2012

If you kill a timer when it is set to repeat will it continue to repeat or does that kill it completely?


Re: Killing a timer on repeat. - [XST]O_x - 01.03.2012

It will kill it completely, of course. (If I understood your meaning correctly)


Re: Killing a timer on repeat. - Robert_Crawford - 01.03.2012

I have a timer implementing objects, how can i make it it cancel from implementing the objects if there are no players connected.


Re: Killing a timer on repeat. - aco_SRBIJA - 01.03.2012

I have another question.

When Kill timer, is it still existing? If no, how to define times of repeating? (I know for stopcode, but if I would like to run once again...First time 5 repeats, then killing him, and after something, on command calling to run again with 5 times of repeat?)


Re: Killing a timer on repeat. - Robert_Crawford - 02.03.2012

Well you could stack the timers.


Re: Killing a timer on repeat. - Walsh - 02.03.2012

Example
pawn Code:
new
    Timer,
    TimerCount = 0; // Sets the timer count to zero.
CMD:start(playerid,params[])
{
    Timer = SetTimer("YourFunction",5000,true); // Repeats a timer of 5 seconds
    return 1;
}

forward YourFunction();
public YourFunction() // Every 5 seconds....
{
    TimerCount++; // Timer count is added. The Timer will repeat itself until it matched the below if statement.
    if(TimerCount == 5) // If the timer has been repeated five times.
    {
        KillTimer(Timer); // Then kill the timer.
        return 1;
    }
}



Re: Killing a timer on repeat. - Robert_Crawford - 02.03.2012

now how could i make sure that the timer does perform its function if a player is not connected?


Re: Killing a timer on repeat. - Walsh - 02.03.2012

You would add the timer under OnPlayerConnect, and then under your timer function loop through all the players and check if the any players are connected. Heres the example. This should work.
pawn Code:
new Timer;

forward YourFunction();
public YourFunction() // Every 5 seconds....
{
    // Object Code here
    for(new i = 0; i < MAX_PLAYERS; i++) // Loops through all the players
    {
       
        if(!IsPlayerConnected(i)) // If IsPlayerConnected for all players returns false.
        {
            KillTimer(Timer); // Then kill the timer.
        }
    }
    return 1;
}
public OnPlayerConnect(playerid)
{
    Timer = SetTimer("YourFunction",5000,true); // Repeats a timer of 5 seconds
    return 1;
}



Re: Killing a timer on repeat. - Robert_Crawford - 02.03.2012

now wouldn't it happen if the player disconnected?


Re: Killing a timer on repeat. - Walsh - 02.03.2012

I found a better way that actually works using a PlayerCount variable. (duh)
pawn Code:
new Timer,PlayerCount;
public OnGameModeInit()
{
    PlayerCount = 0; // Whenever the server is executed, player count will be zero
    return 1;
}
public OnPlayerConnect(playerid)
{
    Timer = SetTimer("YourFunction",5000,true); // Repeats a timer of 5 seconds
    PlayerCount++; // Whenever a player connects player count will inscrease by 1.
    return 1;
}
public OnPlayerDisconnect(playerid)
{
    PlayerCount--; // When a player disconnects player count will decrease by 1.
    return 1;
}
forward YourFunction();
public YourFunction() // Every 5 seconds....
{
    // Object Code here
    if(PlayerCount == 0) // If no players are on then....
    {
        KillTimer(Timer); // kill the timer.
    }
    return 1;
}