Member
|
10-25-2022
, 17:31
Re: best way to share an int array among plugins?
|
#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_id, num_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_id, num_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(id, amount) 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(id, level, cid) { if (!cmd_access(id, level, cid, 2)) return PLUGIN_HANDLED
new target[32]
read_argv(1, target, 31)
new player = cmd_target(id, target, 0)
if (!player) return PLUGIN_HANDLED
new name[32] get_user_name(player, name, charsmax(name))
console_print(id, "Array for %s = %d", name, global_array_get(player)) //g_someGlobalArray[id]
return PLUGIN_HANDLED }
public cmdSetArray(id, level, cid) { if (!cmd_access(id, level, cid, 3)) return PLUGIN_HANDLED
new target[32], amount[8]
read_argv(1, target, 31) read_argv(2, amount, 7)
amount[0] = str_to_num(amount);
new player = cmd_target(id, target, 0)
if (!player) return PLUGIN_HANDLED
new name[32] get_user_name(player, name, charsmax(name))
console_print(id, "Old array for %s = %d", name, global_array_get(player))
global_array_set(id, amount[0]); //g_someGlobalArray[id] = amount[0]
console_print(id, "New array for %s = %d", name, global_array_get(player)) return PLUGIN_HANDLED }
public cmdAddArray(id, level, cid) { if (!cmd_access(id, level, cid, 3)) return PLUGIN_HANDLED
new target[32], amount[8]
read_argv(1, target, 31) read_argv(2, amount, 7)
amount[0] = str_to_num(amount);
new player = cmd_target(id, target, 0)
if (!player) return PLUGIN_HANDLED
new name[32] get_user_name(player, name, charsmax(name))
console_print(id, "Old array for %s = %d", name, global_array_get(player))
global_array_set(id, global_array_get(id) + amount[0]); //g_someGlobalArray[id] = g_someGlobalArray[id] + amount[0]
console_print(id, "New array for %s = %d", name, global_array_get(player)) return PLUGIN_HANDLED }
Last edited by zXCaptainXz; 10-25-2022 at 17:37.
Reason: Adding details
|
|