PDA

View Full Version : Working with KeyValues


Shaman
07-09-2007, 15:48
1-How can I write or change values in a KV file?
2-Is there anyway to access a value like: FindIntKeyValue(kv, 'Data/Players/Shaman/Money')?
3-Is there any other saving method? (Other than SQL. Like nVault?)
I want to use sql, but some users don't have SQL servers and it's hard to install a SQL plugin.

What I am trying to do is make a file that stores users information like experience points, level, money and things that they have bought.

sumguy14
07-09-2007, 16:18
I suggest SQL, people can go to sites and get free SQL databases if they need one.

KeyValue's work like a tree

"rootnode"
{
"key1"
{
"valuename" "value"
}
}

That is a standard KeyValue's file, you can store simple things like XP or level

"playerdata"
{
"STEAM_0:0:8274827"
{
"xp" "50"
"level" "2"
}
}

To create this you would do:

new Handle:config; // Make db handle

InitDatabase
{
config=CreateKeyValues("playerdata"); // Creates the KeyValue tree in memory
decl String:path[PLATFORM_MAX_PATH];
BuildPath(Path_SM,path,PLATFORM_MAX_PATH,"configs/dbfile.txt"); // Builds a path to your db file
if(!FileToKeyValues(config,path)) // Transfers the file to memory, if it returns false, it will kill the plugin and spit out an error
SetFailState("Failed to load database!");
}

AddPlayerToDB(const String:steamid[])
{
KvRewind(config); // Rewind our keyvalue tree to the top
if(KvJumpToKey(config,steamid,false)) // Will try to jump to the player's key that holds his info, and if it exists it will stop here
return;
KvJumpToKey(config,steamid,true); // This will create the player's key
KvSetNum(config,"xp",0); // Creates the valuename that stores XP
KvSetNum(config,"level",1); // Creates the valuename that stores level (defaults to level 1)
// We're done!
}

SaveDatabase
{
decl String:path[PLATFORM_MAX_PATH];
BuildPath(Path_SM,path,PLATFORM_MAX_PATH,"configs/dbfile.txt"); // Builds a path to your db file
KeyValuesToFile(config,path); // Transfer to file for storage
}

Hope this helps! There might be a few minor mistakes or typos, not exactly a SM pro

Shaman
07-09-2007, 16:35
Thanks :)

Shaman
07-11-2007, 05:13
Solved.