How do I use the KeyValues class?

Please post any questions about developing your plugin here. Please use the search function before posting!
User avatar
Doldol
Senior Member
Posts: 200
Joined: Sat Jul 07, 2012 7:09 pm
Location: Belgium

How do I use the KeyValues class?

Postby Doldol » Tue Apr 11, 2017 11:11 am

It isn't very clear to me how to best use this class.

Let's say I'm trying to write this to a file (I don't care about newlines/whitespace), how would I construct this best using the KeyValues class? (Or is there a better way?)

Code: Select all

"Admins"
{
   "Admin1"
   {
      "auth"      "steam"
      "identity"      "STEAM_0:1:1337"
      "flags"      "z"
      "group"      "Group1"
      "immunity"   "100"
   }
   "Admin2"
   {
      "auth"      "steam"
      "identity"      "STEAM_0:1:13372"
      "flags"      "a"
      "group"      "Group2"
      "immunity"   "1"
   }
}
User avatar
Ayuto
Project Leader
Posts: 2193
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: How do I use the KeyValues class?

Postby Ayuto » Tue Apr 11, 2017 4:50 pm

Syntax: Select all

from keyvalues import KeyValues

root = KeyValues('Admins')

admin1 = root.find_key('Admin1', True)
admin1.set_string('auth', 'steam')
admin1.set_string('identity', 'STEAM_0:1:1337')
admin1.set_string('flags', 'z')
admin1.set_string('group', 'Group1')
admin1.set_string('immunity', '100')

admin2 = root.find_key('Admin2', True)
admin2.set_string('auth', 'steam')
admin2.set_string('identity', 'STEAM_0:1:13372')
admin2.set_string('flags', 'a')
admin2.set_string('group', 'Group2')
admin2.set_string('immunity', '1')

# Will create a file called 'admins.txt' in the game directory (e.g. cstrike)
root.save_to_file('admins.txt')
I think it makes sense to add a 'from_dict' method to construct a KeyValues class from a dictionary.
User avatar
Doldol
Senior Member
Posts: 200
Joined: Sat Jul 07, 2012 7:09 pm
Location: Belgium

Re: How do I use the KeyValues class?

Postby Doldol » Tue Apr 11, 2017 6:17 pm

Thanks! I understand how it works now.

Ayuto wrote:I think it makes sense to add a 'from_dict' method to construct a KeyValues class from a dictionary.


Yes please!
User avatar
Ayuto
Project Leader
Posts: 2193
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: How do I use the KeyValues class?

Postby Ayuto » Tue Apr 11, 2017 6:49 pm

There you go:
https://github.com/Source-Python-Dev-Te ... 28fa4af32c

Test code:

Syntax: Select all

from pprint import pprint

from colors import RED
from events.manager import game_event_manager
from memory import get_object_pointer

d = {
'key1': '0',
'key2': 2,
'key3': 2.3,
'key4': {
'subkey1': '4',
'subkey2': 5,
'subkey3': 6.7,
'subkey4': {
'subsubkey1': '4',
'subsubkey2': 5,
'subsubkey3': 6.7,
'subsubkey4': RED,
'subsubkey5': 2<<32,
'subsubkey6': get_object_pointer(game_event_manager),
'subsubkey7': False,
'subsubkey8': 0
}
},
'key5': 1,
'key6': True,
'key7': -3,
'key8': -(2<<30)
}

pprint(d)

new_root = KeyValues.from_dict('Admins', d)
pprint(new_root.as_dict())
User avatar
Doldol
Senior Member
Posts: 200
Joined: Sat Jul 07, 2012 7:09 pm
Location: Belgium

Re: How do I use the KeyValues class?

Postby Doldol » Tue Apr 11, 2017 8:19 pm

That's fast. & Supporting nested dicts out of the box, I like the implementation.
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Re: How do I use the KeyValues class?

Postby decompile » Tue May 02, 2017 12:17 am

Would there be any support for file paths instead of the filename in the game directory?

Or should we move the file afterwards?
User avatar
Ayuto
Project Leader
Posts: 2193
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: How do I use the KeyValues class?

Postby Ayuto » Tue May 02, 2017 5:07 am

Both, relative and absolute paths, are supported.

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 21 guests