(vault designed for beginners) nvp.inc

For scripting examples for learning how to use AMXX.
User avatar
Archive-Poster
Posts: 1229
Gender:
United States of America

(vault designed for beginners) nvp.inc

Post by Archive-Poster »

Description This inc file have functions for saving/retrieving designed for beginners, if you know how to use nvault please DO NOT USE THIS INC.

Source

Code: Select all

#if defined _nvp_included
  #endinput
#endif
#define _nvp_included

#include <nvault>

/**
*	Open a vault by its name.
*
*	@param 	VaultName	The vault name to open/create if non-existend.
*	@return:		Vault Id
*
*/
stock nvp_open( VaultName[] )
{
	new iOpened = nvault_open( VaultName );
	new iClosed;

	if( iOpened == INVALID_HANDLE )
	{
		set_fail_state("Problems while oppenning %s.vault",VaultName);
		iClosed = -1;
	}

	else
	{
		iClosed = iOpened;	
	}

	return iClosed;
}

/**
 * Save an integer to an index.
 *
 * @param	Vaultid			The id of the vault.(The handler)
 * @param	id			The index of the player.
 * @pram	Symbol[]		The Symbol to make unique the save for your needs/plugin.
 * @param	Integer			The Integer to save.
 * @return	true/false		If we found the 'id' saved.			
 */
stock nvp_set_integer( VaultId, id, const Symbol[], int)
{
	new szAuthId[34]; get_user_authid( id, szAuthId, charsmax(szAuthId) );
	new buffer[64]; formatex( buffer, charsmax(buffer), "%s%s", szAuthId, Symbol );

	new szData[11]; num_to_str( int, szData, charsmax( szData ) );

	nvault_set( VaultId, buffer, szData );
}


/**
 * Retrieve an integer from an index.
 *
 * @param	Vaultid			The id of the vault.(The handler)
 * @param	id			The index of the player.
 * @param	Symbol[]		The symbol to perform the search.
 * @param	Integer			The Integer to save.
 * @return	The Integer result.
 */
stock nvp_get_integer( VaultId, id, const Symbol[] )
{
	new szAuthId[34]; get_user_authid( id, szAuthId, charsmax(szAuthId) );
	new buffer[64]; formatex( buffer, charsmax(buffer), "%s%s", szAuthId, Symbol );

	new szData[11], iTs;

	return nvault_lookup( VaultId, buffer, szData, charsmax(szData), iTs ) ? str_to_num(szData) : 0;
}

/**
 * Save an string to an index.
 *
 * @param	Vaultid			The id of the vault.(The handler)
 * @param	id			The index of the player.
 * @pram	Symbol[]		The Symbol to make unique the save for your needs/plugin.
 * @param	String[]		The string to save.
 * @noreturn
 */
stock nvp_set_string( VaultId, id, const Symbol[], const String[])
{
	new szAuthId[34]; get_user_authid( id, szAuthId, charsmax(szAuthId) );
	new buffer[64]; formatex( buffer, charsmax(buffer), "%s%s", szAuthId, Symbol );

	nvault_set( VaultId, buffer, String );
}

/**
 * Retrieve an string from an index.
 *
 * @param	Vaultid			The id of the vault.(The handler)
 * @param	id			The index of the player.
 * @param	Symbol[]		The symbol to perform the search.
 * @param	String[]		The string to copy data from the savement.
 * @param	maxchars		The lengh of the String[] ( use charsmax(String[]) ).
 * @noreturn
 */
stock nvp_get_string( VaultId, id, const Symbol[], const String[], maxchars=0 )
{
	new szAuthId[34]; get_user_authid( id, szAuthId, charsmax(szAuthId) );
	new buffer[64]; formatex( buffer, charsmax(buffer), "%s%s", szAuthId, Symbol );

	new iTs;
	return nvault_lookup( VaultId, buffer, String, maxchars, iTs );
}

/**
 * 	Reset the vault, so it deletes all the entries 
 * 	@noreturn
 */
stock nvp_reset( VaultId )
{
	nvault_prune( VaultId, 0, get_systime( ) );
}
Testing code

Code: Select all

#include <amxmodx>
#include <nvp>

new VaultId;
#define SYMBOL "HealthTest"

public plugin_init()
{
    VaultId = nvp_open("MyVaultTest");

    register_clcmd( "test", "ptest" );
    register_clcmd( "show", "pshow" );
}

public plugin_end()
{
    nvault_close(VaultId);
}

public ptest(id)
{
    new iCurrentHealth = get_user_health(id);

    nvp_set_integer( VaultId, id, SYMBOL, iCurrentHealth);
}


public pshow(id)
{
    client_print(id, print_chat, "Your current health was: %i", nvp_get_integer( VaultId, id, SYMBOL ) );
} 
Output:

Code: Select all

STEAM_0:1:180498108HealthTest           100

Who is online

Users browsing this forum: No registered users and 0 guests