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

Solved Pass ConVar value to other plugins


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
shanapu
Veteran Member
Join Date: Apr 2015
Location: .de
Old 07-19-2018 , 14:14   Pass ConVar value to other plugins
Reply With Quote #1

I will work on a "master/core" plugin with some Cvars(8+).
I need to pass the values of these Cvars to other "slave plugins/modules".
The values are planned to read only once by the "slaves/modules" during OnConfigsExecuted().
There will be 10+ "slaves/modules" that need these values.

What is the "best", less expensive &/or smartest way?

FindConVar or native or even a forward? and why?
Or is there a better/other way I'm missing?


"FindConVar"-Way
Quote:
Core plugin
PHP Code:
public void OnPluginStart()
{
    
gc_sName CreateConVar("plugin_name""Name""");
    
gc_iTime CreateConVar("plugin_time""10.0""");

Less code on core.
Least code at all.

ok to make it foolproof I should hook convar change too.

this way many plugins call the same convars from convar-cache(?)

Module plugin
PHP Code:
public void OnConfigsExecuted()
{
    
ConVar cBuffer FindConVar("plugin_name");
    
cBuffer.GetString(g_sNamesizeof(g_sName));

    
cBuffer FindConVar("plugin_time");
    
g_fTime cBuffer.FloatValue;



"Native"-Way
Quote:
Core plugin
PHP Code:
public void OnPluginStart()
{
    
gc_sName CreateConVar("myplugin_name""Name""");
    
gc_fTime CreateConVar("myplugin_time""10.0""");
}

public 
APLRes AskPluginLoad2(Handle myselfbool latechar[] errorint err_max)
{
    
CreateNative("MyPlugin_Name"Native_Name);
    
CreateNative("MyPlugin_Time"Native_Time);

    
RegPluginLibrary("myplugin");
}

public 
int Native_Name(Handle pluginint args)
{
    
char sName[32];
    
gc_sName.GetString(sNamesizeof(sName));
    
SetNativeString(1sNamesizeof(sName));
}

public 
int Native_Time(Handle pluginint args)
{
    return 
gc_fTime.FloatValue;

more code on core - less work on modules

This way many api calls to the core module(?)

Module plugin
PHP Code:
#include myplugin

public void OnConfigsExecuted()
{
    
MyPlugin_Name(g_sName);

    
g_fTime MyPlugin_Time();



"Forward"-Way
Quote:
Core plugin
PHP Code:
public void OnPluginStart()
{
    
gc_sName CreateConVar("plugin_name""Name""");
    
gc_iTime CreateConVar("plugin_time""10.0""");
}

public 
APLRes AskPluginLoad2(Handle myselfbool latechar[] errorint err_max)
{
    
g_hSendConfig CreateGlobalForward("MyPlugin_Config"ET_IgnoreParam_StringParam_Cell);

    
RegPluginLibrary("myplugin");
}

public 
int OnConfigsExecuted()
{
    
char sName[32];
    
gc_sName.GetString(sNamesizeof(sName));

    
Call_StartForward(g_hSendConfig);
    
Call_PushString(sName);
    
Call_PushFloat(gc_fTime.FloatValue);
    
Call_Finish();

more code on core - less work on modules

This way the forward will only called once and all plugins will recieve the values.
I assume this is the less expensive way.

Module plugin
PHP Code:
#include myplugin

public void MyPlugin_Config(char[] namefloat time)
{
    
Format(g_sNamesizeof(g_sName), name);
    
g_fTime time;

I know there are maybe some missing lines, missing var declartions or rather HookConVarChanges.
These example codes are just for better visualization of the question.

From the point of writing new modules I would prefer natives or forwards.
My gut tells me to use forwards.

What do you think? Thanks for reading!
__________________
coding & free software

Last edited by shanapu; 07-21-2018 at 15:26.
shanapu is offline
Psyk0tik
Veteran Member
Join Date: May 2012
Location: Homeless
Old 07-19-2018 , 16:05   Re: Pass ConVar value to other plugins
Reply With Quote #2

I would personally go with natives since you can get and set values across plugins. With FindConVar you would have to rely on the server having the plugin/module with that convar. And since it's modular, that means having that plugin/module would be optional. At least with natives, you don't have to add checks to see if the plugin/module is even there or not. Just my view on it.

Edit: If you're just collecting data, forwards can work too.
__________________

Last edited by Psyk0tik; 07-19-2018 at 16:06.
Psyk0tik is offline
Neuro Toxin
Veteran Member
Join Date: Oct 2013
Location: { closing the void; }
Old 07-19-2018 , 18:25   Re: Pass ConVar value to other plugins
Reply With Quote #3

You can also do this using Dynamic (it uses natives).

https://github.com/ntoxin66/Dynamic/...ltiple-plugins

https://github.com/ntoxin66/Dynamic/...etting-Objects
__________________
Neuro Toxin is offline
Dr!fter
The Salt Boss
Join Date: Mar 2007
Old 07-19-2018 , 19:24   Re: Pass ConVar value to other plugins
Reply With Quote #4

I suggest FindConVar or forwards. There is no real point in creating additional natives, you can simply just store the convar handle globally once and then call the same natives anyway. The only positive of natives is that you have less code to write in each module, but its 2 lines and its one less native call (not that it really matters). Forwards are the best id say, and you can also force all submodules to update at any time by calling a forward (not sure if this is important).

You can also avoid calling FindConVar by using a forward when the cvar is created and forwarding the handle to all the submodules.

Last edited by Dr!fter; 07-19-2018 at 19:26.
Dr!fter is offline
shanapu
Veteran Member
Join Date: Apr 2015
Location: .de
Old 07-21-2018 , 15:26   Re: Pass ConVar value to other plugins
Reply With Quote #5

Thanks for all your answers!

Quote:
Originally Posted by Dr!fter View Post
... Forwards are the best id say, and you can also force all submodules to update at any time by calling a forward (not sure if this is important).
Even though this is not an important feature for this plugin, for me this is the decisive argument for Forwards in general.

Quote:
Originally Posted by Dr!fter View Post
... You can also avoid calling FindConVar by using a forward when the cvar is created and forwarding the handle to all the submodules.
And that's fantastic! I didn't even think of passing the handle instead of the values. In this way, the submodules, can even change the master convars, like FindConvar if required.

Thank you very much!

For me, this topic is solved, but I'm still open to hearing new arguments!

edit: here a test snippet of passing convars handles:

Quote:
Master:
PHP Code:
public void OnPluginStart()
{
    
gc_sName CreateConVar("plugin_name""Name""");
    
gc_fTime CreateConVar("plugin_time""10.0""");
}
public 
APLRes AskPluginLoad2(Handle myselfbool latechar[] errorint err_max)
{
    
g_hSendConfig CreateGlobalForward("MyPlugin_Config"ET_IgnoreParam_CellParam_Cell);
}

public 
void OnConfigsExecuted()
{
    
Call_StartForward(g_hSendConfig);
    
Call_PushCell(gc_sName);
    
Call_PushCell(gc_fTime);
    
Call_Finish();

Module
PHP Code:
ConVar gc_sName;
ConVar gc_fTime;

public 
void MyPlugin_Config(ConVar nameConVar time)
{
    
gc_sName name;
    
gc_fTime time;

__________________
coding & free software

Last edited by shanapu; 07-21-2018 at 15:29.
shanapu is offline
Dr!fter
The Salt Boss
Join Date: Mar 2007
Old 07-21-2018 , 20:54   Re: Pass ConVar value to other plugins
Reply With Quote #6

I’ll add some more notes.

Be careful with passing handles (I’m not entirely sure what happens if you pass the convar and the owner of the handle is unloaded.) I assume the handle is invalid after that but not sure.

If you’re going to use the same logic in multiple sub modules, make a separate sp that you can include in each that handles all of that. For example have all the forwards in a separate file so you only have to include it in each rather than copy pasta.

You can get creative with your forwards, have one forward for all your cvars and pass an enum value to identify which one it is and then store the handles in a array indexed by said enum.

There is lots of ways to change it up. In theory you can even forward a cloned handle to a methodmap that inherits from stringmap and have the stringmap store the handles and the values instead. Then make the methodmap have get/set functions.

Hope that made sense, typing is hard.

EDIT: if using the methodmap method a native would be better. You also wouldn’t have to worry about updating any data as the main plugin would do it. This probably simplifies everything a lot.

Last edited by Dr!fter; 07-21-2018 at 21:00.
Dr!fter is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 07-22-2018 , 01:16   Re: Pass ConVar value to other plugins
Reply With Quote #7

Quote:
Originally Posted by Dr!fter View Post
Be careful with passing handles (I’m not entirely sure what happens if you pass the convar and the owner of the handle is unloaded.) I assume the handle is invalid after that but not sure.
ConVar handles are owned by the SourceMod core.

Edit: Or more accurately, their owner is NULL and identity is a pointer to the SourceMod core, which amounts to the same thing.
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 07-22-2018 at 01:31.
Powerlord is offline
Dr!fter
The Salt Boss
Join Date: Mar 2007
Old 07-22-2018 , 07:06   Re: Pass ConVar value to other plugins
Reply With Quote #8

Quote:
Originally Posted by Powerlord View Post
ConVar handles are owned by the SourceMod core.

Edit: Or more accurately, their owner is NULL and identity is a pointer to the SourceMod core, which amounts to the same thing.
I knew the actual convar is, wasnt sure if the handle was owned by the plugin or not. But tested and it always returns the same handle as the one when created, so yea it is owned by SM.

Last edited by Dr!fter; 07-22-2018 at 07:07.
Dr!fter 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 06:59.


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