Raised This Month: $ Target: $400
 0% 

best way to share an int array among plugins?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
loki_himself
Member
Join Date: Nov 2021
Old 10-24-2022 , 18:33   best way to share an int array among plugins?
Reply With Quote #1

...

Last edited by loki_himself; 11-23-2022 at 12:12.
loki_himself is offline
EFFx
Veteran Member
Join Date: Feb 2016
Location: São Paulo, Brasil
Old 10-24-2022 , 21:53   Re: best way to share an int array among plugins?
Reply With Quote #2

Natives is your option.
__________________
• Ranking System • AutoMix 5vs5 System
• Web Ban System • Plugins for free

____________________________________________
For private works:
• Discord: EFFEXo#8850 • Steam: EFFEXo
EFFx is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 10-24-2022 , 22:51   Re: best way to share an int array among plugins?
Reply With Quote #3

Natives tutorial: https://forums.alliedmods.net/showthread.php?t=41251
__________________

Last edited by fysiks; 10-24-2022 at 22:51.
fysiks is offline
loki_himself
Member
Join Date: Nov 2021
Old 10-25-2022 , 10:52   Re: best way to share an int array among plugins?
Reply With Quote #4

...

Last edited by loki_himself; 11-23-2022 at 13:28.
loki_himself is offline
zXCaptainXz
Member
Join Date: May 2017
Old 10-25-2022 , 17:31   Re: best way to share an int array among plugins?
Reply With Quote #5

This is your main plugin where your global array will be stored, you have a register 2 native functions, one to get the array value for a specific index and another to set the array value for a specific index
PHP Code:
#include <amxmodx>

new g_someGlobalArray[33]

