Raised This Month: $51 Target: $400
 12% 

Little help with fetching keyvalues


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
IceCucumber
Member
Join Date: Dec 2011
Old 10-01-2014 , 16:27   Little help with fetching keyvalues
Reply With Quote #1

Hello,

I've been playing around with keyvalues a bit, but am having difficulty retrieving values.
My keyvalues file looks something like this:
Quote:
"MyKeyValues"
{
   "lowerlevel"
   {
      "STEAM_0:0:123"
      {
         "test_1"      "foobar"
         "test_2"      "some value"
      }
   }
}
And the code (i'm trying to return the value of "test_2"):
PHP Code:
new Handle:kv CreateKeyValues("MyKeyValues");
FileToKeyValues(kv"addons/sourcemod/configs/mykeyvalues.kv");

new 
String:kvBuffer[32];

KvRewind(kv);
KvJumpToKey(kv"STEAM_0:0:123");
KvGetString(kv"test_2"kvBuffersizeof(kvBuffer), "default if not found");
KvRewind(kv);

CloseHandle(kv);

PrintToServer("kvBuffer: %s"kvBuffer); 
However, PrintToServer returns "default if not found" instead of "some value". How would I properly navigate this sort of keyvalues structure to retrieve a value?
Do I need to traverse through "lowerlevel" somehow before KvJumpToKey? I tried KvGotoFirstSubKey(kv), to no avail.

Last edited by IceCucumber; 10-01-2014 at 16:47.
IceCucumber is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 10-01-2014 , 16:51   Re: Little help with fetching keyvalues
Reply With Quote #2

Yes, you need to traverse through "lowerlevel" before you can KvJumpToKey "STEAM_0:0:123". I'm not sure why doing KvGotoFirstSubKey first didn't work for you, though.
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
IceCucumber
Member
Join Date: Dec 2011
Old 10-01-2014 , 17:09   Re: Little help with fetching keyvalues
Reply With Quote #3

PHP Code:
new String:section[64];

KvRewind(kv);

KvGetSectionName(kvsectionsizeof(section));
PrintToServer("section: %s"section);

KvGotoFirstSubKey(kv); // shouldn't we be at "lowerlevel" now, not "MyKeyValues" anymore?

KvGetSectionName(kvsectionsizeof(section));
PrintToServer("section: %s"section); 
Returns:
Quote:
section: MyKeyValues
section: MyKeyValues

Last edited by IceCucumber; 10-01-2014 at 17:11.
IceCucumber is offline
xf117
Senior Member
Join Date: Mar 2010
Location: Russia
Old 10-02-2014 , 07:52   Re: Little help with fetching keyvalues
Reply With Quote #4

Are you sure you are loading kv file fine? Looks like your plugin can't access the file and only creates the kv. That's why it only has MyKeyValues, it has nothing else to work with. Check the boolean from
PHP Code:
FileToKeyValues(kv"addons/sourcemod/configs/mykeyvalues.kv"); 
xf117 is offline
Send a message via ICQ to xf117
WildCard65
Veteran Member
Join Date: Aug 2013
Location: Canada
Old 10-02-2014 , 09:35   Re: Little help with fetching keyvalues
Reply With Quote #5

Best to never hard code a path to SM folder, use the BuildPath native.
__________________
WildCard65 is offline
splewis
Veteran Member
Join Date: Feb 2014
Location: United States
Old 10-02-2014 , 11:01   Re: Little help with fetching keyvalues
Reply With Quote #6

Here's how you'd use BuildPath:

Code:
decl String:configFile[PLATFORM_MAX_PATH];
BuildPath(Path_SM, configFile, sizeof(configFile), "configs/mykeyvalues.cfg");
You could explicitly check if the file exists too, but the return value of FileToKeyValues should do that as well I believe.
__________________
splewis is offline
Marcus_Brown001
AlliedModders Donor
Join Date: Nov 2012
Location: Illinois, United States
Old 10-02-2014 , 12:36   Re: Little help with fetching keyvalues
Reply With Quote #7

FileToKeyValues returns true if the conversion was successful, false if it isn't. I don't know if it fails where the path would be if it failed, though.
Marcus_Brown001 is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 15:32.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode