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

GetCmdArg() in a variable or function


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Loll10T
Member
Join Date: Jul 2022
Old 07-17-2023 , 20:24   GetCmdArg() in a variable or function
Reply With Quote #1

I want to use the input of a convar in order to run a function, but I can't seem to understand how to do it with GetCmdArg().
I've tried putting it inside of the function, but I get the error 34 (argument does not have a default value (argument 1)).
Code:
public Action ParseName(int client, int args)
{
	char filename[64];
	Function(GetCmdArg(1, filename, sizeof(filename)););
}
I have no idea on how to use GetCmdArg() inside of a char variable.

Last edited by Loll10T; 07-17-2023 at 20:24.
Loll10T is offline
little_froy
Senior Member
Join Date: May 2021
Old 07-17-2023 , 21:19   Re: GetCmdArg() in a variable or function
Reply With Quote #2

what's the "function prototype" of Function()?
little_froy is online now
Loll10T
Member
Join Date: Jul 2022
Old 07-21-2023 , 22:27   Re: GetCmdArg() in a variable or function
Reply With Quote #3

Code:
void LoadConfig(char path[70])
{
//code here
}
I can post the entire code, it's Orinuse's Reserve Control plugin.
What I intend to do is rewrite the code so instead of reloading one specific cfg file, it loads a file based on your input. Since I have no idea how SourcePawn works (and I've read the wiki, and still can't figure out how it works lol), I'm a little bit stuck :b
Loll10T is offline
Grey83
Veteran Member
Join Date: Dec 2014
Location: Ukraine
Old 07-22-2023 , 03:12   Re: GetCmdArg() in a variable or function
Reply With Quote #4

