Passing optional function parameters to another one
#1

For example, there is function CallLocalFunction(const function[], const format[], {Float,_}:...), I want to create a function with optional parameters, add prefix to first named variable, and pass them to CallLocalFunction. Let it be - MyFunction(const function[], const format[], {Float,_}:...). At first, it looks like:
Код:
MyFunction(const function[], const format[], {Float,_}:...)
{
	new func_name[128];
	format(func_name, sizeof(func_name), "%s_%s", "prefix", function);
	return CallLocalFunction(func_name, format, {Float,_}:...);
}
but it's not right (doesn't compile) - it's not possible to pass the optional parameters directly. I have read this topic
http://forum.sa-mp.com/showpost.php?...&postcount=150 - but, have no idea how to pass variables. Can some body give the right code?
Reply
#2

you can't name things that already exist like function or format, use different names
Reply
#3

Quote:
Originally Posted by cessil
Посмотреть сообщение
you can't name things that already exist like function or format, use different names
I think, you didn't understand me. My objective is not to name 2 things equally. I have 2 different functions with optional parameters (for example Func1 and Func2), I want to know, how to pass THIS OPTIONAL PARAMETERS from Func1 to Func2? Let it be some optional variables in Func1 ( Func1(playerid, format[], {Float,_}:...) ),Func2 will be called from Func1. Then I need to pass this Func1 optional variables ( {Float,_}:... ) to Func2 - how to do this?
Reply
#4

Any idea?
Reply
#5

Unfortunately, I'm not good at AMX assembly. Could you please write the piece of code for me?
The incorrect implementation:
Код:
MyFunction(const function[], const format[], {Float,_}:...)
{
	new func_name[128];
	format(func_name, sizeof(func_name), "%s_%s", "prefix", function);
	return CallLocalFunction(func_name, format, {Float,_}:...);
}
Reply
#6

Quote:
Originally Posted by ******
Посмотреть сообщение
Look up y_va - it wraps these functions so you don't need to use any assembly at all.
The trouble is not in passing optional parameters to format function, I need to pass this optional parameters (unknown number of parameters) to CallLocalFunction. So:
Код:
MyFunction(name[], vartypes[], OPTIONAL_PARAMETERS...........)
{
    CallLocalFunction(name, vartypes, OPTIONAL_PARAMETERS...........);
}
Do you realize what I want to implement?
Reply
#7

Again, y_va:
pawn Код:
MyFunction(name[], vartypes[], va_args<>)
{
    CallLocalFunction(name, vartypes, va_start<2>);
}
Reply
#8

I can't to send the optional parameters that way.

Quote:
Originally Posted by ******
Посмотреть сообщение
Actually it would be va_CallLocalFunction, but other than that yes.
Can you tell me more about that?

My code:
Код:
stock RunModules(const function_name[], const format[], va_args<>)
{
	new module_count, func_name[128];
	
	module_count = sizeof(modules);
	for(new i = 0; i < module_count; i++)
	{
		strdel(func_name, 0, sizeof(func_name)-1);
		strcat(func_name, modules[i]);
		strcat(func_name, "_");
		strcat(func_name, function_name);
		//format(func_name, sizeof(func_name), "%s_%s", modules[i], function_name);

		if((funcidx(func_name)) != -1)  va_CallLocalFunction(func_name, format, va_start<2>);
	}
	return 0;
}
Could you please write va_CallLocalFunction for me?
Reply
#9

Hmm.. Found va_CallLocalFunction in https://raw.github.com/Y-Less/YSI/ma...e/YSI/y_va.inc . But there was no such function in my y_va.inc. Now, the code works.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)