SA-MP Forums Archive
Serious Discussion about TIMERS! - 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)
+---- Forum: Discussion (https://sampforum.blast.hk/forumdisplay.php?fid=84)
+---- Thread: Serious Discussion about TIMERS! (/showthread.php?tid=662422)



Serious Discussion about TIMERS! - NexyAG - 31.12.2018

Hello everyone, I really want to do something like this:
Code:
public OnPlayerConnect(playerid)
{
	SetTimerEx(funcname[], 650, true:flag, "d", playerid);
	//Example:
	SetTimerEx(funcname[], 650, true:AUTO_KILL_ON_PLAYER_DISCONNECT, "d", playerid);
	return 1;
}
I'm really wondering how can I make timer that kills when player disconnect in this case?
I know that i can create some custom function where i can make enum and than for each started player timer i give a some ++ increment for createdtimers and then just kill all through loop through enum, but that is not so efficient bcuz i cannot know if it is player timer, even if i do sth like flag, i need your opinion


Re: Serious Discussion about TIMERS! - CantBeJohn - 01.01.2019

So uh, you're looking to do this?:

PHP Code:
new
    
p_timername[MAX_PLAYERS];
public 
OnPlayerConnect(playerid)
{
    
p_timername[playerid] = SetTimerEx(. . .); // Your SetTimerEx here, SetTimerEx returns Timer IDs.
    
return 1;
}
public 
OnPlayerDisconnect(playeridreason)
{
    
KillTimer(p_timername[playerid]); // Kill that timer for the specific player.
    
return 1;




Re: Serious Discussion about TIMERS! - CantBeJohn - 01.01.2019

Quote:
Originally Posted by Y_Less
View Post
ptask
To add to what Y_Less means; pTask is a thing with y_timers. So, use y_timers.


Re: Serious Discussion about TIMERS! - NexyAG - 01.01.2019

CantBeJohn, you did not understand or better said i did not explained well, i know that with creating pvar than assigning that pvar to timer than when player disconnect just use killtimer(pvar), but i need timer to kill itself automatically, without this, it often happens to forget to kill timer and boom...
I know about y_timers but i do not know if they have this feature mentioned above ( kill ptask when player dc without any code needed by me )
EDIT : Lemme explain better:
I have LoginPlayer function, i need this timer to be executed when pconnect but if player dc before timer executed kill it automatically
EDIT 2 : Lemme write code to even more explain:
Code:
new PTimer[MAX_PLAYERS][500];
new UsedTimers[MAX_PLAYERS] = -1;

public OnPlayerConnect(playerid)
{
	SetPlayerTimer("LoginPlayer", 8000, false, "i", playerid);
}

public SetPlayerTimer(code..)
{
	PTimer[playerid][UsedTimers[playerid]] = SetTimerEx("LoginPlayer", 8000, false, "i", playerid);
	UsedTimers[playerid]++;
}

public OnPlayerDisconnect(playerid, reason)
{
	for(new i=0; i < UsedTimers[playerid]; i++)
	{
		KillTimer(PTimer[playerid][i]);
		UsedTimers[playerid] = -1;
	}
}
But this code above uses alot of memory ( i suppose ) and isn't so efficient bcuz timer id may be number far greater than 500 in this case...
I have some idea with enums without using arrays but still its not efficient enough


Re: Serious Discussion about TIMERS! - Spmn - 01.01.2019

Use y_timers' ptask or any plugin that provides per-player timers.


Re: Serious Discussion about TIMERS! - IllidanS4 - 01.01.2019

You can use PawnPlus to create a per-player Map or List, then use task_ms to represent the waiting task, create a new handle to it via handle_new, call handle_acquire on the handle, and store it in the map or list. Then, when the player disconnects, call map_delete_deep or list_delete_deep. This will delete the per-player collection and all its contents, thus release the handles and in turn destroy all tasks stored there, even though they were not completed yet.

Waiting on such a task will be cancelled and no further code will run.


Re: Serious Discussion about TIMERS! - NexyAG - 01.01.2019

Quote:
Originally Posted by IllidanS4
View Post
You can use PawnPlus to create a per-player Map or List, then use task_ms to represent the waiting task, create a new handle to it via handle_new, call handle_acquire on the handle, and store it in the map or list. Then, when the player disconnects, call map_delete_deep or list_delete_deep. This will delete the per-player collection and all its contents, thus release the handles and in turn destroy all tasks stored there, even though they were not completed yet.

Waiting on such a task will be cancelled and no further code will run.
I'm actually alredy using PawnPlus since the very beginning of the plugin, i was using only 1% of it ( wait_ms haha )


Re: Serious Discussion about TIMERS! - B3x7K - 18.01.2019

pTask or SetTimerEx...

Just

timer[playeird] = SetTimerEx bla bla on player connect.

then

PHP Code:
public OnPlayerDisconnect(playeridreason)
{
    for(new 
i=0UsedTimers[playerid]; i++)
    {
        
KillTimer(PTimer[playerid][i]);
                
// Are you lol?, if you put this on loop, it stops looping because variable i greater than -1.
        //UsedTimers[playerid] = -1;
    
}
       
//place in here, because if the variable i is greater than your UsedTimers, then the script continue read this until return 1;
        
UsedTimers[playerid] = -1;
        return 
1;