Return to Main Page

Using KeyValues

Page white text.png Description:How to use Source Engine KeyValues.
link=User:Dim1xs Author:Dim1xs
Calendar.png Created:April 13th 2024

The Beggining

The KeyValues format is used in the Source engine to store meta data for resources, scripts, materials, VGUI elements, and more...

KeyValues(VDF) is a bit similar to JSON, but unfortunately JSON is not yet embedded in HL2GMED (you still can use JSON library for lua)

Look here for all KeyValues class methods.

KeyValues usage examples

Let's try to parse some script, for example we could try to read weapon script:

      
function parseWeaponScript( strPath )
   local curfilename = strPath
   local kv = KeyValues("weapon_357.txt")
   kv:LoadFromFile(curfilename)

   local sub = kv:GetFirstSubKey()
   while sub ~= NULL_KEYVALUES do
      print(sub:GetName().."=="..sub:GetString())
      sub = sub:GetNextKey()
   end

   kv:deleteThis()
end
                  
parseWeaponScript("scripts/weapon_357.txt")

                                 
Don't forget to remove KeyValue object after use, otherwise it will make a lot of garbage in Lua memory!

Output:
printname==#HL2_357Handgun
viewmodel==models/weapons/v_357.mdl
playermodel==models/weapons/w_357.mdl
anim_prefix==python
Bucket==1
bucket_position==1
clip_size==6
default_clip==6
primary_ammo==357
secondary_ammo==None
weight==7
item_flags==0
damage==75
SoundData==
TextureData==                                                                    
                                 
Here's also another example, how to loop values.
Make sure you are using NULL_KEYVALUES instead of NULL

Thank you for reading this article, see ya later!

JOIN HL2GMED DISCORD SERVER!