SA-MP Forums Archive
Timer Bug - 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: Timer Bug (/showthread.php?tid=580820)



Timer Bug - SecretBoss - 08.07.2015

Hello I just created a question system and I want to make the question removing after 60 seconds so I created a timer

Код:
CMD:ask(playerid, params[])
{
    if(sscanf(params, "s[256]", Ask[playerid])) return SCM(playerid, -1, ""grey"Usage: "white"/ask [question]");
	if(AskInProgress[playerid] == true) return SCM(playerid, -1, ""red"[ERROR] "white"You have already an active question");
	
	new string[128];
	
	format(string, sizeof(string), ""orange"QUESTION from %s: "white"%s", pName(playerid), Ask[playerid]);
	SCMH(-1, string);
	
	AskInProgress[playerid] = true;
	SetTimer("deleteask", 5000, true);
	return 1;
}
then the callback

Код:
public:deleteask(playerid)
{
	if(AskInProgress[playerid] == true)
 	{
	    AskInProgress[playerid] = false;

	    SCM(playerid, -1, ""red"[INFO] "white"Your question has been automatically removed");
	    
	    SetTimer("deleteask", playerid, false);
	    
	}
    return 1;
}
I tested a lot of things but nothing worked


Re: Timer Bug - dusk - 08.07.2015

You don't pass the player id to deleteask. Do you think it will just magically appear?

If you want to pass arguments to a timer function, use SetTimerEx.

pawn Код:
SetTimerEx("deleteask", 5000, false, "i", playerid);

forward deleteask(playerid);
public deleteask(playerid)
{
    if(AskInProgress[playerid] == true)
    {
        AskInProgress[playerid] = false;

        SCM(playerid, -1, ""red"[INFO] "white"Your question has been automatically removed");
    }
    return 1;
}

Also, the 2nd parameter of SetTimer/SetTimerEx is if the timer should be repeated. If set to true, the function will be called EVERY 5 seconds untill you use KillTimer.


Re: Timer Bug - SecretBoss - 08.07.2015

Quote:
Originally Posted by dusk
Посмотреть сообщение
You don't pass the player id to deleteask. Do you think it will just magically appear?

If you want to pass arguments to a timer function, use SetTimerEx.

pawn Код:
SetTimerEx("deleteask", 5000, false, "i", playerid);

forward deleteask(playerid);
public deleteask(playerid)
{
    if(AskInProgress[playerid] == true)
    {
        AskInProgress[playerid] = false;

        SCM(playerid, -1, ""red"[INFO] "white"Your question has been automatically removed");
    }
    return 1;
}

Also, the 2nd parameter of SetTimer/SetTimerEx is if the timer should be repeated. If set to true, the function will be called EVERY 5 seconds untill you use KillTimer.
I used

Код:
KillTimer(deleteask(playerid));
and it was repeating, btw SetTimerEx is just for one time and SetTimer for repeating?


Re: Timer Bug - dusk - 08.07.2015

No, both SetTimerEx AND SetTimerEx can be used for repeating and non-repeating timers.

They will be repeating if you set the 3rd parameter to true.
pawn Код:
SetTimer("AFunction", 1000, true); // a repeating timer
SetTimer("AFunction", 1000, false); // a non repeating timer
SetTimerEx("AFunction", 1000, true, "i", 5); // a repeating timer
SetTimerEx("AFunction", 1000, false, "i", 5); // a non repeating timer

And that's not how you use KillTimer. SetTimer and SetTimerEx return timer IDs, which are neede in KillTimer. For example:

pawn Код:
new timerid = SetTimer("AFunction", 1000, true);
KillTimer(timerid);



Re: Timer Bug - SecretBoss - 08.07.2015

Quote:
Originally Posted by dusk
Посмотреть сообщение
No, both SetTimerEx AND SetTimerEx can be used for repeating and non-repeating timers.

They will be repeating if you set the 3rd parameter to true.
pawn Код:
SetTimer("AFunction", 1000, true); // a repeating timer
SetTimer("AFunction", 1000, false); // a non repeating timer
SetTimerEx("AFunction", 1000, true, "i", 5); // a repeating timer
SetTimerEx("AFunction", 1000, false, "i", 5); // a non repeating timer

And that's not how you use KillTimer. SetTimer and SetTimerEx return timer IDs, which are neede in KillTimer. For example:

pawn Код:
new timerid = SetTimer("AFunction", 1000, true);
KillTimer(timerid);
oh ok got it, thanks for info I am new on timers, to be honest its my first timer xD thanks for all the info +rep for you