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

Making use of a text-file in Sourcemod


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
wauterboi
Junior Member
Join Date: Dec 2013
Old 02-13-2014 , 17:48   Making use of a text-file in Sourcemod
Reply With Quote #1

Hey everyone - I'm very new to Sourcemod. I've been able to work my way around with my knowledge of Lua, the API page, and posts here on the forums! But now I'm trying to kick it up a notch with an idea I have.

I want to be able to rotate classes, weapons, perks, and what not based on a text file, organized something along these lines:

Code:
"1"
{

	"description"	"Crits Only Soldiers!"
	"class"		"soldier"
	"perks"		"addconds go here"
	"weapons"	"weapons go here"

}

"2"
{

	"description"	"Demoknights Go!"
	"class"		"demoman"
	"perks"		"addconds go here"
	"weapons"	"weapons go here"

}
I'm not sure how I would do this. I'm aware of ReadFile, OpenFile, and what not, but I've yet to see someone explain how to go this far with organizing things into an array.

The goal is to be able to refer to the different variables of each number and get the various strings associated.

Any help would be awesome!
wauterboi is offline
bl4nk
SourceMod Developer
Join Date: Jul 2007
Old 02-13-2014 , 17:53   Re: Making use of a text-file in Sourcemod
Reply With Quote #2

https://wiki.alliedmods.net/KeyValue...d_Scripting%29
bl4nk is offline
wauterboi
Junior Member
Join Date: Dec 2013
Old 02-13-2014 , 20:27   Re: Making use of a text-file in Sourcemod
Reply With Quote #3

Thanks bl4nk (and Powerlord)! I'm having a bit of trouble, unfortunately.

I want to be able to grab all the values from the Keyvalues. For instance:

Code:
"howdown"
{

	"critsoldiers"
	{

		"description"	"Crits Only Soldiers!"
		"class"		"Soldier"
		"perks"		"11"
		"weapons"	"18;10;6"

	}

	"demoknights"
	{
	
		"description"	"Demoknights Go!"
		"class"		"Demoman"
		"perks"		"none"
		"weapons"	"608;406;132"

	}

}
I want to be able to grab each of the values as a string with a function. Ideally, it would be as such:

Code:
GetHowdownValue( "critsoldiers", "description" )
// GetHowdownValue( <name>, <value> );
// Would return "Crits Only Soldiers!"
For testing purposes, I've been trying to print the resulting string to chat. Nothing's printing, sadly.

I've been looking at this page, and my tinkering and attempts to understand the code have failed miserably.

Code:
public OnPluginStart( )
{
	GetHowdownValue( "critsoldiers", "description", 255 );
}

bool:GetHowdownValue( const String:RoundName[], String:ReqValue[], maxlength )
{

	new Handle:kv = CreateKeyValues("howdown");
	FileToKeyValues(kv, "cfg/howdown.txt");
	if (!KvJumpToKey(kv, RoundName))
	{
		return false;
	}
	KvGetString(kv, "description", ReqValue, maxlength);
	CPrintToChatAll( "Testing" );
	CloseHandle(kv);
	return true;
	
}
From what I'm understanding, "new Handle:kv = CreateKeyValues("howdown");" creates a new handle to refer back to - a blank palette for keyvalues to be shoved into. "FileToKeyValues(kv, "cfg/howdown.txt");" pours all of the keyvalues from my text file into kv.

The if statement that follows "if (!KvJumpToKey(kv, RoundName))..." determines if what I'm looking for even exists. If it doesn't it returns false and nothing happens.

"KvGetString(kv, "description", ReqValue, maxlength);" gets the string value for description and outputs it right back into ReqValue. "CPrintToChatAll( "Testing" );" is just for me to see that it even worked, and "CloseHandle(kv);" stops browsing the keyvalues entirely. "return true;" to indicate everything worked out fine.

Could someone help me out here? I know how I'd do this with Lua, but not with Sourcepawn, and it's like trying to speak a language to someone who speaks a different language. Bah.
wauterboi is offline
wauterboi
Junior Member
Join Date: Dec 2013
Old 02-13-2014 , 22:52   Re: Making use of a text-file in Sourcemod
Reply With Quote #4

I've realized that it's not reading from the file correctly for some reason...

"howdown.txt" is now "howdown.ini" in the sourcemod/configs folder. The file path is correct. (I can print the filepath just fine to chat.)

Code:
"howdown"
{
	"critsoldiers"
	{

		"description"	"Crits Only Soldiers!"
		"class"		"Soldier"
		"perks"		"11"
		"weapons"	"18;10;6"
	}

	"demoknights"
	{
	
		"description"	"Demoknights Go!"
		"class"		"Demoman"
		"perks"		"none"
		"weapons"	"608;406;132"
	}
}
Code:
public OnPluginStart( )
{
	GetHowdownValue( "critsoldiers", "description" );
}

