nVault Tutorial
nVault Tutorial
Intro nVault is an AMX-X tool designed for an easy method of saving and retrieving data from an external file. I have seen many people ask how to save data in a plugin so it can be retrieved after a map change or server restart. nVault will allow you to do this and it is very simple to use. nVault stores data using a key:data system; each chunk of data that you save has a unique key that corresponds to it. Both key and data are always strings when saving but data can be retrieved as integer, float, or string (see nvault_get). Data is saved to a nvault file in binary format, not plain text; this means that you cannot edit the file in [your favorite text editor here] but there is an nVault Editor that can be used. Also note that you cannot iterate through entries in a nvault file with the native nvault functions. You can iterate through entries using the nVault Utility include file. The ability the store arrays in nVault can also be accomplished with the nVault Utility. nVault info from AMX-X documentation: http://www.amxmodx.org/funcwiki.php?go=module&id=20 Code:
nVault is a binary vault implementation. It allows you to open named vaults (which are stored in Code:
nvault_open - Opens a vault by name (such as "myvault"). Before doing any transactions with a vault, it must first be opened. This is done with the nvault_open command; it takes 1 string parameter which is the name of the file that is created on the servers hard-disk ([name].vault). When you call the nvault_open() command, it will return a handle (integer value) that is used by all of the other nvault functions as a reference to that particular vault. If an error occurs when opening the vault, nvault_open() will return -1 (INVALID_HANDLE) so it is a good idea to verify if it opened correctly prior to using any other commands. Code:
nvault_open( const szVaultName[] ) PHP Code:
This function will close the vault. It takes the vault handle to close as a parameter. Once a vault is closed you cannot do any transactions with the vault until it is re-opened. In most circumstances it is best to put nvault_close() in plugin_end(). Failure to close a vault file will cause it to be corrupt and you will lose all data. Code:
nvault_close( iHandle ) PHP Code:
This function will lookup data using a string key and will place the found data into szValue by-reference for a max of iMaxLength characters. If data is found, the function will return 1 (true), if no data found 0 (false). Timestamp will hold the time at which the entry was written/updated or touched last. This function can only retrieve data from the vault and store it into a string variable. See timestamps section below for an explanation of how timestamps work. This function should be used if you would like to see if a particular key exists in the vault. Code:
nvault_lookup( iHandle , const szKey[] , szValue[] , iMaxLength , iTimestamp ) PHP Code:
This function is very similar to nvault_lookup; it looks up data in a vault using the specified key. However, nvault_get is a bit more flexible than nvault_lookup because it allows you to retrieve data directly into a variable other than string. This will negate the need for you to do any data conversion once the data is retrieved as you would need to do if you use nvault_lookup to retrieve a float or int. Note, nvault_get does not allow you to retrieve the timestamp so if you do need the timestamp then you will need to use nvault_lookup. Code:
nvault_get( handle , const szKey[] , ... ) PHP Code:
This will write data to the vault and timestamp the entry. Code:
nvault_set( handle , const key[] , const value[] ) PHP Code:
The same as nvault_set() except this writes an entry without a timestamp so the entry will not be removed by the nvault_prune() function (if called). The timestamp value is 0 when permanent. Code:
Return value: 1 on success, 0 on failure. This removes an entry from the vault that has the specified key value. Code:
nvault_remove( handle , const key[] ) PHP Code:
This function will 'touch' an entry, updating it's timestamp; the entry value will remain unchanged. If the key does not exist that is being touched, an empty entry for that key will be created. See timestamps section below for an explanation of timestamps. Code:
nvault_touch( iHandle , const szKey[] , [ iTimestamp = -1 ] ) PHP Code:
This function is used to remove entries that fall within the specified start and end timestamps. A timestamp value is in seconds so keep this in consideration when pruning entries. See timestamps section below for an explanation of timestamps. Code:
nvault_prune( iHandle , iStart , iEnd ) PHP Code:
Timestamps in nvault are in unix-time format (aka POSIX time) which is equal to the number of seconds that have elapsed since midnight January 1, 1970. To prune or compare a timestamp retrieved in nvault_lookup, you can do simple multiplication to convert seconds to minutes, hours, days, or weeks. Example: Day = 60 * 60 * 24 = 86400. In the above nvault_prune example it shows how to prune entries that are 15 days old or more. If you wanted to prune entries that are 10+ minutes old, you would do nvault_prune( iHandle , 0 , get_systime() - ( 60 * 10 ) ). See this link for info on Unix Time: http://en.wikipedia.org/wiki/Unix_time AMX-X include file for converting Unix Time <-> Normal Time: http://forums.alliedmods.net/showthread.php?t=91915 Convert Unix Time <-> Normal Time on the web: http://www.onlineconversion.com/unix_time.htm Using nVault in your plugin If you will be using nvault functions constantly throughout your plugin, it is best to keep the nvault file open while your plugin is running. To do this, use a global variable to hold the handle and call nvault_open() in plugin_init() or plugin_config() (or anywhere you choose). To close the vault, call nvault_close() in plugin_end(). nVault Limitations The major things (IMO) that are missing in nVault is the ability to determine the number of entries in a vault, the ability to cycle through entries in a vault, and storing arrays. I created an include file, nVault Utility which provides this missing functionality. Just be advised, the nVault handle used with nVault and nVault Utility are *NOT* interchangeable, which means you will have 2 separate handles if you are working with both at the same time (ie. you cannot use the handle returned by nvault_open() with functions in nVault utility and vice-versa. The only exception for this is for the two functions nvault_set_array() and nvault_get_array(), which do use the handle returned by nvault_open(). Example Plugin The below plugin shows how to do save a players money and score to the vault. When a user disconnects, these items are saved and when he re-connects they get restored.
Plugin
|
Re: nVault
Sweet, I was just looking for this tutorial the other day :). I hope it's good :|.
EDIT: Read it. Would there be file access issues if you tried to access a single nVault from two plugins? Just curious really. |
Re: nVault
wooT thx for this tuto :O
+karma edit: seems like i cant give now, will try give later and thx for the tut |
Re: nVault
so many tutorials in such a small periode of time... gj although i never needed to use nvault, sql <3 :D
|
Re: nVault
Well done. Nice to know but I will stay with (My)SQL(ite) :)
|
Re: nVault
The overhead for SQL isn't always necessary for basic data storage. nVault also does not require the knowledge of how to write queries. nVault provides a very basic and easy to understand means of saving\retrieving data to an external file. :grrr:
|
Re: nVault
Quote:
|
Re: nVault
Good job! I always wanted to know how to use the prune function.
PHP Code:
PHP Code:
|
Re: nVault
Quote:
Question: I have a bad opinion about vaults ever since my old WC3 Server suffered from a huge XP-database and crashed on every mapchange. Is this still possible with nVault? |
Re: nVault
Quote:
I've never had any issues with nVault. I have used it for all of my data saving needs. |
Re: nVault
Quote:
|
Re: nVault
Thanks Exolent for catching the typo. +k
|
Re: nVault
Made corrections to example plugin. There were a few minor typo's and added required modules to make it compile via copy and paste.
|
Re: nVault
If you could add a function to return just the timestamp for a vault key that would be great.
Code:
Also, it would be awesome it we could write as to the save to the vault in the same data type that it was intended to be in. |
Re: nVault
Code:
|
Re: nVault
Thanks for the tut!
|
Re: nVault
Thanks for the tutorial, it has been useful for me.
Quote:
And how many entries is to much for nvault? |
Re: nVault
Quote:
Of course there's the problem two or more threads can't write to one file at the same time, but this isn't an issue, since AMXx runs on a single thread. |
Re: nVault
Yeah, I figured that was the case.
|
Re: nVault
Can anyone help me with storing multiple data.
Code:
#include < amxmodx > |
Re: nVault
PHP Code:
PHP Code:
|
Re: nVault
Thank you Bugsy, now it works ;]
|
Re: nVault
XP title e.g 1 kill 10 XP, and 800 XP title serious player
XP PHP Code:
PHP Code:
|
Re: nVault
Quote:
|
Re: nVault
Sockets + php to handle real important database stuff.
nVault to logs less important temporary data. What more?:) gj |
Re: nVault
Quote:
|
Re: nVault
PHP Code:
|
Re: nVault
That will delete entries that are older than 10 days.
nvault_prune( Vault , start time , end time ) All entries that fall within starting and ending time are deleted. 0 = start of time get_systime() = now 86400 * 10 = 10 days |
Re: nVault
Is there a way, that it loads the nvault but doesn't create a file in data/vault ?
I've tried this in CS and it did create one but it seems like I can't get this to working in TFC. Could you maybe have a look bugsy? |
Re: nVault
Is there a function to prune only one key in a vault file? For example the scores in the example plugin.
|
Re: nVault
Code:
|
Re: nVault
What's the point of nvault_touch if it only updates timestamp? I would as well want it to update my data.
Do I have to first make a check if data exists and only then use nvault_set? (Finds first and replaces the data after) Or does nvault_set do it automatically without creating a new record? Nothing is mentioned if nvault_set updates or not. Just interested. |
Re: nVault
Quote:
Quote:
If it were to create new entries instead of updating, those keys would not be unique. So the answer is: yes, nvault_set() updates the data if it already exists. |
Re: nVault
Thanks for the stock exolent. However, how should I check if the days expired, and then prune that key for the user? A general pruning doesn't work, since I don't know how to do that with your stock. Right now I have this:
PHP Code:
|
Re: nVault
Quote:
|
Re: nVault
See my post in #34. It just only for one player and does work IMO.
|
Re: nVault
Sorry about that bibu, I didn't see page 2 for some reason and that this has already been resolved. Exolents function will do just fine, the only thing I think you have wrong is the window of time for pruning entries. Correct me if I'm wrong, I'm a little rusty.
PHP Code:
End: 86400 * days = Days to remove in seconds (which is days starting from 01/01/1970 or w\e). Should be Start: 0 (01/01/1970) End: get_systime() - ( get_pcvar_num( amx_death_expire_days ) * 86400 ) ) (Now - X days) |
Re: nVault
Bugsy I am thinking abit wrong and couldn't really understand your explanation. Could you rephrase that?
|
Re: nVault
Quote:
PHP Code:
PHP Code:
|
Re: nVault
Quote:
Quote:
0 < 17 day old entry to delete < 15 days from now 0 < 1328148921(now minus 17 days) < 1328321721(now minus 15 days) Here's some math: Start = 0 Calculate End time value (Now - 15 days)
Outdated entry example to delete (17 days old, Now - 17 days)
Delete this 17 day old entry since: 0(Start) < 1328142046(17 day old entry) < 1328314846(now - 15 days) So what you want is this: PHP Code:
|
All times are GMT -4. The time now is 09:55. |
Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.