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

Can I intercept an Event when it's about to fire?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
BoosterGold
Member
Join Date: Nov 2016
Old 03-08-2018 , 18:26   Can I intercept an Event when it's about to fire?
Reply With Quote #1

Is there a way to detect when an Event is about to fire and run some code before that happens? I realize this might sound like I'm trying to attempt time travel.

Specifically, in the event that Event_RoundEnd would fire, I want to stop the round from ending, do some stuff, then allow the Event_RoundEnd to continue.

I'm sorry if this doesn't make sense, I've been trying to solve a weird issue for days.
BoosterGold is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 03-08-2018 , 19:03   Re: Can I intercept an Event when it's about to fire?
Reply With Quote #2

Blocking the round end event isn't going to actually stop the round from ending. Events are just notifications. If what you really want is to just do something before the round end event fires (though the round may really be over already), you can use a pre-hook.

Edit: For CS:S or CS:GO, you can probably use CS_OnTerminateRound.

Last edited by Fyren; 03-08-2018 at 19:12.
Fyren is offline
cravenge
Veteran Member
Join Date: Nov 2015
Location: Chocolate Factory
Old 03-09-2018 , 00:43   Re: Can I intercept an Event when it's about to fire?
Reply With Quote #3

For games other than CS:S and CS:GO, you can do this:
PHP Code:
void ToggleRound(bool bPause false)
{
    
int client GetRandomClient();
    if (!
client || !IsClientInGame(client))
    {
        
client GetRandomClient();
        if (!
client || !IsClientInGame(client))
        {
            return;
        }
    }
    
    
FindConVar("sv_pausable").SetInt(1);
    if (
bPause)
    {
        
FakeClientCommand(client"setpause");
    }
    else
    {
        
FakeClientCommand(client"unpause");
    }
    
FindConVar("sv_pausable").SetInt(0);
}

int GetRandomClient()
{
    
int iClient 0;
    for (
int i 1<= MaxClientsi++)
    {
        if (
IsClientInGame(i))
        {
            
iClient i;
            break;
        }
    }
    return 
iClient;

cravenge is offline
Visual77
Veteran Member
Join Date: Jan 2009
Old 03-09-2018 , 09:00   Re: Can I intercept an Event when it's about to fire?
Reply With Quote #4

Quote:
Originally Posted by cravenge View Post
For games other than CS:S and CS:GO, you can do this:
PHP Code:
void ToggleRound(bool bPause false)
{
    
int client GetRandomClient();
    if (!
client || !IsClientInGame(client))
    {
        
client GetRandomClient();
        if (!
client || !IsClientInGame(client))
        {
            return;
        }
    }
    
    
FindConVar("sv_pausable").SetInt(1);
    if (
bPause)
    {
        
FakeClientCommand(client"setpause");
    }
    else
    {
        
FakeClientCommand(client"unpause");
    }
    
FindConVar("sv_pausable").SetInt(0);
}

int GetRandomClient()
{
    
int iClient 0;
    for (
int i 1<= MaxClientsi++)
    {
        if (
IsClientInGame(i))
        {
            
iClient i;
            break;
        }
    }
    return 
iClient;

That looks ok, but remember that FindConVar is really cpu expensive (if used multiple times in the same round). I assume this would only be used 2 times per round but still. For optimization, always cache the cvars in OnPluginStart and retrieve the value using ConVar.FloatValue or ConVar.IntValue.

Last edited by Visual77; 03-09-2018 at 09:05.
Visual77 is offline
cravenge
Veteran Member
Join Date: Nov 2015
Location: Chocolate Factory
Old 03-09-2018 , 20:40   Re: Can I intercept an Event when it's about to fire?
Reply With Quote #5

That source code was a month ago. Found it in my forked version of [L4D2] Scores/Teams Manager plugin, and the idea behind it was from a certain versus server that I frequently go to.
cravenge is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 03-10-2018 , 14:05   Re: Can I intercept an Event when it's about to fire?
Reply With Quote #6

Quote:
Originally Posted by Visual77 View Post
That looks ok, but remember that FindConVar is really cpu expensive (if used multiple times in the same round). I assume this would only be used 2 times per round but still. For optimization, always cache the cvars in OnPluginStart and retrieve the value using ConVar.FloatValue or ConVar.IntValue.
That is incorrect, it is just a hashtable lookup after the first call (by any plugin for each cvar).
__________________
asherkin is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 03-10-2018 , 15:29   Re: Can I intercept an Event when it's about to fire?
Reply With Quote #7

Quote:
Originally Posted by asherkin View Post
That is incorrect, it is just a hashtable lookup after the first call (by any plugin for each cvar).
In the past, we've been explicitly told by SourceMod devs, yourself included, to cache the return values from FindConVar because it's slow. Don't be surprised if people are going to continue saying that.
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
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

Forum Jump


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


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