This include provides the ability to store arrays in nVault. Out of the box, nVault allows you to only save data as a single string. This is cumbersome when you need to store multiple pieces of data because you first need to convert it to a single sting before saving. On top of that, when retrieving the data you then need to parse each value out of the string and then convert it back to an integer and/or copy it back to a string/float variable. This include file solves this problem by allowing you to save and retrieve data directly to and from an array. Data of various types (int, bool, string, float) can all be stored in a single array by using an enum sized array (see example plugin).
Note: This include file requires that you use a fixed version of the nVault module. During the development of these new functions, a bug was discovered in the nVault module that will result in invalid values and possibly errors. This fixed module is attached below and is also available in AMX-X dev builds >= 4589. Thanks to Arkshine for fixing this so quickly for me.
Functions
nvault_set_array() - Save an array to vault.
nvault_get_array() - Read an array from vault.
nvault_arraysize() - Returns size of array stored.
nvault_isarray() - Returns true/false for if data is an array.
Note: This include file uses the nVault handle returned from nvault_open(). It does NOT use the nvault_util_open() file handle used by nVault Utility. This include file has no dependency on nVault Utility. nVault Utility does, however, provide the ability to read all array entries. For nvault_util_read_array()/nvault_util_readall_array(), you would use the nvault_util_open() file handle as with all functions in nVault Utility.
Descriptions
nvault_set_array( vault , key , array[] , size )
This function saves an array to vault. The largest array that can be saved is 500 cells; you can increase this by increasing the constant _NVAULT_ARRAY_MAXARRAYSIZE in the include file. Multidimensional arrays are not directly supported, but if you use an enum to size your array, it can be accomplished. Arrays are stored in a binary format with encoding to prevent null bytes from being included in the output string .. which means you cannot edit/view arrays saved in nVault with an nVault viewer application.
Code:
vault - Vault handle.
key - Key for entry.
array[] - Array to store in vault.
size - Size of array.
nvault_get_array( vault , key , array[] , size )
This function reads an array from vault. This will only read arrays that were stored in the vault using nvault_set_array().
Code:
vault - Vault handle.
key - Key for entry.
array[] - Array to read data in to.
size - Size of array.
nvault_arraysize( vault , key )
This function returns the size of the array data stored in nvault. It will return 0 if the record does not exist or if the data in the nVault record is not an array. It will otherwise return the size of the array in cells.
Code:
vault - Vault handle.
key - Key for entry.
nvault_isarray( vault , key )
This function determines whether or not the data is an array.
//Fill array index with dummy player data copy( pdData[ id ][ pdName ] , charsmax( pdData[][ pdName ] ) , "bugsy" ); copy( pdData[ id ][ pdSteamID ] , charsmax( pdData[][ pdSteamID ] ) , "STEAM_0:1:12345" ); pdData[ id ][ pdKills ] = 524; pdData[ id ][ pdDeaths ] = 13; pdData[ id ][ pdHeadShots ] = 0; pdData[ id ][ pdMoney ] = 16000; pdData[ id ][ pdLastOrigin ][ 0 ] = _:12.34; pdData[ id ][ pdLastOrigin ][ 1 ] = _:56.78; pdData[ id ][ pdLastOrigin ][ 2 ] = _:90.12;
//Display what was stored server_print( "SET %s" , pdData[ id ][ pdName ] ); server_print( "SET %s" , pdData[ id ][ pdSteamID ] ); server_print( "SET %d" , pdData[ id ][ pdKills ] ); server_print( "SET %d" , pdData[ id ][ pdDeaths ] ); server_print( "SET %d" , pdData[ id ][ pdHeadShots ] ); server_print( "SET %d" , pdData[ id ][ pdMoney ] ); server_print( "SET %f %f %f" , pdData[ id ][ pdLastOrigin ][ 0 ] , pdData[ id ][ pdLastOrigin ][ 1 ] , pdData[ id ][ pdLastOrigin ][ 2 ] );
//Save array to nvault nvault_set_array( g_Vault , "TestSet" , pdData[ id ][ PlayerData:0 ] , sizeof( pdData[] ) );
//Fill number arrays with dummy data and display for ( new i = 0 ; i < sizeof( iRegularArray ) ; i++ ) { iRegularArray[ i ] = random( 545545 ); fFloatArray[ i ] = random_float( -5000.0 , 20000.0 );
server_print( "%d - SET Regular=%d Float=%f" , i , iRegularArray[ i ] , fFloatArray[ i ] ); }
public TestGetFunc() { //id=5 is holding the data that we manually loaded into it and we do not want to that that since it //will not tell us for sure that data successfully read into it from nvault. //id=6 will be used since it is empty. new id = 6;
//Read array into array nvault_get_array( g_Vault , "TestSet" , pdData[ id ][ PlayerData:0 ] , sizeof( pdData[] ) );
//Display data that was read server_print( "GET %s" , pdData[ id ][ pdName ] ); server_print( "GET %s" , pdData[ id ][ pdSteamID ] ); server_print( "GET %d" , pdData[ id ][ pdKills ] ); server_print( "GET %d" , pdData[ id ][ pdDeaths ] ); server_print( "GET %d" , pdData[ id ][ pdHeadShots ] ); server_print( "GET %d" , pdData[ id ][ pdMoney ] ); server_print( "GET %f %f %f" , pdData[ id ][ pdLastOrigin ][ 0 ] , pdData[ id ][ pdLastOrigin ][ 1 ] , pdData[ id ][ pdLastOrigin ][ 2 ] );
//Display number arrays read from vault for ( new i = 0 ; i < sizeof( iRegularArray ) ; i++ ) { server_print( "%d - GET Regular=%d Float=%f" , i , iRegularArray2[ i ] , fFloatArray2[ i ] ); } }
Hello @Bugsy, I am facing a problem regarding Loading and Saving data using nvault_array.
Players data is missing sometimes, not immediately after changelevel... they are able to play all day and tomorrow all of the data, skins, keys, etc are not in their inventory. How is that possible? Give me a hint, I am not so friendly with SQL saving and loading data....
I didn't look closely at your code but you are using many nvault array records, ideally you should have all data in the same one. Are you changing the save method of an existing plugin to use nVault array?
I think it's same think, it depends how you declare variables (eg: new stattrak[MAX+1][33] where max could be more than 100)...
----
Quote:
I didn't look closely at your code but you are using many nvault array records, ideally you should have all data in the same one. Are you changing the save method of an existing plugin to use nVault array?
I choosed the option to separate data because I like the code to be clean and code readable and friendly to read.
First I have used nvault, but because there is so much data to store I faced some issues that data strings can not be so longer and had to split in more strings and using strok method, etc, headacke, with nvault_array is much easier. but now I am facing this...
I think it's same think, it depends how you declare variables (eg: new stattrak[MAX+1][33] where max could be more than 100)...
I guess, but it looks sloppy being inconsistent.
Quote:
Originally Posted by aEEk
I choosed the option to separate data because I like the code to be clean and code readable and friendly to read.
First I have used nvault, but because there is so much data to store I faced some issues that data strings can not be so longer and had to split in more strings and using strok method, etc, headacke, with nvault_array is much easier. but now I am facing this...
I think you did the complete opposite. It's much more uniform to have an enumerator define all of your constants instead of having multiple arrays floating around in your code, and indexed inconsistently var[id][i] and var[i][id]. It's easy to mess something up with code like this since you may forget how you are indexing an array and may assign a value with var[id][i] when you declared the array for var[i][id] use.
It looks like you are saving a ton of data for each player and this can likely be reduced. Can you explain what this is for?
PHP Code:
// LOAD STATTRAK SKINS to USER ID formatex(szKey, charsmax(szKey), "%s-STTK", auth);