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

is this broken


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
sdz
Senior Member
Join Date: Feb 2012
Old 06-24-2018 , 16:22   is this broken
Reply With Quote #1

i dont think this is how this is supposed to work but

writing stuff to keyvalues file
kv is global variable
.importedfromfile every time it needs to be accessed
making command to rewrite some values
how it should look
Code:
"Data"
{
	"Trail Indices"
	{
		"1"			"Generic Laser"
	}

	"Trail"
	{
		"Generic Laser"
		{
			"Material"			"materials/sprites/laser.vmt"
		}
	}

	"Hat Indices"
	{
		"1"			"Watermelon"
		"2"			"TV"
		"3"			"Cone"
	}

	"Hats"
	{
		"Watermelon"
		{
			"Model"			"models/props_junk/watermelon01.mdl"
			"Offset"		"eyes"
			"Scale"			"1.0"
		}

		"TV"
		{
			"Model"			"models/props_c17/tv_monitor01.mdl"
			"Offset"		"0 0 0"
			"Scale"			"1.0"
		}

		"Cone"
		{
			"Model"			"models/props_junk/trafficcone001a.mdl"
			"Offset"		"0 0 8"
			"Scale"			"1.0"
		}
	}
}
what this snippet should be doing is editing a value in the cone section of hats, from 0 0 8 to 0 0 20 (or any string inside hats/trails section)

PHP Code:
public Action Command_EditHatProperty(int clientint args)
{
    if(
args 3)
    {
        
CReplyToCommand(client"{dodgerblue}[RP]{default} Invalid Syntax: sm_edithatproperty <hat> <property> <value>");
        return 
Plugin_Handled;
    }

    
char arg[32]; GetCmdArg(1argsizeof(arg));

    
g_ConfigKV.ImportFromFile(g_ConfigPath);
    
g_ConfigKV.JumpToKey("Hats");

    
//If it failed, the hat doesn't likely exist:
    
if(!g_ConfigKV.JumpToKey(arg))
    {
        
CReplyToCommand(client"{dodgerblue}[RP]{default} Invalid Hat: {dodgerblue}%s{default}."arg);
        return 
Plugin_Handled;
    }

    
char prop[32]; GetCmdArg(2propsizeof(prop));
    
char nullCheck[16]; g_ConfigKV.GetString(propnullChecksizeof(nullCheck), "NULL");

    
//Check if our attribute exists:
    
if(StrEqual(nullCheck"NULL"false))
    {
        
CReplyToCommand(client"{dodgerblue}[RP]{default} Invalid Attribute: {dodgerblue}%s{default}."prop);
        return 
Plugin_Handled;
    }

    
char val[64]; GetCmdArg(3valsizeof(val));
    
g_ConfigKV.SetString(propval);
    
CReplyToCommand(client"{dodgerblue}[RP]{default} Set Attribute {dodgerblue}%s{default} of {dodgerblue}%s{default} to: {dodgerblue}%s{default}."propargval);

    
//Save:
    
g_ConfigKV.Rewind();
    
g_ConfigKV.ExportToFile(g_ConfigPath);
    return 
Plugin_Handled;

what it is doing:
Code:
"data"
{
	"Trail Indices"
	{
		"1"		"Generic Laser"
	}
	"Trail"
	{
		"Generic Laser"
		{
			"material"		"materials/sprites/laser.vmt"
		}
	}
	"Hat Indices"
	{
		"1"		"Watermelon"
		"2"		"TV"
		"3"		"Cone"
	}
	"Hats"
	{
		"Watermelon"
		{
			"model"		"models/props_junk/watermelon01.mdl"
			"offset"		"eyes"
			"scale"		"1.000000"
		}
		"TV"
		{
			"model"		"models/props_c17/tv_monitor01.mdl"
			"offset"		"0 0 0"
			"scale"		"1.000000"
		}
		"data"
		{
			"model"		"models/props_junk/trafficcone001a.mdl"
			"offset"		"0 0 8"
			"scale"		"1.000000"
			"Trail Indices"
			{
				"1"		"Generic Laser"
			}
			"Trail"
			{
				"Generic Laser"
				{
					"material"		"materials/sprites/laser.vmt"
				}
			}
			"Hat Indices"
			{
				"1"		"Watermelon"
				"2"		"TV"
				"3"		"Cone"
			}
			"Hats"
			{
				"Watermelon"
				{
					"model"		"models/props_junk/watermelon01.mdl"
					"offset"		"eyes"
					"scale"		"1.000000"
				}
				"TV"
				{
					"model"		"models/props_c17/tv_monitor01.mdl"
					"offset"		"0 0 0"
					"scale"		"1.000000"
				}
				"Cone"
				{
					"model"		"models/props_junk/trafficcone001a.mdl"
					"offset"		"0 0 20"
					"scale"		"1.000000"
				}
			}
		}
	}
}

im not going crazy, right? is this just inherently going to happen with a globally created KeyValues object being edited and something not lining up or...?

Last edited by sdz; 06-24-2018 at 16:32.
sdz is offline
DJ Tsunami
DJ Post Spammer
Join Date: Feb 2008
Location: The Netherlands
Old 06-24-2018 , 16:52   Re: is this broken
Reply With Quote #2

It simply writes two tabs between the key and value. So if you have keys with different lengths, the values won't necessarily line up.
__________________
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
sdz
Senior Member
Join Date: Feb 2012
Old 06-24-2018 , 17:25   Re: is this broken
Reply With Quote #3

Quote:
Originally Posted by DJ Tsunami View Post
It simply writes two tabs between the key and value. So if you have keys with different lengths, the values won't necessarily line up.
unless im misunderstanding I think you missed what I was pointing out between the two keyvalues files

if you compared them side by side, you'll notice in the hats section, the section cone is replaced entirely with the whole keyvalues structure starting from "Data" making the entire thing nearly 2x in size
https://www.diffchecker.com/x4M1xAHJ

Last edited by sdz; 06-24-2018 at 17:27.
sdz is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 06-24-2018 , 17:39   Re: is this broken
Reply With Quote #4

ImportFromFile doesn't clear the existing KV structure in memory before the import takes place. If the current position in the tree is inside of "Hats" then the ImportFromFile will place everything it imports within "Hats."
__________________
ddhoward is offline
sdz
Senior Member
Join Date: Feb 2012
Old 06-24-2018 , 18:13   Re: is this broken
Reply With Quote #5

so what i'm understanding as well as gathering from testing is that I need to delete the file, then export, or else it just kinda repastes the entirely kv into itself..

please correct me if there's a better method of going about that that doesn't include using multiple files

Last edited by sdz; 06-24-2018 at 18:14.
sdz is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 06-24-2018 , 18:16   Re: is this broken
Reply With Quote #6

No, you don't need to delete the file. This has nothing to do with the file.

You need to clear the KV structure that is in memory.

PHP Code:
delete g_ConfigKV;
g_ConfigKV = new KeyValues("Data"); 
__________________
ddhoward is offline
sdz
Senior Member
Join Date: Feb 2012
Old 06-24-2018 , 18:19   Re: is this broken
Reply With Quote #7

riiiight always something simple
thanks

mightve helped if i actually read the post instead of skimming it over as well

Last edited by sdz; 06-24-2018 at 18:23.
sdz 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 03:02.


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