AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Global Shared CVARS - How To ? (https://forums.alliedmods.net/showthread.php?t=56035)

kurian 06-05-2007 03:23

Global Shared CVARS - How To ?
 
I have multiple plugins that share the same CVAR. They function independent of each other.

Any of these plugins can be used together so the problem arises when it comes to the CVAR being registered for the first time.

How do I register a CVAR before hand so that it is available for all plugins to use.

Right now I am doing this in all the plugins (Actually this block is being commonly included from a customized amxmisc.inc)

PHP Code:

    new amx_override_spec get_cvar_pointer("amx_override_spec")
    
    if (!
amx_override_spec)
    {
        
amx_override_spec register_cvar("amx_override_spec","1")
    } 

Basically every plugin checks if the CVAR is already declared and if not declares it. Any further plugins will see the declared CVAR. This isn't a function that is going to be executed repeatedly so the get_cvar_pointer inefficiency is not a problem.

Is there any way to declare a CVAR before hand so that its existence need not be checked each time. I don't really want to create a dummy plugin to do this. And even so it would have to be the FIRST entry in plugins.ini so that it gets executed first.

Is there any command or anything could be placed in amxx.cfg to create a CVAR ?

_Master_ 06-05-2007 07:02

Re: Global Shared CVARS - How To ?
 
I do believe I've seen something like a cvar, command manager round here but I think they might be a bit much for what you need. Just checking cvars in plugin_init() is better since server load is at it's minimum.

Also AMXX does NOT provide any mechanism of pre-declaring stand-alone cvars (it has something to do with cvar registration and owner plugin). Moving forward, you could edit an always-active amxx plugin and create the cvar there, leaving the rest of the plugins to just access it.

EDIT: Or you could declare the cvar pointer as public and thus fully exposing it to other plugins.

Lee 06-05-2007 08:33

Re: Global Shared CVARS - How To ?
 
Maybe I'm completely missing your problem, but..

Code:

register_cvar("a_cvar", "1");
new cvar_pointer = get_cvar_pointer("a_cvar");

register_cvar() already checks to see if the cvar has been declared previously. Whether this is better is debatable because if the cvar does not already exist then you've just discarded the return value which you will then be interested in.

If these plugins are only for personal use then why not register the cvar in admincmd.sma or one of the other base add-ons..? In fact, why is this such an issue in the first place? plugin_init() should be the last place you look at optimising. The rest of the code should be perfect before you even consider this.

pRED* 06-05-2007 17:14

Re: Global Shared CVARS - How To ?
 
You could register it in one of the plugins plugin_precache..

Then it will already be there in all of the plugin_init's, so you wont need to check if it exsits.

_Master_ 06-05-2007 21:15

Re: Global Shared CVARS - How To ?
 
Again:
Just doing this in plugin_init() is ok !!! The overload is irrelevalnt !!!

sawce 06-06-2007 00:23

Re: Global Shared CVARS - How To ?
 
Lee is completely correct.

It doesn't matter how many plugins register a cvar, if it has already been registered when another call is made, register_cvar() will act basically like get_cvar_pointer(), and give you the pointer to it for use with pcvars. The value of the already existing cvar will not be changed by subsequent calls to it via register_cvar().

Additionally, no plugins "own" a cvar. It is open for anything within the engine to use freely.

There is no need whatsoever to make a central repository of cvar pointers, or exposing cvar pointers via public variables.

kurian 06-06-2007 08:15

Re: Global Shared CVARS - How To ?
 
Quote:

Originally Posted by _Master_ (Post 486150)
Again:
Just doing this in plugin_init() is ok !!! The overload is irrelevalnt !!!

Actually I am making a modified include file that adds functionality to all existing plugins. All you have to do is recompile all your current plugins and i dont want people to have to write code into their existing plugins. I just want them to open the SMA and hit recompile. Thats why the entire content of this procedure MUST be in the include file.

This is the feature I was making

http://forums.alliedmods.net/showthread.php?t=55956

kurian 06-06-2007 08:23

Re: Global Shared CVARS - How To ?
 
Quote:

Originally Posted by sawce (Post 486175)
It doesn't matter how many plugins register a cvar, if it has already been registered when another call is made, register_cvar() will act basically like get_cvar_pointer(), and give you the pointer to it for use with pcvars. The value of the already existing cvar will not be changed by subsequent calls to it via register_cvar().

So what your saying is that I can simply write

Code:

amx_override_spec = register_cvar("amx_override_spec","1")
So the first time the function is called, the CVAR will be created and its value will be set to 1. Then suppose the user changes the value to 0 by typing amx_override_spec 0 in console. Then if the function is executed again in another plugin, it will simply return a CVAR pointer and the value will still remain at 0 even though register_cvar re initializes it with a value of 1 ?


If so then this seems to be the best option. Because I CANNOT add any code in the plugins themselves, everything must be contained in the include file.

sawce 06-06-2007 10:19

Re: Global Shared CVARS - How To ?
 
Quote:

Originally Posted by kurian (Post 486264)
So what your saying is that I can simply write

Code:

amx_override_spec = register_cvar("amx_override_spec","1")
So the first time the function is called, the CVAR will be created and its value will be set to 1. Then suppose the user changes the value to 0 by typing amx_override_spec 0 in console. Then if the function is executed again in another plugin, it will simply return a CVAR pointer and the value will still remain at 0 even though register_cvar re initializes it with a value of 1 ?


If so then this seems to be the best option. Because I CANNOT add any code in the plugins themselves, everything must be contained in the include file.

Correct.


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

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