Killing a timer on repeat.
#1

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

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

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

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?)
Reply
#5

Well you could stack the timers.
Reply
#6

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;
    }
}
Reply
#7

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

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;
}
Reply
#9

now wouldn't it happen if the player disconnected?
Reply
#10

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;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)