SetTimer and SetTimerEx -
Penguin1997 - 14.08.2016
I've alot of doubts related to these two functions and wiki is not helping. Id 0 plays a role in it too, why I think that is because I tested it months ago.
Lets say we've a timer
Code:
SetTimer("Function",5000,1);
Q1. Is the timer looping through all players ?
Q2. Is the timer looping through only ID 0 ?
Q3. If ID 0 logs out will it stop working ?
Now if we look at this timer, here we attach a global variable:
Code:
new global;
global = SetTimer("Function",5000,1);
Q4. Is the timer looping through all players ?
Q5. Is the timer looping through only ID 0 ?
Q6. If ID 0 logs out will it stop working ?
Q7. What difference it would make when if we equal it to a localvar (
new localvar[MAX_PLAYERS]) ?
What I think is, when you equal a
global or
player (new
playervar[MAX_PLAYERS]) var with a timer it usually is to kill a timer when a specific condition is reached if I'm not wrong.
Have a look at SetTimerEx now:
Code:
SetTimerEx("Function2", 5000, false, "i", playerid);
Q8. Is the timer looping through all players or just the specific player?
Q9. If we equal it to a global or local var (
new localvar[MAX_PLAYERS]) what difference it can possibly make rather than stopping when a condition is reached ?
I would really appreciate if someone clarify these questions.
Re: SetTimer and SetTimerEx -
Vince - 14.08.2016
Timers have absolutely nothing to do with loops whatsoever. It all depends on what you're doing in the callback. SetTimerEx differs from SetTimer only in that it can pass additional parameters to the callback. These paremeters do not necessarily need to include a playerid, although they usually do.
If a timer is pertaining to an individual player then SetTimerEx is used and if it's a timer that potentially needs to be killed later then the id of that timer is stored in a player-array. Repeating timers are usually killed in the callback that they first called although this does not have to be the case. One example is holding a key for at least 5 seconds:
PHP Code:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
if(PRESSED(KEY_SPRINT))
{
gKeyTimer[playerid] = SetTimerEx("KeyHeldAction", 5000, false, "i", playerid);
}
if(RELEASED(KEY_SPRINT))
{
KillTimer(gKeyTimer[playerid]);
}
return 1;
}
public KeyHeldAction(playerid)
{
SendClientMessage(playerid, -1, "You held ~k~~PED_SPRINT~ for 5 seconds!");
return 1;
}
If the player first presses the key down the timer gets activated. If the player releases the key before the time is up the timer will be killed and the callback will not be called.
Re: SetTimer and SetTimerEx -
Penguin1997 - 15.08.2016
Thanks and 1 more question, is the timer auto killed if player logs out.
Example #1:
Code:
OnPlayerConnect(playerid)
{
SetTimerEx("Function2", 120000, false, "i", playerid);
return 1;
}
Example #2:
Code:
new timergoing[MAX_PLAYERS];
OnPlayerConnect(playerid)
{
timergoing[playerid] = SetTimerEx("Function2", 120000, false, "i", playerid);
return 1;
}
Example #3:
Code:
OnPlayerConnect(playerid)
{
SetTimer("Function",80000,false);
return 1;
}
Funtions:
Code:
Function (playerid)
{
// code
return 1;
}
Function2 ()
{
// code
return 1;
}
Re: SetTimer and SetTimerEx -
Stinged - 15.08.2016
Timers have nothing to do with players. You can pass a playerid parameter if you want, but still they run no matter what.
If the timer doesn't repeat (.repeating = false), it will automatically get killed whenever it gets called.
So if you want to kill a timer (that is repeating) that passes the player's id as a parameter, you have to do that yourself under OnPlayerDisconnect.
Re: SetTimer and SetTimerEx -
Logic_ - 15.08.2016
To kill a timer, you will need to put its value in a variable, and use KillTimer to kill it. You must use Player pools or foreach to loop through the players.
I have a question too, is a repeating timer, killed when the server is exited or a gamemode is changed or restarted?
Re: SetTimer and SetTimerEx -
Penguin1997 - 15.08.2016
What I did to most of my timers on server is equal them to a variable and they are activated on certain callbacks like on OnPlayerConnect, OnPlayerKeyState, Infection etc, and they are not repeated timers, however I did something extra just to end all timers in the end in case if any one is currently ongoing, but they do end up after certain period of time as I added KillTimer function inside the timer function also, In the end I killed them all on OnPlayerDisconnect as I don't want timers to mess up the server performance. As for the public timers for anticheats etc, I equal them to a global variable and use SetTimer for it, and kill the global variable when gamemode exit.
Re: SetTimer and SetTimerEx -
Stinged - 15.08.2016
There's a huge possibility that I'm wrong but I think killing timers under OnGameModeExit is useless because everything gets killed by that point.
Re: SetTimer and SetTimerEx -
Kar - 18.08.2016
Quote:
Originally Posted by Stinged
There's a huge possibility that I'm wrong but I think killing timers under OnGameModeExit is useless because everything gets killed by that point.
|
gmxing
Re: SetTimer and SetTimerEx -
Mister0 - 19.08.2016
this is interesting, and it can be dezolvolted this disctution
Re: SetTimer and SetTimerEx - WhiteGhost - 19.08.2016
Quote:
Originally Posted by Stinged
There's a huge possibility that I'm wrong but I think killing timers under OnGameModeExit is useless because everything gets killed by that point.
|
Destory everything When the GameMode ends is a good thing,i was told that it helps to clear the code..
Alot of person still dont even destory pickups when the gamemodeends..
PHP Code:
new PC;
public OnGameModeInit()
{
PC = CreatePickup(1210,1,0.0,0.0,0.0,0);
return 1;
}
public OnGameModeExit()
{
DestroyPickup(PC);
return 1;
}
public OnPlayerDisconnect(playerid, reason)
{
DestroyPickup(PC);//x?.?
return 1;
}