Raised This Month: $63 Target: $400
 15% 

Call a Function from itself?!?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
DJPlaya
Senior Member
Join Date: Nov 2014
Location: Germany
Old 10-14-2019 , 16:40   Call a Function from itself?!?
Reply With Quote #1

For a Project ime currently working on, i would like to call a Function from itself (if thats possible).
I know it sounds dump, but its the most efficient and compact way todo what i want.
The idea is to bring multiply Features into one big Function so i dont have to write a Function for every Feature (especially if they only require 1-3 Lines of Code).

This is an Example of how i thought it could work, all Vars only get created when called from Outside. This is identified by iAction which carries a positive Value when beeing called from Outside and a negative Value when beeing called from Inside.
Sadly this dosent work, it seems the Compiler dosent recognize this Mechanism and spits 'undefined symbol' Errors.
Code:
#include <smlib>

public void OnPluginStart()
{
	Some_Action(1, 1, "test");
}

Some_Action(iClient, iAction, const char[] cReason, any ...)
{
	if(iAction == 0) // 0 - Do nothing
		return;
		
	if(iAction > 0) // This is a Check, negative Values mean the Function is re-run
	{
		bool bTranslated; // Its okay to leave the old Values of bTranslated and cReason, we are just not allowed to change them
	}
	
	if(!StrContains(cReason, "TRANS_"))
	{
		if(iAction > 0) // This is a Check, negative Values mean the Function is re-run // Its okay to leave the old Values of bTranslated and cReason, we are just not allowed to change them
		{
			char cReason2[256];
			VFormat(cReason2, sizeof(cReason2), cReason, 4);
		}
	}
	
	else // Is Translated
	{
		bTranslated = true;
		
		if(iAction > 0) // This is a Check, negative Values mean the Function is re-run // Its okay to leave the old Values of bTranslated and cReason, we are just not allowed to change them
			Translate(cReason, cReason2, 256);
	}
	
	if(iAction < 0) // This is a Check, negative Values mean the Function is re-run
		iAction = Math_Abs(iAction);
		
		
	if(iAction == 1)
	{
		if(!bTranslated)
			PrintToServer("test");
			
		else
			Some_Action(iClient, -2, cReason2);
	}
	
	if(iAction == 2)
		PrintToServer("test translated");
}

Translate(char[] cTranslation, char[] cDestination, iMaxlenght)
{
	// Placeholder
	strcopy(cTranslation, iMaxlenght, cDestination);
}
Someone may have an Idea how todo this? Is there maybe an forgotten Compiler Option i dont know of like 'pragma unused'?
__________________
Free hosting 4 interesting Projects
DJPlaya is offline
Send a message via Skype™ to DJPlaya
Silvers
AlliedModders Donor
Join Date: Aug 2010
Location: SpaceX
Old 10-14-2019 , 17:00   Re: Call a Function from itself?!?
Reply With Quote #2

You could use "static bool bTranslated" at the top inside the function, and only reset to default value when called from outside the function.
__________________
Silvers is offline
DJPlaya
Senior Member
Join Date: Nov 2014
Location: Germany
Old 10-15-2019 , 07:12   Re: Call a Function from itself?!?
Reply With Quote #3

Quote:
Originally Posted by Silvers View Post
You could use "static bool bTranslated" at the top inside the function, and only reset to default value when called from outside the function.
That would work, but then i would also have to move 'cReason2' to the top, even doe its not an valid Input
__________________
Free hosting 4 interesting Projects
DJPlaya is offline
Send a message via Skype™ to DJPlaya
Dragokas
Veteran Member
Join Date: Nov 2017
Location: Ukraine
Old 10-16-2019 , 17:57   Re: Call a Function from itself?!?
Reply With Quote #4

Just be careful, sourcemod has too limited stack for recursion.
__________________
Expert of CMD/VBS/VB6. Malware analyst.
[My plugins] [My tools] [GitHub]
Dragokas 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 15:32.


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