Raised This Month: $32 Target: $400
 8% 

Help, KeysValues get key name


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
PunkChop
Junior Member
Join Date: Jul 2018
Old 07-26-2018 , 04:46   Help, KeysValues get key name
Reply With Quote #1

i have this txt file:
PHP Code:

"Servers"
{
    
"CSGO.SERVER # Fun"        "62.88.189.161:26371"
    "CSGO.SERVER # Bhop"     "82.81.169.160:25126"
    "CSGO.SERVER  # RolePlay"     "82.81.169.125:5125"
    "CSGO.SERVER  # JailBreak"     "22.82.159.212:25251"
    "CSGO.SERVER  # Combat Surf"     "82.82.129.124:2220"

and this code:

PHP Code:

StringMap serversList
;
public 
loadServersList()
{
    
KeyValues kv = new KeyValues("Servers");
    if(!
kv.ImportFromFile("addons/sourcemod/configs/ServersList.txt"))
    {
        
PrintToChatAll("Cannot find servers list file");
    }
    
    
kv.GotoFirstSubKey(true);
    do
    {
            
char key[64], value[64];
            
KvGetSectionName(kvkeysizeof(key));
            
kv.GetString(keyvaluesizeof(value));
            
PrintToChatAll("im here4 %s | %s"keyvalue);
            
/* Add it to the global list */
            
serversList.SetValue(keyvaluefalse); // cannot add key & value to serversList(Type: StringMap)
            
PrintToChatAll("im here5");

    } while (
kv.GotoNextKey(false));

im trying to get with key the server name & value
but it giving me key "Servers"

Please help me.

Last edited by PunkChop; 07-26-2018 at 07:23.
PunkChop is offline
mug1wara
AlliedModders Donor
Join Date: Jun 2018
Old 07-26-2018 , 05:30   Re: Help, KeysValues get key name
Reply With Quote #2

Wrong:

PHP Code:
kv.GetString(keyvaluesizeof(value)); 
Because "key" is section name, which you showed before.

Right:

PHP Code:
kv.GetString("The Key Name"valuesizeof(value)); 
mug1wara is offline
PunkChop
Junior Member
Join Date: Jul 2018
Old 07-26-2018 , 06:22   Re: Help, KeysValues get key name
Reply With Quote #3

Quote:
Originally Posted by mug1wara View Post
Wrong:

PHP Code:
kv.GetString(keyvaluesizeof(value)); 
Because "key" is section name, which you showed before.

Right:

PHP Code:
kv.GetString("The Key Name"valuesizeof(value)); 
yeah, im trying to get the key name.. so section name not help me.. i want the var:"key" to contain"CSGO.SERVER..."
do you know good way to get the key name without write anytime "CSGO.SERVER...."?

Last edited by PunkChop; 07-26-2018 at 06:24.
PunkChop is offline
8guawong
AlliedModders Donor
Join Date: Dec 2013
Location: BlackMarke7
Old 07-26-2018 , 06:50   Re: Help, KeysValues get key name
Reply With Quote #4

Your keyvalues isn't even formatted correctly....
https://wiki.alliedmods.net/KeyValue...eMod_Scripting)
__________________
8guawong is offline
PunkChop
Junior Member
Join Date: Jul 2018
Old 07-26-2018 , 07:14   Re: Help, KeysValues get key name
Reply With Quote #5

Quote:
Originally Posted by 8guawong View Post
Your keyvalues isn't even formatted correctly....
https://wiki.alliedmods.net/KeyValue...eMod_Scripting)
updated, now i can't add key&value to serversList

Last edited by PunkChop; 07-26-2018 at 07:23.
PunkChop is offline
DJ Tsunami
DJ Post Spammer
Join Date: Feb 2008
Location: The Netherlands
Old 07-26-2018 , 07:33   Re: Help, KeysValues get key name
Reply With Quote #6

You can't get key names with KeyValues. You need to use SMCParser, which fires an event for every key/value pair.

Or you can write the config file like so:
PHP Code:
"Servers"
{
    
"1"
    
{
        
"name"  "CSGO.SERVER # Fun"
        "host"  "62.88.189.161:26371"
    
}
    
"2"
    
{
        
"name"  "CSGO.SERVER # Bhop"
        "host"  "82.81.169.160:25126"
    
}

And then use kv.GotoNextKey() to loop through all sections and get the name and host.
__________________
Advertisements | REST in Pawn - HTTP client for JSON REST APIs
Please do not PM me with questions. Post in the plugin thread.

Last edited by DJ Tsunami; 07-26-2018 at 07:35.
DJ Tsunami is offline
PunkChop
Junior Member
Join Date: Jul 2018
Old 07-26-2018 , 07:47   Re: Help, KeysValues get key name
Reply With Quote #7

Quote:
Originally Posted by DJ Tsunami View Post
You can't get key names with KeyValues. You need to use SMCParser, which fires an event for every key/value pair.

Or you can write the config file like so:
PHP Code:
"Servers"
{
    
"1"
    
{
        
"name"  "CSGO.SERVER # Fun"
        "host"  "62.88.189.161:26371"
    
}
    
"2"
    
{
        
"name"  "CSGO.SERVER # Bhop"
        "host"  "82.81.169.160:25126"
    
}

And then use kv.GotoNextKey() to loop through all sections and get the name and host.
ok.. Thanks i did it:

PHP Code:

"Servers"
{
    
"CSGO.SERVER # Fun"
    
{
        
"IP"    "82.81.169.166:27010"
    
}
    
"CSGO.SERVER # Bhop"
     
{
        
"IP"    "82.81.169.160:27010"
    
}
    
"CSGO.SERVER # RolePlay"
     
{
        
"IP"    "82.81.169.185:27015"
    
}
    
"CSGO.SERVER # JailBreak"
     
{
        
"IP"    "82.81.169.182:27915"
    
}
    
"CSGO.SERVER # Combat Surf"
     
{
        
"IP"    "82.81.169.184:27020"
    
}

PHP Code:

    kv
.GotoFirstSubKey(true);
    do
    {
            
char key[64];
            
char value[64];
            
KvGetSectionName(kvkeysizeof(key));
            
kv.GetString("IP"valuesizeof(value));

            
/* Add it to the global list */
            
serversList.SetString(keyvaluefalse);

    } while (
kv.GotoNextKey(false)); 

Last edited by PunkChop; 07-26-2018 at 09:12.
PunkChop is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 07-26-2018 , 10:59   Re: Help, KeysValues get key name
Reply With Quote #8

KvGetString with NULL_STRING as the key will return the current key name.
__________________

Last edited by asherkin; 07-26-2018 at 11:01.
asherkin 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 08:38.


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