@bigalgator,
It is very easy setting default parameters for a function.
Let’s take the MACD example of the ADK Sample project.
It looks like this
// ExampleMACD()
// This function demonstrates
// how to call-back internal AFL function
AmiVar VExampleMACD( int NumArgs, AmiVar *ArgsTable )
{
// First you need to fill-in
// arguments table.
//
// we will call back MACD that gets two float
// arguments:
AmiVar arg[ 2 ];
arg[ 0 ].type = VAR_FLOAT;
arg[ 0 ].val = 12;
arg[ 1 ].type = VAR_FLOAT;
arg[ 1 ].val = 26;
// Now you can call internal AFL function
// using CallFunction callback pointer of the site interface
// Please note that NO ERROR checking is done
// on number/types of arguments passed
// Specifying wrong args here ends with access violation in most cases
AmiVar result = gSite.CallFunction( "macd", 2, arg );
return result;
}
And the entry of that function in the function table of the same Sample project looks like this
"ExampleMACD", { VExampleMACD, 0, 0, 0, 0, NULL },
So in simple words,
the string “ExampleMACD” is your AFL function’s name,
VExampleMACD is the DLL function’s name
1st zero is your number of array arguments
2nd zero is the number of string arguments
3rd zero is the number of float arguments
4th zero is number of defaults
and the NULL is indicating that no defaults are defined.
You have all zero here because the function has two fix variables set within the function. So in function table above no number of arguments need to be set. That’s why all zero.
Now next (modified) case would be using no fix variables but passing some arguments from AFL to your function. In that case the simplest form would look like this modification of the above example
// ExampleMACD()
// This function demonstrates
// how to call-back internal AFL function
AmiVar VExampleMACD( int NumArgs, AmiVar *ArgsTable )
{
// First you need to fill-in
// arguments table.
//
// we will call back MACD that gets two float
// arguments:
AmiVar arg[ 2 ];// two arguments
arg[ 0 ].type = VAR_FLOAT;
arg[ 0 ].val = ArgsTable[ 0 ].val;// your 1st inserted parameter of ExampleMACD(arg1, arg2)
arg[ 1 ].type = VAR_FLOAT;
arg[ 1 ].val = ArgsTable[ 1 ].val;// your 2nd inserted parameter of ExampleMACD(arg1, arg2)
// Now you can call internal AFL function
// using CallFunction callback pointer of the site interface
// Please note that NO ERROR checking is done
// on number/types of arguments passed
// Specifying wrong args here ends with access violation in most cases
AmiVar result = gSite.CallFunction( "macd", 2, arg );
return result;
}
The function table would look like this now
"ExampleMACD", { VExampleMACD, 0, 0, 2, 0, NULL },
Why “2”? Because inbuilt MACD function that we call via gSite.CallFunction expects two arguments of type number so we set “2” as number of floats at 3rd place after VExampleMACD. Still no default values here. That’s why still “0, NULL”
In the next (final) case of this post we add some default values to be used if nothing is passed.
For this case we only need to change the function table of the 2nd example’s function of this post and define two default values.
float MACD_defaults[] = {12.0f, 26.0f};// if no arguments are inserted in AFL function MACD() then these default values will be used for calculation
FunctionTag gFunctionTable[] = { //#array, #string, #float, #defaults, defaultlist
"ExampleMACD( fast_default = 12, slow_default = 26 )", { VExampleMACD, 0, 0, 0, 2, MACD_defaults },
// your other functions .....
};
So “2” is now at 4th place after VExampleMACD but not at 3rd place as before (remember, 4th place after DLL function name is number of default values). And NULL is replaced by MACD_defaults. MACD_defaults is your list of default values as you can see above of function table. That sample list consists of two values since two ones are expected as default ones in that example function (I am only repeating this for people who would read to quickly without carefully reading).
Note, I have changed the description too -> ExampleMACD( fast_default = 12, slow_default = 26 )
The stuff within the brackets will be shown in AFL editor if you type “ExampleMACD(” there and “Parameter info” being checked in Tools-Preferences-Editor.
I am still not sure if that’s what Nick was asking for or unsure about.
Please report/add if there are any mistakes/typos found in this quick tutorial.