public GetHowdownValue( const String:RoundName[], String:ReqValue[] )
{

	decl String:RecValue[32];
	
	new Handle:kv = CreateKeyValues( "howdown" );
	
	decl String:hdBuildPath[PLATFORM_MAX_PATH];
	BuildPath( Path_SM, hdBuildPath, PLATFORM_MAX_PATH, "configs/howdown.ini" );
	FileToKeyValues( kv, "hdBuildPath" );
	
	if ( KvGotoFirstSubKey( kv ) )
	{
	
		do
		{
		
			KvGetString( kv, "description", ReqValue, sizeof(RecValue) );
			CPrintToChatAll( "%s This should work...", RecValue );
			
		} while ( KvGotoNextKey( kv ) );
		
	}
	
	else
	
	{
	
		CPrintToChatAll( "!" );
		
	}
	
	CloseHandle( kv );
	
}
wauterboi is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 02-13-2014 , 23:41   Re: Making use of a text-file in Sourcemod
Reply With Quote #5

Code:
FileToKeyValues( kv, "hdBuildPath" );
remove the quotes around hdBuildPath.
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
wauterboi
Junior Member
Join Date: Dec 2013
Old 02-14-2014 , 00:17   Re: Making use of a text-file in Sourcemod
Reply With Quote #6

Whoops, I'm dumb. Now it's spurting out:

Code:
on This should work...
It should be printing, "Crits Only Soldiers! This should work..."

Last edited by wauterboi; 02-14-2014 at 00:18.
wauterboi is offline
11530
Veteran Member
Join Date: Sep 2011
Location: Underworld
Old 02-14-2014 , 15:29   Re: Making use of a text-file in Sourcemod
Reply With Quote #7

Change:

PHP Code:
KvGetString(kv"description"ReqValuesizeof(RecValue) );
//to
KvGetString(kvReqValueRecValuesizeof(RecValue) ); 
__________________
11530 is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 02-14-2014 , 15:51   Re: Making use of a text-file in Sourcemod
Reply With Quote #8

Quote:
Originally Posted by 11530 View Post
Change:

PHP Code:
KvGetString(kv"description"ReqValuesizeof(RecValue) );
//to
KvGetString(kvReqValueRecValuesizeof(RecValue) ); 
More like
PHP Code:
KvGetString(kv"description"RecValuesizeof(RecValue) ); 
since we don't know what's in ReqValue.
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
11530
Veteran Member
Join Date: Sep 2011
Location: Underworld
Old 02-14-2014 , 16:49   Re: Making use of a text-file in Sourcemod
Reply With Quote #9

Quote:
Originally Posted by Powerlord View Post
More like
PHP Code:
KvGetString(kv"description"RecValuesizeof(RecValue) ); 
since we don't know what's in ReqValue.
He's passing it as an argument of the GetHowdownValue function, sent from OnPluginStart.
__________________
11530 is offline
wauterboi
Junior Member
Join Date: Dec 2013
Old 02-14-2014 , 20:28   Re: Making use of a text-file in Sourcemod
Reply With Quote #10

I've got it to work just fine by replacing with the variable name as suggested by 11530, but now comes the problem of trying to get the function to return a string (which is an array). That can't happen, apparently.

Code:
#include <sourcemod>
#include <sdktools>
#include <tf2>
#include <tf2_stocks>
#include <sdkhooks>
#include <morecolors>
#include <smlib>

#define CHAT_PREFIX "{orange}[Howdown]{default}:"

public Plugin:myinfo =
{
	name	 	= "Hostage Howdown",
	author 		= "wauterboi",
	description = "Every thirty seconds your concept of reality is destroyed.",
	version 	= "Egg.",
	url 		= "http://www.dotbsp.com/"
};

public OnPluginStart( )
{
	CPrintToChatAll( "%s", GetHowdownValue( "critsoldiers", "description" ) )
	CPrintToChatAll( "%s", GetHowdownValue( "demoknights", "description" ) )
}

bool:GetHowdownValue( const String:RoundName[], String:ReqValue[] )
{

	decl String:RecValue[32];
	
	new Handle:kv = CreateKeyValues( "howdown" );
	
	decl String:hdBuildPath[PLATFORM_MAX_PATH];
	BuildPath( Path_SM, hdBuildPath, PLATFORM_MAX_PATH, "configs/howdown.ini" );
	FileToKeyValues( kv, hdBuildPath );
	
	if ( KvJumpToKey( kv, RoundName ) )
	{
	
		do
		{
		
			KvGetString(kv, ReqValue, RecValue, sizeof(RecValue) );  
			
		} while ( KvGotoNextKey( kv ) );
	
		CloseHandle( kv );
		return true;
	}
	
	else
	
	{
	
		CPrintToChatAll( "!" );
		CloseHandle( kv );
		return false;
		
	}
	
}
For right now, I've changed it to a bool function and it doesn't print anything (I'm just returning true or false) and I've been looking at all kinds of stuff trying to figure out how to make this work. Unfortunately, I can't grasp what's going on with the forum posts recommendations of string copying and pasting to the original variables of the function.

Last edited by wauterboi; 02-14-2014 at 20:29.
wauterboi 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:36.


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