Quote:
Originally Posted by Natsheh
So what nvautl get returns when i saved in a key value 5 as integer
Dose it returns 5 or 0?
|
You cannot save anything as an integer. nVault allows only saving as strings.
So you can save "5" as a string and then use nvault_get() to return it as an integer. nvault_get() can be used in 3 ways.
PHP Code:
nvault_get( vault, key[] ) // function returns 5 (integer)
nvault_get( vault, key[] , fVal ) // function returns 1. fVal set to 5.0 (float)
nvault_get( vault , key[] val[] , maxchars ) // function returns 1 (num chars). val[] equals "5" (string)
The point I was making is it cannot safely be used to determine if an integer or float data value exists because if the data being stored is 0, the return value is 0 which is the same as if no value was found at all.
As an example, assume the below data exists in the vault [key:data]
[STEAM_1234 : "0" ]
The below would return 0, as would calling nvault_get() on a key that does not exist.
nvault_get( vault , "STEAM_1234" )
I think we're getting off topic here. I was just trying to give you a pointer about safely using nvault_get() to avoid issues in your code. If you want to make your code respond a particular way if the key exists in the vault, nvault_lookup() is the way to go. The downside is you will always need to convert the returned string value to an integer or float, if needed.
As you can see, there is no concept of 'return X only if the key was found.'
PHP Code:
static cell nvault_get(AMX *amx, cell *params)
{
unsigned int id = params[1];
if (id >= g_Vaults.length() || !g_Vaults.at(id))
{
MF_LogError(amx, AMX_ERR_NATIVE, "Invalid vault id: %d\n", id);
return 0;
}
NVault *pVault = g_Vaults.at(id);
unsigned int numParams = (*params)/sizeof(cell);
int len;
char *key = MF_GetAmxString(amx, params[2], 0, &len);
const char *val = pVault->GetValue(key);
switch (numParams)
{
case 2:
{
return atoi(val);
break;
}
case 3:
{
cell *fAddr = MF_GetAmxAddr(amx, params[3]);
*fAddr = amx_ftoc((REAL)atof(val));
return 1;
break;
}
case 4:
{
len = *(MF_GetAmxAddr(amx, params[4]));
return MF_SetAmxString(amx, params[3], val, len);
break;
}
}
return 0;
}
__________________