AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Remember more values with nVault? (https://forums.alliedmods.net/showthread.php?t=325323)

supertrio17 06-16-2020 19:25

Remember more values with nVault?
 
Hey, so I want to have nVault remember 2 values. Now it just remembers 1 of them:
PHP Code:

    new szClass[20];
    new 
szKey[40];

    
formatex(szKeycharsmaxszKey ), "%sCLASS"g_szAuthID[id]);
    
formatex(szClasscharsmaxszClass ), "%d"g_Class[id]); // Wizard

    
nvault_set(g_VaultszKeyszClass); 

And this is how I retrieve it:
PHP Code:

        new szKey[40];
        
formatex(szKeycharsmaxszKey ), "%sCLASS"g_szAuthID[id3]);
        new 
iClass nvault_get(g_VaultszKey ); 

But how do I remember 2 values, for example his class (like shown) and his level?
I was thinking something like this
PHP Code:

    new szClass[20];
    new 
szLevel[20];
    new 
szKey[40];

    
formatex(szKeycharsmaxszKey ), "%sCLASS"g_szAuthID[id]);
    
formatex(szClasscharsmaxszClass ), "%d"g_Class[id]); // Wizard
    
formatex(szLevelcharsmaxszLevel ), "%d"g_Level[id]);

    
nvault_set(g_VaultszKeyszClassszLevel); 

If that is possible, but then how do I retrieve it?

Bugsy 06-16-2020 19:32

Re: Remember more values with nVault?
 
A few options:

4-byte integers you would need to store side by side in a string "434 34343 3141414" and then parse when retrieved
2-byte integers - You can fit 2 into a 4-byte integer and save as a single integer value
1-byte integers - You can fit 4 into a 4-byte integer and save as a single integer value

String or float values
Pack side by side, similar to the 4-byte integer way above "string1" "string2" "string3"

OR use nVault Array and storing anything is simple

supertrio17 06-16-2020 19:40

Re: Remember more values with nVault?
 
Hey, so I didn't understand you quite well, I need something like this. First value: 1/2/3/4/5/6. Second value 1/2.
Is it problem for you to give me a simple example?

Bugsy 06-16-2020 19:57

Re: Remember more values with nVault?
 
All numbers in AMX-X are stored in a 4-byte cell. Each byte can hold a value between 0 and 255. So you can pack 4 0 to 255 values within 1 cell. If the values are between 1 and 6 only, you can pack more than just 4 into a cell.

For your example, you need 6 so this idea wouldn't work.

I would use nVault Array, it's the simplest if you are not experienced.

CrazY. 06-16-2020 20:00

Re: Remember more values with nVault?
 
Code:
#include <amxmodx> #include <nvault> new g_vault public plugin_init() {     register_plugin("NVault Example", "Version", "Author")     g_vault = nvault_open("ExampleVault") } public client_disconnected(id) {     if (!is_user_bot(id) && !is_user_hltv(id))         save_player_data(id) } public client_authorized(id, const authid[]) {     if (!is_user_bot(id) && !is_user_hltv(id))         load_player_data(id, authid) } public save_player_data(id) {     new authid[MAX_AUTHID_LEN]     get_user_authid(id, authid, charsmax(authid))     new data[256]     new value1 = 100     new value2 = 200     new value3 = 300     formatex(data, charsmax(data), "%d %d %d", value1, value2, value3)     nvault_set(g_vault, authid, data) } public load_player_data(id, authid[] = "") {     if (!authid[0])         get_user_authid(id, authid, charsmax(authid))     new data[256]     nvault_get(g_vault, authid, data, charsmax(data))     new values[3][85]     parse(data, values[0], charsmax(values[]), values[1], charsmax(values[]), values[2], charsmax(values[]))     new value1 = str_to_num(values[0])     new value2 = str_to_num(values[1])     new value3 = str_to_num(values[2]) }

If you need to store multiple strings consider using nVault Array instead. It's just an include with utility functions, easy, really.

supertrio17 06-16-2020 20:01

Re: Remember more values with nVault?
 
Quote:

Originally Posted by Bugsy (Post 2706098)
All numbers in AMX-X are stored in a 4-byte cell. Each byte can hold a value between 0 and 255. So you can pack 4 0 to 255 values within 1 cell. If the values are between 1 and 6 only, you can pack more than just 4 into a cell.

For your example, you need 6 so this idea wouldn't work.

I would use nVault Array, it's the simplest if you are not experienced.

I don't really feel like using it, simply because I don't want to get includes for everyone who want's this plugin, so I ended up using something like this:
PHP Code:

    new szClass[20];
    new 
szKey[40];

    
formatex(szKeycharsmaxszKey ), "%sCLASS"g_szAuthID[id]);
    
formatex(szClasscharsmaxszClass ), "%d"g_Class[id]); // Soul Master

    
nvault_set(g_VaultszKeyszClass);

    new 
szCLevel[20]

    
formatex(szKeycharsmaxszKey ), "%sCLEVEL"g_szAuthID[id]);
    
formatex(szCLevelcharsmaxszCLevel ), "%d"g_ClassLevel[id]);

    
nvault_set(g_VaultszKeyszCLevel); 

And loading it like this:
PHP Code:

        new cLevel crxranks_get_user_level(id3);
        new 
szKey[40];
        
formatex(szKeycharsmaxszKey ), "%sCLASS"g_szAuthID[id3]);
        new 
iClass nvault_get(g_VaultszKey );
        
formatex(szKeycharsmaxszKey ), "%sCLEVEL"g_szAuthID[id3]);
        new 
iCLevel nvault_get(g_VaultszKey ); 


supertrio17 06-16-2020 20:03

Re: Remember more values with nVault?
 
Quote:

Originally Posted by CrazY. (Post 2706099)
Code:
#include <amxmodx> #include <nvault> new g_vault public plugin_init() {     register_plugin("NVault Example", "Version", "Author")     g_vault = nvault_open("ExampleVault") } public client_disconnected(id) {     if (!is_user_bot(id) && !is_user_hltv(id))         save_player_data(id) } public client_authorized(id, const authid[]) {     if (!is_user_bot(id) && !is_user_hltv(id))         load_player_data(id, authid) } public save_player_data(id) {     new authid[MAX_AUTHID_LEN]     get_user_authid(id, authid, charsmax(authid))     new data[256]     new value1 = 100     new value2 = 200     new value3 = 300     formatex(data, charsmax(data), "%d %d %d", value1, value2, value3)     nvault_set(g_vault, authid, data) } public load_player_data(id, authid[] = "") {     if (!authid[0])         get_user_authid(id, authid, charsmax(authid))     new data[256]     nvault_get(g_vault, authid, data, charsmax(data))     new values[3][85]     parse(data, values[0], charsmax(values[]), values[1], charsmax(values[]), values[2], charsmax(values[]))     new value1 = str_to_num(values[0])     new value2 = str_to_num(values[1])     new value3 = str_to_num(values[2]) }

I'll try this and see if it works, thanks for replying :)

Bugsy 06-16-2020 20:13

Re: Remember more values with nVault?
 
Storing things side-by-side in a string like that was the only way prior to the creation of nVault Array. The nVault Array include just allows you to do it more elegantly in code, even though it's not the most efficient thing in the world.

It's not unheard of/unusual for plugins to require 3rd-party includes, so I wouldn't always rule them out. You can make your code much cleaner using nVault Array, just throwing it out there.


All times are GMT -4. The time now is 16:58.

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