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

Solved KeyValues 'do' Loop Looping Twice?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
ThatKidWhoGames
Veteran Member
Join Date: Jun 2013
Location: IsValidClient()
Old 05-28-2020 , 13:28   KeyValues 'do' Loop Looping Twice?
Reply With Quote #1

Hello,

So for some very odd reason, it seems as though when I am parsing through KeyValues, the do loop loops twice when I have generated the text file that I want the KeyValues to use.

Here is my code so you can better understand what I mean:
PHP Code:
#include <sourcemod>

public void OnPluginStart()
{
    
BrowseKeyValues();
}

void BrowseKeyValues()
{
    
KeyValues kv = new KeyValues("KV Test");

    
char config[256];
    
BuildPath(Path_SMconfigsizeof(config), "configs/kvtest.cfg");
    if (!
FileExists(config))
    {
        
kv.JumpToKey("Section One"true);
        
kv.SetString("key""value1");
        
kv.Rewind();
        
kv.JumpToKey("Section Two"true);
        
kv.SetString("key""value2");
        
kv.Rewind();
        
kv.ExportToFile(config);

        
LogMessage("Generated the config file %s!"config);
    }

    if (!
kv.ImportFromFile(config) || !kv.GotoFirstSubKey())
    {
        
delete kv;
        
SetFailState("Failure parsing the config file %s!");
    }

    
char section[32], value[32];
    do {
        
kv.GetSectionName(sectionsizeof(section));
        
kv.GetString("key"valuesizeof(value));

        
LogMessage("KV Test: (Section: %s) (Key: %s)"sectionvalue);
    } while 
kv.GotoNextKey();

    
delete kv;
}

/*
  Config file contents:
    "KV Test"
    {
        "Section One"
        {
            "key"        "value1"
        }
        "Section Two"
        {
            "key"        "value2"
        }
    }

//////////////////////////////////////////////////////////////////////////////////////////////////////
  Loading the plugin with creating the config file:
    sm plugins load test
    L 05/28/2020 - 13:18:07: [test.smx] Generated the config file addons/sourcemod/configs/kvtest.cfg!
    L 05/28/2020 - 13:18:07: [test.smx] KV Test: (Section: Section One) (Key: value1)
    L 05/28/2020 - 13:18:07: [test.smx] KV Test: (Section: Section Two) (Key: value2)
    L 05/28/2020 - 13:18:07: [test.smx] KV Test: (Section: Section One) (Key: value1)
    L 05/28/2020 - 13:18:07: [test.smx] KV Test: (Section: Section Two) (Key: value2)
    [SM] Loaded plugin test.smx successfully.

//////////////////////////////////////////////////////////////////////////////////////////////////////
  Loading the plugin without creating the config file:
    sm plugins load test
    L 05/28/2020 - 13:19:49: [test.smx] KV Test: (Section: Section One) (Key: value1)
    L 05/28/2020 - 13:19:49: [test.smx] KV Test: (Section: Section Two) (Key: value2)
    [SM] Loaded plugin test.smx successfully.
*/ 
Config file contents:
Code:
"KV Test"
{
	"Section One"
	{
		"key"		"value1"
	}
	"Section Two"
	{
		"key"		"value2"
	}
}
Output loading the plugin with creating the config file:
Code:
sm plugins load test
L 05/28/2020 - 13:18:07: [test.smx] Generated the config file addons/sourcemod/configs/kvtest.cfg!
L 05/28/2020 - 13:18:07: [test.smx] KV Test: (Section: Section One) (Key: value1)
L 05/28/2020 - 13:18:07: [test.smx] KV Test: (Section: Section Two) (Key: value2)
L 05/28/2020 - 13:18:07: [test.smx] KV Test: (Section: Section One) (Key: value1)
L 05/28/2020 - 13:18:07: [test.smx] KV Test: (Section: Section Two) (Key: value2)
[SM] Loaded plugin test.smx successfully.
Output when loading the plugin without creating the config file:
Code:
sm plugins load test
L 05/28/2020 - 13:19:49: [test.smx] KV Test: (Section: Section One) (Key: value1)
L 05/28/2020 - 13:19:49: [test.smx] KV Test: (Section: Section Two) (Key: value2)
[SM] Loaded plugin test.smx successfully.
Any advice would be greatly appreciated!

Thank you!

Last edited by ThatKidWhoGames; 05-28-2020 at 14:07.
ThatKidWhoGames is offline
DJ Tsunami
DJ Post Spammer
Join Date: Feb 2008
Location: The Netherlands
Old 05-28-2020 , 13:53   Re: KeyValues 'do' Loop Looping Twice?
Reply With Quote #2

You are creating the file, and then importing the same file into the KeyValues structure again, creating a duplicate.

Quote:
KeyValues.ImportFromFile Method

Imports a file in KeyValues format. The file is read into the current position of the tree.
__________________
Advertisements | REST in Pawn - HTTP client for JSON REST APIs
Please do not PM me with questions. Post in the plugin thread.
DJ Tsunami is offline
ThatKidWhoGames
Veteran Member
Join Date: Jun 2013
Location: IsValidClient()
Old 05-28-2020 , 14:07   Re: KeyValues 'do' Loop Looping Twice?
Reply With Quote #3

Quote:
Originally Posted by DJ Tsunami View Post
You are creating the file, and then importing the same file into the KeyValues structure again, creating a duplicate.
That's it, thank you so much!
ThatKidWhoGames 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 12:30.


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