This will display the differences between loading and parsing data from various vault systems and methods.
Code:
// nVault saved as array (really a string)
// Possible values -128 to 127 except 0.
nvault_set(hVault, "MyKey", {100, 50, 25});
// ---
new data1[4];
nvault_get(hVault, "MyKey", data1, charsmax(data1));
server_print("nVault: %d, %d, %d", data1[0], data1[1], data1[2]);
// fVault saved as string
// Possible values defined by string length
fvault_set_data("TestVault", "MyKey", "100, 50, 25");
// ---
new data2[12];
new subdata1[4], subdata2[4], subdata3[4];
fvault_get_data("TestVault", "MyKey", data2, charsmax(data2));
parse(data2, subdata1, charsmax(subdata1), subdata2, charsmax(subdata2), subdata3, charsmax(subdata3));
server_print("fVault String: %d, %d, %d", str_to_num(subdata1), str_to_num(subdata2), str_to_num(subdata3));
// fVault saved as array (really a string)
// Possible values -128 to 127 except 0.
fvault_set_data("TestVault", "MyKey", {100, 50, 25});
// ---
new data3[4];
fvault_get_data("TestVault", "MyKey", data3, charsmax(data3));
server_print("fVault Array: %d, %d, %d", data3[0], data3[1], data3[2]);
Why is this important? Well maybe it's not. But if you're gonna learn coding good you should know that there are different solutions to one problem depending on other circumstances. In this case, how many weapons are involved.
The first example looks good and is made for storing integers the way they were intended to.
The second example is not only horrible looking but it's wasteful of those precious resources. The more you add to it, the worse it will be.
The third example is basically like the first one.
So which one should we pick? It depends.
We could use example 1 with a bitsum and bitmasking to set 31 variables to true/false. But that would not look very good.
Code:
#define DEAGLE_BITMASK (1<<0)
#define M4A1_BITMASK (1<<1)
new bool:GoldenDeagle[33], bool:CamoM4A1[33];
new bitsum = nvault_get(hVault, "MyKey");
GoldenDeagle[id] = bitsum & DEAGLE_BITMASK ? true : false;
CamoM4A1[id] = bitsum & M4A1_BITMASK ? true : false;
Another option is to save the bitsum directly inside a global array of players and use the bitmask instead of the booleans.
Both bitsum options are generally very bad as permanent storage. It will create a problem because you can not add more than 31 weapons. You also can't remove old ones to make room for new ones because that would corrupt the vault data.
Instead it would be better to use example 1 or 3 with an "array" and possibly an enumeration index for readability.
That way you have no limit on the vault size, minimizing native calls and also the code space, downside is higher memory usage.
Memory is cheap and won't affect the performance of the server as much as CPU hungry solutions.
So here's the save & load I would suggest.
Code:
#include <amxmodx>
#include <fvault>
enum {
GoldenDeagle,
CamoM4A1,
PLACEHOLDER_KEEP_AT_END
}
new gWeapons[33][PLACEHOLDER_KEEP_AT_END + 1];
new const VAULT_NAME[] = "cs_weapon";
public plugin_init()
{
register_plugin("", "", "");
}
public client_authorized(iIndex)
{
if (!is_user_hltv(iIndex) && !is_user_bot(iIndex)) LoadData(iIndex);
}
public client_disconnect(iIndex)
{
SaveData(iIndex);
}
LoadData(iIndex)
{
new szAuthId[35];
get_user_authid(iIndex, szAuthId, sizeof szAuthId);
if ( ! fvault_get_data(VAULT_NAME, szAuthId, gWeapons[iIndex], sizeof gWeapons[] - 1) )
arrayset(gWeapons[iIndex], -1, sizeof gWeapons[] - 1);
}
SaveData(iIndex)
{
new szAuthId[35];
get_user_authid(iIndex, szAuthId, sizeof(szAuthId));
fvault_set_data(VAULT_NAME, szAuthId, gWeapons[iIndex]);
}
This however requires you to remake the weapon variables.
When you want to give someone a weapon you use the array + index.
Code:
gWeapons[id][GoldenDeagle] = 1
And when you want to remove it:
Code:
gWeapons[id][GoldenDeagle] = -1
When checking for a weapon:
Code:
if ( gWeapons[id][GoldenDeagle] == 1 )
There's a reason for using 1 and -1 instead of 1 and 0. Since the data is saved as a string 0 would terminate the string.
Here's an example with the code you supplied:
Code:
case 1:
{
gWeapons[id][GoldenDeagle] = 1
client_cmd(id,"slot3;slot2;drop")
set_task(0.1,"deagle",id)
//play
credits[id] -= 1
save_credits(id)
}
I'm sorry for complicating it further but in the end this would make the whole code easier to work with in general.
You still need to understand how it works so you can make changes yourself, so if you have any questions just ask.
__________________