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

Solved KeyValues


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
fiction
Member
Join Date: May 2017
Old 11-01-2017 , 13:29   KeyValues
Reply With Quote #1

Hello AM, I'm hoping someone can inform me how to parse some KeyValues correctly.

Here is an example of what I need to parse:
PHP Code:
"Root"
{
    
"Random Section 1"
    
{
        
"Random Key 1" "Random Value 1"
        "Random Key 2" "Random Value 2"
    
}

Whenever I've used KeyValues in the past I've always known all the possible key names in advance. Now to make the config cleaner I'd like to make the key names part of the data rather than changing the format so it has a whole extra level. I've looked through the KeyValues API section a few times and can't figure out how I'd do this, any help is appreciated

Last edited by fiction; 11-02-2017 at 16:21. Reason: Solved
fiction is offline
Vaggelis
Senior Member
Join Date: May 2017
Old 11-01-2017 , 13:39   Re: KeyValues
Reply With Quote #2

check my code here https://forums.alliedmods.net/showthread.php?t=297636
Vaggelis is offline
fiction
Member
Join Date: May 2017
Old 11-01-2017 , 13:47   Re: KeyValues
Reply With Quote #3

Quote:
Originally Posted by Vaggelis View Post
Hey, thanks for your response but it doesn't really solve my issue.

In your plugin you already know the name of the key, in that case it's "Current Money". For example let's say I want to make a plugin where I can filter certain words for certain players and replace them with others, my config might looking something like this:
PHP Code:
"Root" 
{
    
"STEAM_1:0:11101"
    
{
        
"green" "blue"
        "car" "bus"
        "pizza" "fries"
    
}

But now I don't know the key names, so I can't use kv.GetString(). To add I plan to put these values into a StringMap for each player, if someone has a better solution to actually storing the data that would be great too.
fiction is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 11-01-2017 , 14:27   Re: KeyValues
Reply With Quote #4

Between GotoNextKey, GotoFirstSubKey and GetSectionName (and GetString), you can traverse your KV data.

An alternative would be using a DB to store your data.

Edit: if you don't know the exact layout of your data ahead of time, then GetDataType would also be required to tell subsections from subkeys. But if it's like your example data but with multiple replacements and multiple SteamID sections, you wouldn't need it.

Last edited by Fyren; 11-01-2017 at 14:29.
Fyren is offline
fiction
Member
Join Date: May 2017
Old 11-01-2017 , 15:12   Re: KeyValues
Reply With Quote #5

Quote:
Originally Posted by Fyren View Post
Between GotoNextKey, GotoFirstSubKey and GetSectionName (and GetString), you can traverse your KV data.

An alternative would be using a DB to store your data.

Edit: if you don't know the exact layout of your data ahead of time, then GetDataType would also be required to tell subsections from subkeys. But if it's like your example data but with multiple replacements and multiple SteamID sections, you wouldn't need it.
Is that what the API implies by saying "or NULL_STRING", you can get the current keys value without its name?
fiction is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 11-01-2017 , 22:06   Re: KeyValues
Reply With Quote #6

Yes.
Fyren is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 11-02-2017 , 03:20   Re: KeyValues
Reply With Quote #7

This one is little different.
https://forums.alliedmods.net/showpo...82&postcount=3
__________________
Do not Private Message @me

Last edited by Bacardi; 11-02-2017 at 03:20.
Bacardi is offline
fiction
Member
Join Date: May 2017
Old 11-02-2017 , 16:21   Re: KeyValues
Reply With Quote #8

Quote:
Originally Posted by Bacardi View Post
Appreciate your response, I did play around with what you posted a little but KeyValues seems to lack a clear difference between a section and a key. I've decided to use the SMCParser class instead and that works nicely since it has a callback just for entering sections and another for each key-value.
fiction is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 11-02-2017 , 18:00   Re: KeyValues
Reply With Quote #9

Indeed, smc parser is also good
__________________
Do not Private Message @me
Bacardi is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 11-03-2017 , 08:03   Re: KeyValues
Reply With Quote #10

Quote:
Originally Posted by fiction View Post
KeyValues seems to lack a clear difference between a section and a key.
In case someone else in the future cares, you can tell the difference using GetDataType.
Fyren 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 17:20.


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