[Closed] C++ Optional Arguments
I want to expose a function with two arguments and two optional inputs:
#include <maxscript/maxscript.h>
#include <maxscript/maxwrapper/mxsobjects.h>
#include <maxscript/macros/define_instantiation_functions.h>
def_visible_primitive(TestFunction, "TestFunction");
Value* TestFunction_cf(Value **arg_list, int count)
{
// TestFunction <argA> <argB> [optionA:false] [optionB:false]
check_arg_count_with_keys(TestFunction, 2, count);
Value* tmp = NULL;
BOOL optionA = bool_key_arg(optionA, tmp, FALSE);
BOOL optionB = bool_key_arg(optionB, tmp, FALSE);
return &ok;
}
But I got error:
Error C2065 'n_optionA': undeclared identifier
Error C2065 'n_optionB': undeclared identifier
Where I should declare them?
you probably need to add
def_name ( optionA)
def_name ( optionB)
either via an #include or in the file just after the current #includes
Like this?
#include <maxscript/maxscript.h>
#include <maxscript/maxwrapper/mxsobjects.h>
#include <maxscript/macros/define_instantiation_functions.h>
def_name(optionA)
def_name(optionB)
def_visible_primitive(TestFunction, "TestFunction");
Value* TestFunction_cf(Value **arg_list, int count)
{
// TestFunction <argA> <argB> [optionA:false] [optionB:false]
check_arg_count_with_keys(TestFunction, 2, count);
Value* tmp = NULL;
BOOL optionA = bool_key_arg(optionA, tmp, FALSE);
BOOL optionB = bool_key_arg(optionB, tmp, FALSE);
return &ok;
}
I got this error:
add
#include <maxscript/macros/define_external_functions.h>
before the def_names
I just redefined def_name macro and everything works now, But I’m still confused why including define_external_functions.h doesn’t work.
#include <maxscript/maxscript.h>
#include <maxscript/maxwrapper/mxsobjects.h>
#include <maxscript/macros/define_instantiation_functions.h>
#undef def_name
#define def_name(name) Value* n_##name;
def_name(optionA)
def_name(optionB)
def_visible_primitive(TestFunction, "TestFunction");
Value* TestFunction_cf(Value **arg_list, int count)
{
// TestFunction <argA> <argB> [optionA:false] [optionB:false]
check_arg_count_with_keys(TestFunction, 2, count);
Value* tmp;
BOOL optionA = bool_key_arg(optionA, tmp, FALSE);
BOOL optionB = bool_key_arg(optionB, tmp, FALSE);
return &ok;
}
[quote=]But I’m still confused why including define_external_functions.h doesn’t work.yeah the sdx mxs extension projects are very fussy over include order IIRC you have to also include maxscript/macros/local_implementations.h in your library initialization routine followed by the definitions again (why they are best kept in an include) . It’s all very messy.
#include <maxscript/macros/local_implementations.h>
cannot open source file "ClassCfg.h"
Error C1083 Cannot open include file: 'ClassCfg.h': No such file or directory
This pattern works:
#include <maxscript/maxscript.h>
#include <maxscript/maxwrapper/mxsobjects.h>
// ============================================================================
#ifdef ScripterExport
#undef ScripterExport
#endif
#define ScripterExport __declspec( dllexport )
#include <maxscript\macros\define_external_functions.h>
#include <maxscript\macros\define_instantiation_functions.h>
// ============================================================================
def_name(optionA)
def_name(optionB)
// ============================================================================
def_visible_primitive(TestFunction, "TestFunction");
Value* TestFunction_cf(Value **arg_list, int count)
{
// TestFunction <argA> <argB> [optionA:false] [optionB:false]
check_arg_count_with_keys(TestFunction, 2, count);
Value* tmp;
BOOL optionA = bool_key_arg(optionA, tmp, FALSE);
BOOL optionB = bool_key_arg(optionB, tmp, FALSE);
if (optionA)
{
return &true_value;
}
else
{
return &false_value;
}
}
But now problem is the optional argument always return false (default value) , It means bool_key_arg didn’t catch the key:
TestFunction unsupplied unsupplied OptionA:true
-- false
I’m facing very strange behavior of optional arguments.
If I check them in the beginning of the function they return their values without any problem.
But if I instead try to access them for the first time in the middle or close to the end of my function then they turn unsupplied for unknown reason. Could it be that they’re garbage collected?