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

Keyvalues


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Player 1
Member
Join Date: Apr 2012
Old 07-11-2012 , 22:50   Keyvalues
Reply With Quote #1

I'm indexing coordinates around the map for a teleport plugin and i'm having trouble setting up the keyvalues retrieval. I wrote a simple test plugin to try and read the value at
locations > clocktower > emp > x
but i get this error:

KeyValues Error: RecursiveLoadFromBuffer: got EOF instead of keyname in file addons/sourcemod/teleportlocations.txt
locations, (*clocktower*), (*prime*),

PHP Code:
"locations"
{
    
"clocktower"
    
{
        
"ct"
        
{
            
"x"    "-129"
            "y"    "5127"
            "z"    "-1390"
        
}
        
"emp"
        
{
            
"x"    "-3805"
            "y"    "-5324"
            "z"    "-1370"
        
}
        
"prime"
        
{
            
"x"    "-1125"
            "y"    "-45"
            "z"    "-1340"
        
}
    }

PHP Code:
public Action:CMD_TEST(clientargs) {
    new 
value GetCoord("emp""x");
    
PrintToChat(client"%d"value);
}

GetCoord(const String:Location[], const String:Coord[]) {
    new 
value
    decl String
:MapName[64];
    
GetCurrentMap(MapNamesizeof(MapName));
    
    new 
Handle:kv CreateKeyValues("locations");
    
FileToKeyValues(kv"addons/sourcemod/teleportlocations.txt");
    
KvJumpToKey(kvMapNamefalse);
    
KvJumpToKey(kvLocationfalse);
    
value KvGetNum(Handle:kvCoord);
    
CloseHandle(kv);
    return 
value;

Player 1 is offline
psychonic

BAFFLED
Join Date: May 2008
Old 07-12-2012 , 08:00   Re: Keyvalues
Reply With Quote #2

Quote:
Originally Posted by Player 1 View Post
I'm indexing coordinates around the map for a teleport plugin and i'm having trouble setting up the keyvalues retrieval. I wrote a simple test plugin to try and read the value at
locations > clocktower > emp > x
but i get this error:

KeyValues Error: RecursiveLoadFromBuffer: got EOF instead of keyname in file addons/sourcemod/teleportlocations.txt
locations, (*clocktower*), (*prime*),

PHP Code:
"locations"
{
    
"clocktower"
    
{
        
"ct"
        
{
            
"x"    "-129"
            "y"    "5127"
            "z"    "-1390"
        
}
        
"emp"
        
{
            
"x"    "-3805"
            "y"    "-5324"
            "z"    "-1370"
        
}
        
"prime"
        
{
            
"x"    "-1125"
            "y"    "-45"
            "z"    "-1340"
        
}
    }

PHP Code:
public Action:CMD_TEST(clientargs) {
    new 
value GetCoord("emp""x");
    
PrintToChat(client"%d"value);
}

GetCoord(const String:Location[], const String:Coord[]) {
    new 
value
    decl String
:MapName[64];
    
GetCurrentMap(MapNamesizeof(MapName));
    
    new 
Handle:kv CreateKeyValues("locations");
    
FileToKeyValues(kv"addons/sourcemod/teleportlocations.txt");
    
KvJumpToKey(kvMapNamefalse);
    
KvJumpToKey(kvLocationfalse);
    
value KvGetNum(Handle:kvCoord);
    
CloseHandle(kv);
    return 
value;

I'm a bit rusty when it comes to the sp keyvalues api, but I think you need to jump down a level after you jump to the map key, using KvGotoFirstSubKey.
psychonic is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 07-12-2012 , 08:16   Re: Keyvalues
Reply With Quote #3

Just a comment, rather than storing x, y, and z separately, consider storing them as a Vector.

Quote:
Originally Posted by psychonic View Post
I'm a bit rusty when it comes to the sp keyvalues api, but I think you need to jump down a level after you jump to the map key, using KvGotoFirstSubKey.
Having worked with KeyValues more recently, I believe you're correct.
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 07-12-2012 at 08:18.
Powerlord is offline
Player 1
Member
Join Date: Apr 2012
Old 07-12-2012 , 12:19   Re: Keyvalues
Reply With Quote #4

Thanks for your help, both of you.
Player 1 is offline
Player 1
Member
Join Date: Apr 2012
Old 07-12-2012 , 16:34   Re: Keyvalues
Reply With Quote #5

Now I've run into another problem. I want to list the names of the keys under the map names, but after following this thread:
https://forums.alliedmods.net/showthread.php?t=184725
my function lists the map names instead of the keys beneath it. It sounds like Leonardo got his function working, and I can't see what I'm really doing differently to break mine.

Just for clarification, if I'm on clocktower and I run this command, I want the console to read out:
ct
emp
prime
someotherlocation

PHP Code:
"Locations"
{
    
"clocktower"
    
{
        
"ct"    "-129.0 5127.0 -1390"
        "emp"    "-3805.0 -5324.0 -1370.0"
        "prime" "50.0 -927.0 -1370.0"
        "someotherlocation" "1 2 3"
    
}
    
"downtown"
    
{
        
"ct"    "6438.0 -136.0 -3760.0"
        "emp"    "-5672.0 1.0 -3700"
        "prime"    "518.0 2.0 -3750"
    
}

PHP Code:
public Action:CMD_ListLocations(clientargs) {
    
decl String:MapName[64], String:key[64];
    
GetCurrentMap(MapNamesizeof(MapName));
    
    new 
Handle:kv CreateKeyValues("locations");
    
FileToKeyValues(kv"addons/sourcemod/teleportlocations.txt");
    
KvJumpToKey(kvMapNamefalse);
    
KvGotoFirstSubKey(kvfalse);
    
    if (
GetCmdReplySource() == SM_REPLY_TO_CHAT) {
        
PrintToChat(client"[SM] See console for output.");
    }
    
    
PrintToConsole(client"Teleport locations on %s:"MapName);
    
    do {
        
KvGetSectionName(kvkeysizeof(key));
        
PrintToConsole(client"%s"key);
    } while (
KvGotoNextKey(kv));

    
CloseHandle(kv);
    return 
Plugin_Handled;


Last edited by Player 1; 07-12-2012 at 16:49.
Player 1 is offline
Player 1
Member
Join Date: Apr 2012
Old 07-12-2012 , 23:00   Re: Keyvalues
Reply With Quote #6

Nevermind, I figured it out. Be sure to check the bools in KvGotoFirstSubKey and KvGotoNextKey if you're reading this.
So the "while" part of the do-while loop above needed to be this instead:
while (KvGotoNextKey(kv, false));
Player 1 is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 07-13-2012 , 08:40   Re: Keyvalues
Reply With Quote #7

Quote:
Originally Posted by Player 1 View Post
Nevermind, I figured it out. Be sure to check the bools in KvGotoFirstSubKey and KvGotoNextKey if you're reading this.
So the "while" part of the do-while loop above needed to be this instead:
while (KvGotoNextKey(kv, false));
A common idiom while reading KeyValues is to do this:


PHP Code:
if (!KvGotoFirstSubKey(kvtrue))
{
    return; 
// or continue; if in a loop
}

do
{

} while (
KvGotoNextKey(kvtrue)); 
Note that the bool argument to both Kv functions should be identical.
__________________
Not currently working on SourceMod plugin development.
Powerlord 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 12:41.


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