Raised This Month: $32 Target: $400
 8% 

Solved KeyValues kv.GoBack


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Spirit_12
Veteran Member
Join Date: Dec 2012
Location: Toronto, CA
Old 05-27-2018 , 17:33   KeyValues kv.GoBack
Reply With Quote #1

I'm trying to write all the weapon names onto a keyvalues file. I just can't seem to figure how can I bring the tree position back one level within a loop.

Here is the code.

PHP Code:
char sWeaponAssault[5][32] = 
{
    
"rifle",
    
"rifle_desert",
    
"rifle_ak47",
    
"rifle_sg552",
    
"rifle_m60",
};

WeaponNamesToFile()
{
    
// Build file path.
    
BuildPath(Path_SMkv_Pathsizeof(kv_Path), "../../cfg/sourcemod/weapon_prefs.txt");
    
    
KeyValues kv = new KeyValues("Weapon Prefs");
    
    if(
kv.JumpToKey("Assault"true))
    {
        for(
int isizeof(sWeaponAssault); i++)
        {
            
PrintToServer("Weapon Name %s"sWeaponAssault[i]);
            
kv.JumpToKey(sWeaponAssault[i], true);
            
kv.GoBack();
        }
    }
    
kv.ExportToFile(kv_Path);

This gives me back just the second last array value.

PHP Code:
"rifle_sg552"
{

I know, I need to close the handle, but this need to be fixed before hand. Who can help me?
__________________

Last edited by Spirit_12; 05-27-2018 at 20:42.
Spirit_12 is offline
Drixevel
AlliedModders Donor
Join Date: Sep 2009
Location: Somewhere headbangin'
Old 05-27-2018 , 18:31   Re: KeyValues kv.GoBack
Reply With Quote #2

Code:
void WeaponNamesToFile()
{
	// Build file path.
	BuildPath(Path_SM, kv_Path, sizeof(kv_Path), "../../cfg/sourcemod/weapon_prefs.txt");
	
	KeyValues kv = new KeyValues("Weapon Prefs");
	
	if(kv.JumpToKey("Assault", true))
	{
		for (int i = 0; i < sizeof(sWeaponAssault); i++)
		{
			PrintToServer("Weapon Name %s", sWeaponAssault[i]);
			
			if (kv.JumpToKey(sWeaponAssault[i], true))
			{
				kv.SetString("key", "value");
				kv.GoBack();
			}
		}
		
		kv.Rewind();
	}
	
	kv.ExportToFile(kv_Path);
	delete kv;
}
3 primary issues you ran into:
  1. Sourcemod skips sections that have no data when saving. (There maybe another method of creating sections that works?)
  2. You have to rewind the KeyValues handle before you export it if you've changed it.
  3. Be sure to check if JumpToKey returns true before you GoBack.

Code:
"Weapon Prefs"
{
	"Assault"
	{
		"rifle"
		{
			"key"		"value"
		}
		"rifle_desert"
		{
			"key"		"value"
		}
		"rifle_ak47"
		{
			"key"		"value"
		}
		"rifle_sg552"
		{
			"key"		"value"
		}
		"rifle_m60"
		{
			"key"		"value"
		}
	}
}

Last edited by Drixevel; 05-27-2018 at 18:34.
Drixevel is offline
Spirit_12
Veteran Member
Join Date: Dec 2012
Location: Toronto, CA
Old 05-27-2018 , 20:41   Re: KeyValues kv.GoBack
Reply With Quote #3

Your answer was quick and to the point. Have this
__________________
Spirit_12 is offline
Reply


Thread Tools
Display Modes

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 22:14.


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