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

Iterating over my weapon keyvalues


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
wauterboi
Junior Member
Join Date: Dec 2013
Old 01-05-2018 , 20:06   Iterating over my weapon keyvalues
Reply With Quote #1

Hello, I'm pretty confused on how to work with keyvalues.

Code:
"weapons"
{
    "scout"
    {
        "Scattergun"            "13"
        "Force-A-Nature"        "45"
        "The Shortstop"         "220"
        "The Soda Popper"       "448"
    }
    "soldier"
    {
        "Rocket Launcher"       "18"
    }
    "pyro"
    {
        "Flame Thrower"         "21"
    }
    "demoman"
    {
        "Grenade Launcher"      "19"
    }
    "heavy"
    {
        "Minigun"               "15"
    }
    "engineer"
    {
        "Shotgun"               "9"
    }
    "medic"
    {
        "Syringe Gun"           "17"
    }
    "sniper"
    {
        "Sniper Rifle"          "14"
    }
    "spy"
    {
        "Revolver"              "24"
    }
}
Here's my current keyvalues file - it will be filled out a lot more once I figure out how to get this working. Basically, what I want to do is fill up an array with these weapon indexes, and another array with the weapon names - something like g_WeaponIndexes[9][] and g_WeaponNames[9][] - that way I can use the same ID to refer to the same weapon in both arrays.

What would be a good way to go about this?
wauterboi is offline
Dr!fter
The Salt Boss
Join Date: Mar 2007
Old 01-05-2018 , 22:35   Re: Iterating over my weapon keyvalues
Reply With Quote #2

PHP Code:
#include <sourcemod>

char g_szClasses[][] = {"scout""soldier""pyro""demoman""heavy""engineer""medic""sniper""spy"};

ArrayList pWeaponNames[sizeof(g_szClasses)];
ArrayList pWeaponIndexes[sizeof(g_szClasses)];

public 
void OnPluginStart()
{
    for(
int i 0sizeof(g_szClasses); i++)
    {
        
pWeaponNames[i] = new ArrayList(128);
        
pWeaponIndexes[i] = new ArrayList();
    }
    
    
char path[PLATFORM_MAX_PATH];
    
    
BuildPath(Path_SMpathsizeof(path), "data/test.kv");
    
    
KeyValues kv = new KeyValues("weapons");
    
    if(
kv.ImportFromFile(path))
    {
        
kv.GotoFirstSubKey(true);
            
        do
        {
            
char sec[128];
            
kv.GetSectionName(secsizeof(sec));
            
            
int index = -1;
            
            for(
int i 0sizeof(g_szClasses); i++)
            {
                if(
StrEqual(g_szClasses[i], sec))
                {
                    
index i;
                    break;
                }
            }
            
            if(
index == -1)
            {
                
PrintToServer("Invalid key found, exiting");
                
delete kv;
                return;
            }
            
            
kv.GotoFirstSubKey(false);
            do
            {
                
kv.GetSectionName(secsizeof(sec));
                
                
pWeaponNames[index].PushString(sec);
                
pWeaponIndexes[index].Push(kv.GetNum(NULL_STRING));
                
            }while(
kv.GotoNextKey(false))
                
            
kv.GoBack();
            
        }while(
kv.GotoNextKey(true))
        
        
//Print everything out.
        
for(int x 0sizeof(g_szClasses); x++)
        {
            
PrintToServer(g_szClasses[x]);
            for(
int i 0pWeaponNames[x].Lengthi++)
            {
                
char name[128];
                
pWeaponNames[x].GetString(inamesizeof(name));
                
                
PrintToServer("    %s -> %i"namepWeaponIndexes[x].Get(i));
            }
        }
        
        
delete kv;
        return;
    }
    
    
PrintToServer("Failed to parse test.kv");
    
delete kv;

Thats an example, everything is stored in arraylists. There is a sample of looping through everything in there too. I made it check against a list of classes so no matter the order of the kv the classes will still be mapped correctly.
Dr!fter is offline
wauterboi
Junior Member
Join Date: Dec 2013
Old 01-06-2018 , 20:32   Re: Iterating over my weapon keyvalues
Reply With Quote #3

Thanks a ton! I'll be trying this out tomorrow. Might I ask why you used the naming conventions for pWeaponNames? Does the "p" represent "public"?
wauterboi is offline
Dr!fter
The Salt Boss
Join Date: Mar 2007
Old 01-07-2018 , 00:05   Re: Iterating over my weapon keyvalues
Reply With Quote #4

Quote:
Originally Posted by wauterboi View Post
Thanks a ton! I'll be trying this out tomorrow. Might I ask why you used the naming conventions for pWeaponNames? Does the "p" represent "public"?
pointer, since they are technically handles which is sort of like pointers in c++. My naming convention is always all over the place. In the old syntax I would probably have used hWeaponNames.
Dr!fter 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 04:31.


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