SA-MP Forums Archive
How to activate a timer - 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: How to activate a timer (/showthread.php?tid=676044)



How to activate a timer - Calinut200 - 28.05.2020

How to activate a timer like that only if a variable is 1;
PHP Code:
ptask name[1000](playerid
{
    
// my code

This is the timer from YNI includes, i wanna work only work if a variable test[playerid] = 1;
how to make that? I don't wanna to work in background each second and do nothing


Re: How to activate a timer - hopeonxanny - 28.05.2020

You mean server variable or player variable?


Re: How to activate a timer - jasperschellekens - 28.05.2020

pawn Code:
if(test[playerid] == 1) // Run the timer here, e.g SetTimer and the timer is only activated when player var is 1



Re: How to activate a timer - SharpenBlade - 28.05.2020

Quote:
Originally Posted by Calinut200
View Post
How to activate a timer like that only if a variable is 1;
PHP Code:
ptask name[1000](playerid
{
    
// my code

This is the timer from YNI includes, i wanna work only work if a variable test[playerid] = 1;
how to make that? I don't wanna to work in background each second and do nothing
You could create a player timer then.
PHP Code:
new Timer:mytimer[MAX_PLAYERS] = {Timer:-1, ...}; 
In your function, where you set that variable to 1 you should set the timer also.
PHP Code:
if(MyVariable[playerid] == 1)
    
mytimer[playerid] = repeat MyFunction(playerid); 
OnPlayerDisconnect, or when you put that variable on 0:
PHP Code:
if(mytimer[playerid] != Timer:-1)
{
    
stop mytimer[playerid];
    
mytimer[playerid] = Timer:-1;