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

Solved [L4D2] How to deliver extra arguments for callback function in HookConVarChange func?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
desire worker
Junior Member
Join Date: Dec 2018
Location: Umbrella Corporation
Old 12-13-2018 , 10:00   [L4D2] How to deliver extra arguments for callback function in HookConVarChange func?
Reply With Quote #1

Code:
#pragma semicolon 1
#include <sourcemod>
#include <sdktools>
#include <sdktools_functions>
#include <sdkhooks>

new CurrentV[MAXPLAYERS+1];

public OnPluginStart()
{
		CurrentV[1] = CreateConVar("CurrentV[1]", "0", "current speed of player 1", FCVAR_PLUGIN|FCVAR_PROTECTED);
		CurrentV[2] = CreateConVar("CurrentV[2]", "0", "current speed of player 2", FCVAR_PLUGIN|FCVAR_PROTECTED);
		CurrentV[3] = CreateConVar("CurrentV[3]", "0", "current speed of player 3", FCVAR_PLUGIN|FCVAR_PROTECTED);
		CurrentV[4] = CreateConVar("CurrentV[4]", "0", "current speed of player 4", FCVAR_PLUGIN|FCVAR_PROTECTED);
		HookConVarChange(CurrentV[1], SpeedUpdater);
		HookConVarChange(CurrentV[2], SpeedUpdater);
		HookConVarChange(CurrentV[3], SpeedUpdater);
		HookConVarChange(CurrentV[4], SpeedUpdater);
}

public void SpeedUpdater(ConVar convar, char[] oldValue, char[] newValue, client) 
{
		//blah blah blah
}
I wanna deliver extra argument(in this case; client) for callback function in HookConVarChange function but

Code:
		HookConVarChange(CurrentV[1], SpeedUpdater(1));
		HookConVarChange(CurrentV[2], SpeedUpdater(2));
		HookConVarChange(CurrentV[3], SpeedUpdater(3));
		HookConVarChange(CurrentV[4], SpeedUpdater(4));
and

Code:
		HookConVarChange(CurrentV[1], SpeedUpdater, 1);
		HookConVarChange(CurrentV[2], SpeedUpdater, 2);
		HookConVarChange(CurrentV[3], SpeedUpdater, 3);
		HookConVarChange(CurrentV[4], SpeedUpdater, 4);
compiled both but

Both is yielding compile error.

How to deliver extra arguments for SpeedUpdater function?

Last edited by desire worker; 12-14-2018 at 08:21.
desire worker is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 12-13-2018 , 15:57   Re: [L4D2] How to deliver extra arguments for callback function in HookConVarChange f
Reply With Quote #2

You can't.

In your callback, you get the handle of the cvar, so you can use that to know which one it is. For example, you can check cvar == CurrentV[1], probably using a loop.
Fyren is offline
desire worker
Junior Member
Join Date: Dec 2018
Location: Umbrella Corporation
Old 12-13-2018 , 20:06   Re: [L4D2] How to deliver extra arguments for callback function in HookConVarChange f
Reply With Quote #3

like below?
Code:
public void SpeedUpdater(ConVar convar, char[] oldValue, char[] newValue, client) 
{
                new client
		for(client=1, client<GETMAXCLIENT(); client++)
               {
                          if(convar == CurrentV[client])
                                   break;
               } 
               func(client);
}
That means ConVar convar is kind of address(&convar) like c pointer?
desire worker is offline
Psyk0tik
Veteran Member
Join Date: May 2012
Location: Homeless
Old 12-14-2018 , 08:03   Re: [L4D2] How to deliver extra arguments for callback function in HookConVarChange f
Reply With Quote #4

The HookConVarChange()/ConVar.AddChangeHook() function uses a specific callback.