PHP Code:
public Action ParseName(int clientint args)
{
    
char filename[64];
    
GetCmdArg(1filenamesizeof(filename));
    Function(
filename);

https://sm.alliedmods.net/new-api/console/GetCmdArg
Attached Thumbnails
Click image for larger version

Name:	изображение_2023-07-22_1011409.png
Views:	53
Size:	59.9 KB
ID:	201188  
__________________
Grey83 is offline
Loll10T
Member
Join Date: Jul 2022
Old 07-23-2023 , 15:47   Re: GetCmdArg() in a variable or function
Reply With Quote #5

Oh, just found out I couldn't compile it because the array size of filename did not match with path's array size.
Thank you!

Now, I have a new question: How do I send this info to console?
Code:
SetFailState("CONFIG ERROR ID: #%d, %s. (line %d, column %d) [FILE: %s]", result, sError, iLine, iCol, sPath);
I know I can just PrintToConsole or Reply, but look at this:
Code:
// ------------
// SMCParser
// Code is *very* based of: 'l4d_info_editor.sp'
// Step 1: Parse filename from user input, in order to use it in LoadConfigSMC.
public Action ParseConfigSMC(int client, int args)
{
	char filename[70];
	GetCmdArg(1, filename, sizeof(filename));
	LoadConfigSMC(filename);
	return Plugin_Handled;
}
// Step 2: Attempt to load the filename obtained from ParseConfigSMC; if the file is not found,
// then try to load the default file you download from the plugin's site ("l4d_reservecontrol.cfg").
// Otherwise, the plugin fails, resulting in its abortion.
void LoadConfigSMC(char path[70])
{
	//g_smReserveData.Clear();
	char sPath[PLATFORM_MAX_PATH];
	BuildPath(Path_SM, sPath, sizeof(sPath), "data/%s.cfg", path);

	if( FileExists(sPath) )
	{
		SMCParser parser = new SMCParser();
		parser.OnKeyValue = SMC_OnKeyValue;

		// Setup error logging
		char sError[128];
		int iLine, iCol;
		SMCError result = parser.ParseFile(sPath, iLine, iCol);
		if( result != SMCError_Okay )
		{
			BuildPath(Path_SM, sPath, sizeof(sPath), "data/l4d_reservecontrol.cfg");
			if( parser.GetErrorString(result, sError, sizeof(sError)) )
			{
				SetFailState("CONFIG ERROR ID: #%d, %s. (line %d, column %d) [FILE: %s]", result, sError, iLine, iCol, sPath);
				// I want the plugin to keep running, rather than being aborted. Maybe log the error or print it to console.
			}
			else
			{
				SetFailState("Unable to load config. Bad format? Check for missing { } etc.");
				// Same here.
			}
		}

		delete parser;
		return;
	}
	SetFailState("Could not load CFG '%s'! Plugin aborted.", sPath); // Same here as well.
}
I cannot PrintToConsole because I need a client ID, which is not fetched by LoadConfigSMC.

I could send the entire code, but that's pretty much what I've changed. I commented CmdReserveReload (used when reloading the cfg file), since I wasn't able to get it working properly (due to g_smReserveData.Clear() having a lot of errors that could overload the server). I might investigate a way on saving the parsed filename on memory, that way maybe I can get it working again.

Last edited by Loll10T; 07-23-2023 at 16:19. Reason: i have another question; code correction
Loll10T is offline
headline
SourceMod Moderator
Join Date: Mar 2015
Old 08-04-2023 , 13:57   Re: GetCmdArg() in a variable or function
Reply With Quote #6

Quote:
Originally Posted by Loll10T View Post
Oh, just found out I couldn't compile it because the array size of filename did not match with path's array size.
Thank you!

Now, I have a new question: How do I send this info to console?
Code:
SetFailState("CONFIG ERROR ID: #%d, %s. (line %d, column %d) [FILE: %s]", result, sError, iLine, iCol, sPath);
I know I can just PrintToConsole or Reply, but look at this:
Code:
// ------------
// SMCParser
// Code is *very* based of: 'l4d_info_editor.sp'
// Step 1: Parse filename from user input, in order to use it in LoadConfigSMC.
public Action ParseConfigSMC(int client, int args)
{
	char filename[70];
	GetCmdArg(1, filename, sizeof(filename));
	LoadConfigSMC(filename);
	return Plugin_Handled;
}
// Step 2: Attempt to load the filename obtained from ParseConfigSMC; if the file is not found,
// then try to load the default file you download from the plugin's site ("l4d_reservecontrol.cfg").
// Otherwise, the plugin fails, resulting in its abortion.
void LoadConfigSMC(char path[70])
{
	//g_smReserveData.Clear();
	char sPath[PLATFORM_MAX_PATH];
	BuildPath(Path_SM, sPath, sizeof(sPath), "data/%s.cfg", path);

	if( FileExists(sPath) )
	{
		SMCParser parser = new SMCParser();
		parser.OnKeyValue = SMC_OnKeyValue;

		// Setup error logging
		char sError[128];
		int iLine, iCol;
		SMCError result = parser.ParseFile(sPath, iLine, iCol);
		if( result != SMCError_Okay )
		{
			BuildPath(Path_SM, sPath, sizeof(sPath), "data/l4d_reservecontrol.cfg");
			if( parser.GetErrorString(result, sError, sizeof(sError)) )
			{
				SetFailState("CONFIG ERROR ID: #%d, %s. (line %d, column %d) [FILE: %s]", result, sError, iLine, iCol, sPath);
				// I want the plugin to keep running, rather than being aborted. Maybe log the error or print it to console.
			}
			else
			{
				SetFailState("Unable to load config. Bad format? Check for missing { } etc.");
				// Same here.
			}
		}

		delete parser;
		return;
	}
	SetFailState("Could not load CFG '%s'! Plugin aborted.", sPath); // Same here as well.
}
I cannot PrintToConsole because I need a client ID, which is not fetched by LoadConfigSMC.

I could send the entire code, but that's pretty much what I've changed. I commented CmdReserveReload (used when reloading the cfg file), since I wasn't able to get it working properly (due to g_smReserveData.Clear() having a lot of errors that could overload the server). I might investigate a way on saving the parsed filename on memory, that way maybe I can get it working again.
The call to SetFailState will go to the server's logs, which is where I'd expect them.

Of course, if you want it in console, you'll need PrintToConsoleAll
headline 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 01:48.


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