AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   nvault. (https://forums.alliedmods.net/showthread.php?t=321001)

Napoleon_be 01-19-2020 12:48

nvault.
 
So, i've been reading bugsy's tutorial on how to save and load data using nVault. I understand how to save and load a single array, but i have more than 1 array to save & load. The thing is i want to use formatex and use nvault_set just once to save the data. I'm not even sure if i can use nvault_set multiple times to save multiple arrays.

my current code (Code which doesn't work when i change map)
PHP Code:

public client_disconnect(id)
{
    new 
szAuth[35], szTemp[60]; 
    
get_user_authid(idszAuthcharsmax(szAuth));
    
    
formatex(szTempcharsmax(szTemp), "%i#%i#%i"iPoints[id], iTsKilled[id], iCtKilled[id]);
    
    
nvault_set(szVaultNameszAuthszTemp);
}

public 
client_connect(id)
{
    new 
szAuth[35], szTemp[60], szTsKills[33], szCtKills[33], szPoints[33];
    
get_user_authid(idszAuthcharsmax(szAuth));
    
    
formatex(szTempcharsmax(szTemp), "%i#%i#%i"iPoints[id], iTsKilled[id], iCtKilled[id]);
    
nvault_get(szVaultNameszAuthszTemp);
    
    
replace_all(szTempcharsmax(szTemp), "#"" ");
    
    
parse(szTempszTsKillscharsmax(szTsKills), charsmax(szCtKills), szPointscharsmax(szPoints));
    
    
iTsKilled[id] = str_to_num(szTsKills);
    
iCtKilled[id] = str_to_num(szCtKills);
    
iPoints[id] = str_to_num(szPoints);



Bugsy 01-19-2020 13:19

Re: nvault.
 
A key is unique in a vault, so you cannot save multiple items under the same key..the last nvault_set() will overwrite all the previous ones. This is the whole idea behind nvault array, allowing you to save multiple pieces of data under a single key.

Looking at your code it seems as if your mindset is that the variable name has a bearing on the type of data you are saving and retrieving, and this is not the case.

The only thing the nvault module sees is:

nvault_get( vault id , string variable , string variable , length )
nvault_set( vault id , string variable , string variable )

Napoleon_be 01-19-2020 13:25

Re: nvault.
 
Quote:

Originally Posted by Bugsy (Post 2680909)
A key is unique in a vault, so you cannot save multiple items under the same key..the last nvault_set() will overwrite all the previous ones. This is the whole idea behind nvault array, allowing you to save multiple pieces of data under a single key.

Looking at your code it seems as if your mindset is that the variable name has a bearing on the type of data you are saving and retrieving, and this is not the case.

The only thing the nvault module sees is:

nvault_get( vault id , string variable , string variable , length )
nvault_set( vault id , string variable , string variable )

Okay so my second thought wasn't good either, thanks for explaining. Could u tell me what's wrong in my first code which doesn't save or load my values properly?

Bugsy 01-19-2020 13:47

Re: nvault.
 
Quote:

Originally Posted by Napoleon_be (Post 2680911)
Okay so my second thought wasn't good either, thanks for explaining. Could u tell me what's wrong in my first code which doesn't save or load my values properly?

Code:
public client_disconnect(id) {     new szAuth[35], szTemp[60];     get_user_authid(id, szAuth, charsmax(szAuth));         formatex(szTemp, charsmax(szTemp), "%i#%i#%i", iPoints[id], iTsKilled[id], iCtKilled[id]);         nvault_set(szVaultName, szAuth, szTemp); } get_user_authid() should not be called until client_authorized() public client_connect(id) {     new szAuth[35], szTemp[60], szTsKills[33], szCtKills[33], szPoints[33];     get_user_authid(id, szAuth, charsmax(szAuth));         It makes no sense to format this string since it gets immediately overwritten with what nvault_get() retrieves from the vault     formatex(szTemp, charsmax(szTemp), "%i#%i#%i", iPoints[id], iTsKilled[id], iCtKilled[id]);     nvault_get(szVaultName, szAuth, szTemp);         replace_all(szTemp, charsmax(szTemp), "#", " ");     You are missing the szCtKills variable before charsmax(szCtKills)     parse(szTemp, szTsKills, charsmax(szTsKills), charsmax(szCtKills), szPoints, charsmax(szPoints));         iTsKilled[id] = str_to_num(szTsKills);     iCtKilled[id] = str_to_num(szCtKills);     iPoints[id] = str_to_num(szPoints); }

OciXCrom 01-19-2020 14:03

Re: nvault.
 
Also, I don't think there's a need to use "#" as a replacement for a whitespace. Just do "%i %i %i" directly. They're all integers, so there shouldn't be any problems.

PS: the max AuthId length is 64, not 35. You probably don't need 33 for a buffer that holds a single integer, the integer itself can't even be that big.

Napoleon_be 01-20-2020 06:53

Re: nvault.
 
This doesn't work either

PHP Code:

public client_disconnect(id)
{
    new 
szAuth[35], szTemp[60]; 
    
get_user_authid(idszAuthcharsmax(szAuth));
    
    
formatex(szTempcharsmax(szTemp), "%i %i %i"iPoints[id], iTsKilled[id], iCtKilled[id]);
    
    
nvault_set(szVaultNameszAuthszTemp);
}

public 
client_authorized(id)
{
    new 
szAuth[64], szTemp[20], szTsKills[5], szCtKills[5], szPoints[10];
    
get_user_authid(idszAuthcharsmax(szAuth));
    
    
nvault_get(szVaultNameszAuthszTsKillsszCtKillsszPoints);
    
    
parse(szTempszPointscharsmax(szPoints), szTsKillscharsmax(szTsKills), szCtKillscharsmax(szCtKills));
    
    
iPoints[id] = str_to_num(szPoints);
    
iTsKilled[id] = str_to_num(szTsKills);
    
iCtKilled[id] = str_to_num(szCtKills);



OciXCrom 01-20-2020 07:11

Re: nvault.
 
Code:
nvault_get(szVaultName, szAuth, szTemp, charsmax(szTemp))

Napoleon_be 01-20-2020 07:14

Re: nvault.
 
Quote:

Originally Posted by OciXCrom (Post 2680981)
Code:
nvault_get(szVaultName, szAuth, szTemp, charsmax(szTemp))

Thanks, it worked. Also, i see someone else has done the job i was working on before me and used another method, which one is better and more efficient to use?

https://forums.alliedmods.net/showpo...78&postcount=4

OciXCrom 01-20-2020 07:17

Re: nvault.
 
There are many things in his code that need fixing/optimizing, starting from the natives that won't even work.

Napoleon_be 01-20-2020 07:19

Re: nvault.
 
Quote:

Originally Posted by OciXCrom (Post 2680983)
There are many things in his code that need fixing/optimizing, starting from the natives that won't even work.

Yea, i figured those natives won't work. I've posted my code there too, just without natives though.


All times are GMT -4. The time now is 02:40.

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