SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Discussion

Reply
 
Thread Tools Display Modes
Old 31/12/2018, 11:36 PM   #1
NexyAG
Banned
 
Join Date: Oct 2017
Posts: 23
Reputation: 0
Default Serious Discussion about TIMERS!

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
NexyAG is offline   Reply With Quote
Old 01/01/2019, 12:03 AM   #2
CantBeJohn
Big Clucker
 
CantBeJohn's Avatar
 
Join Date: Oct 2016
Posts: 107
Reputation: 15
Default Re: Serious Discussion about TIMERS!

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;

CantBeJohn is offline   Reply With Quote
Old 01/01/2019, 03:49 AM   #3
CantBeJohn
Big Clucker
 
CantBeJohn's Avatar
 
Join Date: Oct 2016
Posts: 107
Reputation: 15
Default Re: Serious Discussion about TIMERS!

Quote:
Originally Posted by ****** View Post
ptask
To add to what ****** means; pTask is a thing with y_timers. So, use y_timers.
CantBeJohn is offline   Reply With Quote
Old 01/01/2019, 10:49 AM   #4
NexyAG
Banned
 
Join Date: Oct 2017
Posts: 23
Reputation: 0
Default Re: Serious Discussion about TIMERS!

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
NexyAG is offline   Reply With Quote
Old 01/01/2019, 12:21 PM   #5
Spmn
Gangsta
 
Join Date: Jun 2015
Location: Romania
Posts: 516
Reputation: 131
Default Re: Serious Discussion about TIMERS!

Use y_timers' ptask or any plugin that provides per-player timers.
Spmn is online now   Reply With Quote
Old 01/01/2019, 12:46 PM   #6
IllidanS4
Huge Clucker
 
IllidanS4's Avatar
 
Join Date: Feb 2013
Posts: 340
Reputation: 260
Default Re: Serious Discussion about TIMERS!

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.
__________________
PawnPlus
YSF
Yet Another Lua Plugin
Long Function Names
 i_quat.inc 


kingsofsa.cz:8888 running Cinematic Mode
IllidanS4 is offline   Reply With Quote
Old 01/01/2019, 01:07 PM   #7
NexyAG
Banned
 
Join Date: Oct 2017
Posts: 23
Reputation: 0
Default Re: Serious Discussion about TIMERS!

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 )
NexyAG is offline   Reply With Quote
Old 18/01/2019, 11:44 PM   #8
B3x7K
Big Clucker
 
B3x7K's Avatar
 
Join Date: Aug 2017
Location: 惑星黒トカゲ
Posts: 67
Reputation: 3
Default Re: Serious Discussion about TIMERS!

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;

__________________

HOW TO ENGLISH?!
AAAAAAAAAAAAAAAA

B3x7K is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Include] inline-timers: Use y_inline functions with timers! iKarim Includes 5 21/04/2018 07:16 AM
discussion astanalol Everything and Nothing 11 11/03/2017 02:24 PM
Deathbug discussion? FireCat Discussion 20 06/07/2012 06:51 AM
What next? (My PC discussion) Smithy Everything and Nothing 14 19/01/2012 08:34 PM
Want to Have a discussion and solution !! FaSihYouSf Scripting Help 4 04/12/2011 01:37 PM


All times are GMT. The time now is 03:23 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.