PHP Code:
typedef ConVarChanged = function void(ConVar convar, const char[] oldValue, const char[] newValue
First off, the callback that you're hooking has an extra parameter, which is the client, and that's ONE of the reasons you're getting compiler errors.

Second, the hook function itself only has 2 parameters (1 if you use the methodmap one), and in your code, you're passing 3 parameters.

Third, your ConVar handles aren't even handles...

Here's how I would do it:

PHP Code:
// old syntax
#include <sourcemod>

#pragma semicolon 1

new Handle:g_hConVars[MAXPLAYERS 1];

public 
OnPluginStart()
{
    
g_hConVars[1] = CreateConVar("CurrentV[1]""0.0""current speed of player 1"FCVAR_PROTECTED);
    
g_hConVars[2] = CreateConVar("CurrentV[2]""0.0""current speed of player 2"FCVAR_PROTECTED);
    
g_hConVars[3] = CreateConVar("CurrentV[3]""0.0""current speed of player 3"FCVAR_PROTECTED);
    
g_hConVars[4] = CreateConVar("CurrentV[4]""0.0""current speed of player 4"FCVAR_PROTECTED);

    for (new 
pos 1pos 5pos++)
    {
        
HookConVarChange(g_hConVars[pos], ConVars_Changed);
    }
}

public 
ConVars_Changed(Handle:convar, const String:oldVal[], const String:newVal[])
{
    for (new 
client 1client <= MaxClientsclient++)
    {
        if (
IsClientInGame(client) && convar == g_hConVars[client])
        {
            new 
Float:flNewSpeed StringToFloat(newVal);
            if (
flNewSpeed 0.0)
            {
                
SpeedUpdater(clientflNewSpeed);
            }
            else
            {
                
SpeedUpdater(client1.0);
            }
        }
    }
}

SpeedUpdater(clientFloat:value)
{
    
PrintToChatAll("Client %i: %N's new speed is: %.2f"clientclientvalue);

PHP Code:
// new syntax
#include <sourcemod>

#pragma semicolon 1
#pragma newdecls required

ConVar g_cvConVars[MAXPLAYERS 1];

public 
void OnPluginStart()
{
    
g_cvConVars[1] = CreateConVar("CurrentV[1]""0.0""current speed of player 1"FCVAR_PROTECTED);
    
g_cvConVars[2] = CreateConVar("CurrentV[2]""0.0""current speed of player 2"FCVAR_PROTECTED);
    
g_cvConVars[3] = CreateConVar("CurrentV[3]""0.0""current speed of player 3"FCVAR_PROTECTED);
    
g_cvConVars[4] = CreateConVar("CurrentV[4]""0.0""current speed of player 4"FCVAR_PROTECTED);

    for (
int iPos 1iPos 5iPos++)
    {
        
g_cvConVars[iPos].AddChangeHook(ConVars_Changed);
    }
}

public 
void ConVars_Changed(ConVar convar, const char[] oldVal, const char[] newVal)
{
    for (
int client 1client <= MaxClientsclient++)
    {
        if (
IsClientInGame(client) && convar == g_cvConVars[client])
        {
            
float flNewSpeed StringToFloat(newVal);
            if (
flNewSpeed 0.0)
            {
                
SpeedUpdater(clientflNewSpeed);
            }
            else
            {
                
SpeedUpdater(client1.0);
            }
        }
    }
}

void SpeedUpdater(int clientfloat value)
{
    
PrintToChatAll("Client %i: %N's new speed is: %.2f"clientclientvalue);

I apologize in advance if I made any typos or misunderstood something. It's late here.
__________________

Last edited by Psyk0tik; 12-14-2018 at 08:05.
Psyk0tik is offline
desire worker
Junior Member
Join Date: Dec 2018
Location: Umbrella Corporation
Old 12-14-2018 , 09:57   Re: [L4D2] How to deliver extra arguments for callback function in HookConVarChange f
Reply With Quote #5

oh that was`t handle... that`s why my convarchange function didn`t work.

thank you for your kind answer and code.
desire worker is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 12-14-2018 , 11:54   Re: [L4D2] How to deliver extra arguments for callback function in HookConVarChange f
Reply With Quote #6

Quote:
Originally Posted by desire worker View Post
That means ConVar convar is kind of address(&convar) like c pointer?
Handles are not pointers, they're more like handles in the Windows API or file descriptors.
Fyren is offline
Spirit_12
Veteran Member
Join Date: Dec 2012
Location: Toronto, CA
Old 12-14-2018 , 12:36   Re: [L4D2] How to deliver extra arguments for callback function in HookConVarChange f
Reply With Quote #7

Quote:
Originally Posted by Crasher_3637 View Post
PHP Code:
    for (int iPos 1iPos 5iPos++)
    {
        
g_cvConVars[iPos].AddChangeHook(ConVars_Changed);
    } 
Don't hardcode your loops when running them with non-static variables.

PHP Code:
    for (int iPosiPos sizeof g_cvConVarsiPos++)
    {
        
g_cvConVars[iPos].AddChangeHook(ConVars_Changed);
    } 
__________________
Spirit_12 is offline
Psyk0tik
Veteran Member
Join Date: May 2012
Location: Homeless
Old 12-14-2018 , 16:49   Re: [L4D2] How to deliver extra arguments for callback function in HookConVarChange f
Reply With Quote #8

Quote:
Originally Posted by Spirit_12 View Post
Don't hardcode your loops when running them with non-static variables.

PHP Code:
    for (int iPosiPos sizeof g_cvConVarsiPos++)
    {
        
g_cvConVars[iPos].AddChangeHook(ConVars_Changed);
    } 
You're right, but I did that in this example because I only created 5 convars. There's no point in hooking the rest if they don't even have a convar stored in them. The OP can adjust it as he/she sees fit.
__________________
Psyk0tik 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:23.


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