C++ Optional Arguments


#1

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?


#2

you probably need to add


def_name ( optionA)
def_name ( optionB) 

either via an #include or in the file just after the current #includes


#3

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:


#4

add


#include <maxscript/macros/define_external_functions.h>

before the def_names


#5

Same error, Did you tested yourself?


#6

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;
}


#7

[quote=]But I’m still confused why including define_external_functions.h doesn’t work.[/quote]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.


#8

#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


#9

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


#10

I found the method that works. Thanks to the blurDLX

#define n_someKey ( Name::intern( _T( "someKey" ) ) )

#11

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?