Raised This Month: $51 Target: $400
 12% 

Solved [L4D2] What's wrong with my timer handle?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Morning
Member
Join Date: May 2021
Old 12-14-2021 , 10:46   [L4D2] What's wrong with my timer handle?
Reply With Quote #1

Having a problem getting my timer to work properly. I'm trying to create chain kill system. If a player kills tanks in less than 5 second intervals, it should add them up until the chain breaks, and then reset. In my head this should work, and it appears to work for the first time but then halts. Here is the code, i've stripped it out of the main plugin for simplicity. Thanks for any help!

Code:
int ga_Chain[MAXPLAYERS + 1];
Handle ChainTimer[MAXPLAYERS+1];

public OnPluginStart()
{
	HookEvent("tank_killed", Event_TankKill);
}

public OnClientDisconnect(int client)  
{
	ga_Chain[client] = 0;
	delete ChainTimer[client];
}

public Event_TankKill(Event event, const char[] name, bool dontBroadcast)
{
        int attackerId = event.GetInt("attacker");
	int client = GetClientOfUserId(attackerId);
	if(!IsFakeClient(client))
	{
		ga_Chain[client]++;
		PrintToConsole(0, "%N killed a tank, %d in chain", client, ga_Chain[client]);
		delete ChainTimer[client];
		ChainTimer[client] = CreateTimer(5.0, EndChain, client, TIMER_FLAG_NO_MAPCHANGE);
	}
}		
		
public Action EndChain(Handle timer, int client)
{
        PrintToConsole(0, "%N chain finished, %d kills", client, ga_Chain[client]);
	ga_Chain[client] = 0;
	return Plugin_Stop;
}

Last edited by Morning; 01-31-2022 at 07:50.
Morning is offline
Silvers
SourceMod Plugin Approver
Join Date: Aug 2010
Location: SpaceX
Old 12-14-2021 , 11:43   Re: [L4D2] What's wrong with my timer handle?
Reply With Quote #2

You should be passing the userid into the timer (attackerId in this case) and verifying it with GetClientOfUserId in the callback, after which you should set ChainTimer[client] to null. Can't see anything else wrong.
__________________
Silvers is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 12-14-2021 , 11:58   Re: [L4D2] What's wrong with my timer handle?
Reply With Quote #3

- Don't use TIMER_FLAG_NO_MAPCHANGE, unless you clear Handle array manually on every map change.

- Remember clear Handle at Timer callback or delete will give you error when timer not exist anymore.

PHP Code:


Handle track_timers
[MAXPLAYERS+1// array 65 index
int commandexecuted[MAXPLAYERS+1]


public 
void OnPluginStart()
{
    
RegConsoleCmd("sm_test"test);
}


public 
Action test(int clientint args)
{

    
// No timer, began record command executed streak
    
if(track_timers[client] == null)
    {
        
commandexecuted[client] = 1;
        
track_timers[client] = CreateTimer(5.0mytimerclient);
        
        
// do not continue code
        
return Plugin_Handled;
    }



    
// We are at this point if there is already timer running.
    // reset timer
    
delete track_timers[client];
    
track_timers[client] = CreateTimer(5.0mytimerclient);


    
commandexecuted[client]++;


    return 
Plugin_Handled;
}


public 
Action mytimer(Handle timerany data)
{
    
// If timer is able to execute this callback, clear track_timers handle
    
track_timers[data] = null;

    
PrintToChatAll("Client index %i command executed %i times"datacommandexecuted[data]);
    
    
commandexecuted[data] = 0;

    return 
Plugin_Continue;

*edit
You could make this work without timers as well, recording current unix time
__________________
Do not Private Message @me

Last edited by Bacardi; 12-14-2021 at 12:36. Reason: nvm
Bacardi is offline
Morning
Member
Join Date: May 2021
Old 12-14-2021 , 15:17   Re: [L4D2] What's wrong with my timer handle?
Reply With Quote #4

It's working correctly now, much thanks Silvers & Bacardi
Morning is offline
Reply



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

Forum Jump


All times are GMT -4. The time now is 13:13.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode