Raised This Month: $ Target: $400
 0% 

How to distinguish public variables with the same name?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Leech_v2
Senior Member
Join Date: Mar 2011
Location: Chinese GuangDong
Old 10-06-2023 , 15:55   How to distinguish public variables with the same name?
Reply With Quote #1

Multiple plugins have declared public variables with the same name.

How to distinguish them? How to change a public variable in a specified plugin?

Or how can each plugin declare a public constant with the same name, just like the MaxClients public constant, and their values are set uniformly by one of the plugins?

The callfunc_* series of functions can distinguish between public functions of different plugins, while public variables can only be searched by name, which is really puzzling.

Moreover, plugins cannot access whether other plugins already have this public variable during compilation, so it is impossible to automatically declare symbols with different names..
Code:
// Plugin A:
public gPublicVar;
Code:
// Plugin B:
public gPublicVar;
Code:
// Plugin C:
public plugin_init()
{
    for (new pluginId = get_pluginsnum(); pluginId--;)
    {
        SetPublicVar(pluginId, /* public variable index */, 150);
    }
}
Leech_v2 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 10-07-2023 , 00:35   Re: How to distinguish public variables with the same name?
Reply With Quote #2

You can get and set public variables using the xvar functions e.g. set_xvar_num() or you can view their documentation directly in amxmodx.inc.
__________________

Last edited by fysiks; 10-07-2023 at 00:45.
fysiks is offline
Leech_v2
Senior Member
Join Date: Mar 2011
Location: Chinese GuangDong
Old 10-07-2023 , 00:59   Re: How to distinguish public variables with the same name?
Reply With Quote #3

Quote:
Originally Posted by fysiks View Post
You can get and set public variables using the xvar functions e.g. set_xvar_num() or you can view their documentation directly in amxmodx.inc.
I know, but they cannot distinguish between public variables with the same name in different plugins
Leech_v2 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 10-07-2023 , 02:28   Re: How to distinguish public variables with the same name?
Reply With Quote #4

That doesn't make sense. If they have the same name then they are supposed to be the same value. If you won't want the variable to conflict with other plugins then give it a different name for your plugins.
__________________
fysiks is offline
Leech_v2
Senior Member
Join Date: Mar 2011
Location: Chinese GuangDong
Old 10-07-2023 , 04:00   Re: How to distinguish public variables with the same name?
Reply With Quote #5

Quote:
Originally Posted by fysiks View Post
That doesn't make sense. If they have the same name then they are supposed to be the same value. If you won't want the variable to conflict with other plugins then give it a different name for your plugins.
In actual testing, 'set_xvar_num' only changes the public variables of the first plugin.
Moreover, plugins cannot know in advance whether there are public variables with the same name (runtime expressions cannot be used during compilation),
so it is impossible to avoid situations with the same name.
Leech_v2 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 10-07-2023 , 15:36   Re: How to distinguish public variables with the same name?
Reply With Quote #6

Quote:
Originally Posted by Leech_v2 View Post
In actual testing, 'set_xvar_num' only changes the public variables of the first plugin.
I just tested with three plugins and they all stay in-sync:

PHP Code:
#include <amxmodx>

public MyPublicVar

new g_pPublicVar
new g_iCount

public plugin_init()
{
    
set_task(1.0"test1display", .flags="b")
    
g_pPublicVar get_xvar_id("MyPublicVar")
}

public 
test1display()
{
    
g_iCount++
    
set_xvar_num(g_pPublicVarg_iCount)
    
server_print("test1: %d"get_xvar_num(g_pPublicVar))

PHP Code:
#include <amxmodx>

public MyPublicVar

new g_pPublicVar

public plugin_init()
{
    
set_task(1.0"test2display", .flags="b")
    
g_pPublicVar get_xvar_id("MyPublicVar")
}

public 
test2display()
{
    
server_print("test2: %d"get_xvar_num(g_pPublicVar))

PHP Code:
#include <amxmodx>

public MyPublicVar

new g_pPublicVar

public plugin_init()
{
    
set_task(1.0"test3display", .flags="b")
    
g_pPublicVar get_xvar_id("MyPublicVar")
}

public 
test3display()
{
    
server_print("Test3: %d"get_xvar_num(g_pPublicVar))



Quote:
Originally Posted by Leech_v2 View Post
Moreover, plugins cannot know in advance whether there are public variables with the same name (runtime expressions cannot be used during compilation),
so it is impossible to avoid situations with the same name.
It's your responsibility as the developer of your own plugins to choose a sufficiently unique name to prevent any collisions like this. There is no way to get around this.
__________________
fysiks is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 10-07-2023 , 15:51   Re: How to distinguish public variables with the same name?
Reply With Quote #7

Ok, so I did some more testing after my last post because I thought of a scenario that you may be trying to use (I can't be sure because you didn't provide any code).

It appears that the system doesn't work like I think you're expecting (i.e. it doesn't seem to work in the same way that MaxClients does, unfortunately). It turns out that you only declare the public variable in one plugin and then all other plugins require the use of the xvar functions. Here is those three plugins modified to show a couple different things:

PHP Code:
#include <amxmodx>

public MyPublicVar

new g_iCount

public plugin_init()
{
    
set_task(1.0"test1display", .flags="b")
}

public 
test1display()
{
    
g_iCount++
    
MyPublicVar g_iCount
    server_print
("test1: %d"MyPublicVar)

PHP Code:
#include <amxmodx>

new g_pPublicVar

public plugin_init()
{
    
set_task(1.0"test2display", .flags="b")
    
g_pPublicVar get_xvar_id("MyPublicVar")
}

public 
test2display()
{
    
server_print("test2: %d"get_xvar_num(g_pPublicVar))

PHP Code:
#include <amxmodx>

public MyPublicVar

new g_pPublicVar

public plugin_init()
{
    
set_task(1.0"test3display", .flags="b")
    
g_pPublicVar get_xvar_id("MyPublicVar")
}

public 
test3display()
{
    
server_print("Test3: %d, %d"get_xvar_num(g_pPublicVar), MyPublicVar)

The "MyPublicVar" variable in the third plugin does not get its value updated ever which is what I can only assume is what you were trying to do.
__________________

Last edited by fysiks; 10-07-2023 at 16:15. Reason: typo
fysiks is offline
Leech_v2
Senior Member
Join Date: Mar 2011
Location: Chinese GuangDong
Old 10-08-2023 , 03:46   Re: How to distinguish public variables with the same name?
Reply With Quote #8

Quote:
Originally Posted by fysiks View Post
...I can only assume is what you were trying to do.
In fact, xvar only accesses public variables in the first plugin, while all public variables in subsequent plugins maintain their default values

The conclusion is that they cannot access the changed values like MaxClients (without the xvar function),
PHP Code:
#include <amxmodx>
public plugin_init()
{
    
register_plugin("Plugin 1""1.0.0""Oinling");
    
server_print("[AMXX] [plugin 1] MaxClients : %d"MaxClients);
}
//////////////////////////////////////////
#include <amxmodx>
public plugin_init()
{
    
register_plugin("Plugin 2""1.0.0""Oinling");
    
server_print("[AMXX] [plugin 2] MaxClients : %d"MaxClients);
}
//////////////////////////////////////////
#include <amxmodx>
public plugin_init()
{
    
register_plugin("Plugin 3""1.0.0""Oinling");
    
server_print("[AMXX] [plugin 3] MaxClients : %d"MaxClients);

print like this:
Code:
[AMXX] [plugin 1] MaxClients : 32
[AMXX] [plugin 2] MaxClients : 32
[AMXX] [plugin 3] MaxClients : 32
nor are they like normal public functions (which can distinguish different plugins)

PHP Code:
#include <amxmodx>
public plugin_init()
{
    new 
pluginId register_plugin("Plugin 1""1.0.0""Oinling");
    
CallPublicFunction(pluginIdfind_plugin_byfile("plugin1.amxx"));
    
CallPublicFunction(pluginIdfind_plugin_byfile("plugin2.amxx"));
    
CallPublicFunction(pluginIdfind_plugin_byfile("plugin3.amxx"));
}
public 
TestFunc(pluginId)
{
    new 
fileName[32]
    
get_plugin(pluginIdfileNamecharsmax(fileName));
    
server_print("[AMXX] [plugin 1] public function called by plugin : ^"%s^""fileName);
}
static 
CallPublicFunction(thisPluginIdtargetPluginId)
{
    
callfunc_begin_i(get_func_id("TestFunc"targetPluginId), targetPluginId);
    
callfunc_push_int(thisPluginId);
    
callfunc_end();
}
//////////////////////////////////////////
#include <amxmodx>
public plugin_init()
{
    new 
pluginId register_plugin("Plugin 2""1.0.0""Oinling");
    
CallPublicFunction(pluginIdfind_plugin_byfile("plugin1.amxx"));
    
CallPublicFunction(pluginIdfind_plugin_byfile("plugin2.amxx"));
    
CallPublicFunction(pluginIdfind_plugin_byfile("plugin3.amxx"));
}
public 
TestFunc(pluginId)
{
    new 
fileName[32]
    
get_plugin(pluginIdfileNamecharsmax(fileName));
    
server_print("[AMXX] [plugin 2] public function called by plugin : ^"%s^""fileName);
}
static 
CallPublicFunction(thisPluginIdtargetPluginId)
{
    
callfunc_begin_i(get_func_id("TestFunc"targetPluginId), targetPluginId);
    
callfunc_push_int(thisPluginId);
    
callfunc_end();
}
//////////////////////////////////////////
#include <amxmodx>
public plugin_init()
{
    new 
pluginId register_plugin("Plugin 3""1.0.0""Oinling");
    
CallPublicFunction(pluginIdfind_plugin_byfile("plugin1.amxx"));
    
CallPublicFunction(pluginIdfind_plugin_byfile("plugin2.amxx"));
    
CallPublicFunction(pluginIdfind_plugin_byfile("plugin3.amxx"));
}
public 
TestFunc(pluginId)
{
    new 
fileName[32]
    
get_plugin(pluginIdfileNamecharsmax(fileName));
    
server_print("[AMXX] [plugin 3] public function called by plugin : ^"%s^""fileName);
}
static 
CallPublicFunction(thisPluginIdtargetPluginId)
{
    
callfunc_begin_i(get_func_id("TestFunc"targetPluginId), targetPluginId);
    
callfunc_push_int(thisPluginId);
    
callfunc_end();

print like this:
Code:
[AMXX] [plugin 1] public function called by plugin : "plugin1.amxx"
[AMXX] [plugin 2] public function called by plugin : "plugin1.amxx"
[AMXX] [plugin 3] public function called by plugin : "plugin1.amxx"
[AMXX] [plugin 1] public function called by plugin : "plugin2.amxx"
[AMXX] [plugin 2] public function called by plugin : "plugin2.amxx"
[AMXX] [plugin 3] public function called by plugin : "plugin2.amxx"
[AMXX] [plugin 1] public function called by plugin : "plugin3.amxx"
[AMXX] [plugin 2] public function called by plugin : "plugin3.amxx"
[AMXX] [plugin 3] public function called by plugin : "plugin3.amxx"

In my opinion, this is completely a design error. From MaxClients' performance, Amxmodx has the ability to access and assign public variables in all plugins.

This is very counterintuitive....
PHP Code:
#include <amxmodx>
public gVar;
public 
plugin_init()
{
    
register_plugin("Plugin 1""1.0.0""Oinling");
    
gVar 30;
    
server_print("[Plugin 1] xvar(gVar):%d gVar:%d"get_xvar_num(get_xvar_id("gVar")), gVar);
}
////////////////////////////////////////////////////////////////////////
#include <amxmodx>
public gVar;
public 
plugin_init()
{
    
register_plugin("Plugin 2""1.0.0""Oinling");
    
gVar 31;
    
server_print("[Plugin 2] xvar(gVar):%d gVar:%d"get_xvar_num(get_xvar_id("gVar")), gVar);
}
////////////////////////////////////////////////////////////////////////
#include <amxmodx>
public gVar;
public 
plugin_init()
{
    
register_plugin("Plugin 3""1.0.0""Oinling");
    
gVar 32;
    
server_print("[Plugin 3] xvar(gVar):%d gVar:%d"get_xvar_num(get_xvar_id("gVar")), gVar);

Code:
[Plugin 1] xvar(gVar):30 gVar:30
[Plugin 2] xvar(gVar):30 gVar:31
[Plugin 3] xvar(gVar):30 gVar:32

Last edited by Leech_v2; 10-08-2023 at 03:54.
Leech_v2 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 10-08-2023 , 13:34   Re: How to distinguish public variables with the same name?
Reply With Quote #9

You do realize that this is built on a game engine that was created over 20 years ago, right? Any significant changes like what you're asking for won't make it into AMX Mod X based on how dead this community actually is unless you make the changes yourself and compile your own version of AMX Mod X.

Using xvars, you can effectively have a variable like MaxClients except that you simply need to always use the xvar natives to set and get the values in every plugin. Also realize that if you try to use this variable as anything other than a constant or only have a single writer, you're going to likely have race conditions.
__________________
fysiks is offline
bigdaddy424
Senior Member
Join Date: Oct 2021
Location: Jupiter
Old 10-08-2023 , 20:25   Re: How to distinguish public variables with the same name?
Reply With Quote #10

dirty trick to avoid get_p/x/cvar num every second
PHP Code:
new var;bind_pcvar_num(create_cvar("private_cvar""0"FCVAR_SPONLY), var) 
__________________
bigdaddy424 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 16:27.


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