Keyvalues.KeyValues

From EventScripts Community Encyclopedia



Contents

Class: keyvalues.KeyValues

Note: Requires EventScripts 2.0
Module: keyvalues
EventScripts version: 2.0

Overview

Description

This is a wrapper class for Valve's C++ KeyValues class. It is basically a tree-based datastructure where every node has a name and a value.

Quick Example

import keyvalues
k = keyvalues.KeyValues(name="top")
k['myvalue'] = 'hello'
k['mysubkey'] = keyalues.KeyValues(name='mysubkey')
k['mysubkey']['mysubvalue'] = 99
k.save('/tmp/mykeyfile.txt')

Methods

__init__(name=None, parent=None, existingid=None, filename=None) (i.e. the constructor)

You can create a new KeyValues object by passing in different combinations of arguments: With only an existingid, KeyValues returns a wrapper around that existing id (i.e. C++ pointer). With only a parent, we'll create a new randomly named subkey. With a parent and name, we'll find/make a subkey of that name. With a name only, we'll create an unparented key with that name. With a filename, we'll load the KeyValues tree from disk and return it as the parent.

.load(filename)

Loads this KeyValue's contents from a file.

.save(filename)

Save this KeyValue's contents to a file.

.getParent()

Returns the parent KeyValues class to this one.

.keys()

Returns a list of top-level subkeys.

__getitem__(name) (i.e. dictionary-style lookup)

Look up a value by name, a lot like a dictionary. Can return a value or a subkey, depending on what is returned.

__setitem__(self, name, value) (i.e. dictionary-style storing)

Stores a name/value pair in the keygroup. Only ints, floats, strings, and KeyValues classes (as subkeys) can be stored.

getName()

Returns the key's name

getString(valname)
getFloat(valname)
getInt(valname)

Returns directly a typed version of the key's value for that value name.

__iter__() (i.e. list-style iteration)

Allows you to loop through the KeyValues class subkeys in a for x in mykey: command.

Attributes

{{{attributes}}}

Notes

  • The memory for a KeyValues object is released when a KeyValues object goes out of scope and has no references to it. It will not release the memory if the top of the tree was created by es.keygroupcreate unless you use es.keygroupdelete.
  • Be careful that no other scripts does es.keygroupdelete on your KeyValyes object or crashes will ensure when you try to use it.
  • When opening a file it must not contain multi-line comments (/*comment */) as it will be parsed incorrectly

See Also

blog comments powered by Disqus