Raised This Month: $32 Target: $400
 8% 

Solved Repeatable Timer & Client Index


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
ThatKidWhoGames
Veteran Member
Join Date: Jun 2013
Location: IsValidClient()
Old 09-12-2018 , 08:15   Repeatable Timer & Client Index
Reply With Quote #1

Hello!

I have a quick question in regard to what I am coding:
PHP Code:
#include <sourcemod>

#define CONFIG "addons/sourcemod/configs/commandcooldown.cfg"

StringMap g_hHashMap;
Handle g_hTimer[MAXPLAYERS+1];
int g_iTimeleft[MAXPLAYERS+1];

public 
void OnPluginStart()
{
    
BrowseKeyValues();
}

public 
void OnClientDisconnect(int client)
{
    
delete g_hTimer[client];
    
g_iTimeleft[client] = 0;
}

public 
Action OnCommand(int client, const char[] commandint argc)
{
    if (
g_iTimeleft[client])
    {
        
ReplyToCommand(client"[SM] You cannot use this command again for %i seconds."g_iTimeleft[client]);
        return 
Plugin_Handled;
    } else {
        
g_hHashMap.GetValue(commandg_iTimeleft[client]);
        
g_hTimer[client] = CreateTimer(1.0Timer_CallbackclientTIMER_REPEAT);
    }

    return 
Plugin_Continue;
}

public 
Action Timer_Callback(Handle timerint client)
{
    
g_iTimeleft[client]--;
    if (!
g_iTimeleft[client])
    {
        
g_hTimer[client] = null;
        return 
Plugin_Stop;
    }

    return 
Plugin_Continue;
}

void BrowseKeyValues()
{
    
delete g_hHashMap;
    
KeyValues kv = new KeyValues("Command Cooldown");
    if (!
FileExists(CONFIG))
    {
        
kv.SetString("kill""30");
        
kv.Rewind();
        
kv.ExportToFile(CONFIG);
    }

    if (!
kv.ImportFromFile(CONFIG) || !kv.GotoFirstSubKey(false))
    {
        
delete kv;
        
SetFailState("Failure parsing the config file %s!"CONFIG);
    }

    
g_hHashMap = new StringMap();
    
char command[64];
    
int cooldown;
    do {
        
kv.GetSectionName(commandsizeof(command));
        
kv.GotoFirstSubKey(false);
        
cooldown kv.GetNum(NULL_STRING);
        
g_hHashMap.SetValue(commandcooldown);
        
AddCommandListener(OnCommandcommand);
        
LogMessage("Added %i second cooldown for command %s."cooldowncommand);
    } while 
kv.GotoNextKey(false);
    
delete kv;

Do I need to pass the client's UserID instead of their index or not (in the CreateTimer() function)? By my understanding, since I am assigning the timer handle to each client specifically, then I don't need to.

Thanks,
Grant

Last edited by ThatKidWhoGames; 09-12-2018 at 10:22.
ThatKidWhoGames is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 09-12-2018 , 10:17   Re: Repeatable Timer & Client Index
Reply With Quote #2

The reason for passing user IDs instead of client IDs is to ensure that it's the same client once the timer callback actually happens. In your case, you're killing the timers when they disconnect, which achieves the same thing.
Fyren is offline
ThatKidWhoGames
Veteran Member
Join Date: Jun 2013
Location: IsValidClient()
Old 09-12-2018 , 10:21   Re: Repeatable Timer & Client Index
Reply With Quote #3

Quote:
Originally Posted by Fyren View Post
The reason for passing user IDs instead of client IDs is to ensure that it's the same client once the timer callback actually happens. In your case, you're killing the timers when they disconnect, which achieves the same thing.
Thanks!
ThatKidWhoGames 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 21:47.


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