public 
plugin_natives()
{
    
register_native("global_array_set""native_global_array_set"//First param is the name of the function that you will call from other plugins, second parameter is the name of the handler in this plugin
    
register_native("global_array_get""native_global_array_get")
}

public 
native_global_array_get(plugin_idnum_params)
{
    new 
id get_param(1//global_array_get(-->id<--), first parameter so get_param(1)
    
    
if (0>id<33)
    {
        
log_error(AMX_ERR_NATIVE"Invalid ID (%d)"id)
        return -
1;
    }
    
    return 
g_someGlobalArray[id]; //Return the value of the array to whichever plugin calling this function
}

public 
native_global_array_set(plugin_idnum_params)
{
    new 
id get_param(1//global_array_set(>--id<--, amount), first parameter so get_param(1)
    
    
if (0>id<33)
    {
        
log_error(AMX_ERR_NATIVE"Invalid ID (%d)"id)
        return 
false;//Returning false in case you want to check if the operation did not happen successfully
    
}
    
    new 
amount get_param(2//global_array_set(id, >--amount<--), second parameter so get_param(2)
    
    
g_someGlobalArray[id] = amount //Modify the global array value to the value specified by any plugin calling this function
    
return true;//Just returning true in case you want to check if the operation happened successfully



Below is an example plugin which can access the array of the main plugin and modify it:
PHP Code:
#include <amxmodx>
#include <amxmisc>

//First we have to tell this plugin about the natives we created previously and the parameters they take (you can put them in an include file with descriptions but this is enough)

native global_array_set(idamount
native global_array_get(id)

public 
plugin_init()
{
    
register_concmd("amx_getarray""cmdGetArray"ADMIN_KICK"<name or #userid> - Returns array value for player")
    
register_concmd("amx_setarray""cmdSetArray"ADMIN_KICK"<name or #userid> <amount> - Sets array value for player to amount" )
    
register_concmd("amx_addarray""cmdAddArray"ADMIN_KICK"<name or #userid> <amount> - Adds array value for player by amount")
}

public 
cmdGetArray(idlevelcid)
{
    if (!
cmd_access(idlevelcid2))
        return 
PLUGIN_HANDLED

    
new target[32]

    
read_argv(1target31)

    new 
player cmd_target(idtarget0)

    if (!
player)
        return 
PLUGIN_HANDLED

    
new name[32]
    
get_user_name(playernamecharsmax(name))

    
console_print(id"Array for %s = %d"nameglobal_array_get(player)) //g_someGlobalArray[id]

    
return PLUGIN_HANDLED
}


public 
cmdSetArray(idlevelcid)
{
    if (!
cmd_access(idlevelcid3))
        return 
PLUGIN_HANDLED

    
new target[32], amount[8]

    
read_argv(1target31)
    
read_argv(2amount7)

    
amount[0] = str_to_num(amount);

    new 
player cmd_target(idtarget0)

    if (!
player)
        return 
PLUGIN_HANDLED

    
new name[32]
    
get_user_name(playernamecharsmax(name))

    
console_print(id"Old array for %s = %d"nameglobal_array_get(player))

    
global_array_set(idamount[0]); //g_someGlobalArray[id] = amount[0]

    
console_print(id"New array for %s = %d"nameglobal_array_get(player))
    return 
PLUGIN_HANDLED
}

public 
cmdAddArray(idlevelcid)
{
    if (!
cmd_access(idlevelcid3))
        return 
PLUGIN_HANDLED

    
new target[32], amount[8]

    
read_argv(1target31)
    
read_argv(2amount7)

    
amount[0] = str_to_num(amount);

    new 
player cmd_target(idtarget0)

    if (!
player)
        return 
PLUGIN_HANDLED

    
new name[32]
    
get_user_name(playernamecharsmax(name))

    
console_print(id"Old array for %s = %d"nameglobal_array_get(player))

    
global_array_set(idglobal_array_get(id) + amount[0]); //g_someGlobalArray[id] = g_someGlobalArray[id] + amount[0]

    
console_print(id"New array for %s = %d"nameglobal_array_get(player))
    return 
PLUGIN_HANDLED


Last edited by zXCaptainXz; 10-25-2022 at 17:37. Reason: Adding details
zXCaptainXz is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 10-25-2022 , 21:41   Re: best way to share an int array among plugins?
Reply With Quote #6

Quote:
Originally Posted by loki_himself View Post
thanks. that tut does not have much substance.
It gets people started. You can search for the various functions that are used in custom natives to see how they get used. It is what it is, as they say.

Quote:
Originally Posted by loki_himself View Post
is there any plugin using natives like a generic data service? basically a plugin that holds global data, you can just include it in any plugin and save and share data, maybe by string key. [...] do you know any big plugins that use natives in a best practice way?
Quote:
Originally Posted by loki_himself View Post
i assume that is faster than vault?
If a vault allows simultaneous access to it among multiple plugins I'd say that's probably a really good way to start. I'd bet it's likely faster than any quick plugin you'd write with your own custom natives. You could even write your plugin in a way that would make it easy to change at a later time (i.e. a custom include that would mask vault calls that you would later swap out for something else) so that you can get on with your primary goal.

Quote:
Originally Posted by loki_himself View Post
or is there any json data storage using natives?
JSON isn't really a way for storing data in memory unless you want to be extremely inefficient. Granted, I'm not a Javascript expert.

Quote:
Originally Posted by loki_himself View Post
basically i want to hold data globally in memory. i do not need to make it persistent.
Using a vault, you could simply empty it when the map starts and then you'd effectively have non-persistent storage.
__________________

Last edited by fysiks; 10-25-2022 at 21:42.
fysiks is offline
loki_himself
Member
Join Date: Nov 2021
Old 10-26-2022 , 07:15   Re: best way to share an int array among plugins?
Reply With Quote #7

...

Last edited by loki_himself; 11-23-2022 at 13:28.
loki_himself is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 10-26-2022 , 22:23   Re: best way to share an int array among plugins?
Reply With Quote #8

I don't use vaults so I'm not sure if simultaneous access is supported. If it's not then it won't work for what you want.

Personally, I would expect to open the vault once when the plugin starts and then close it at the end especially if you're using it more often than a couple times during the map.

It looks like zXCaptainXz's plugin example is a good start. If you're looking for a key-value pair type setup you could instead implement the data storage using a Trie which would allow you to use a string instead of the the `id`.

Quote:
Originally Posted by loki_himself View Post
i wish they had given a better documentation on that. i know im over 10 years late but there is hardly any best practices collected in 1 place
That is in the nature of open source software IMO (generally, but not always).
__________________
fysiks 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 15:31.


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