ed.koffeman Posted January 21, 2014 Share Posted January 21, 2014 I can pass an array to a global C++ function, from the example given in the topic: int arrayOfInts[] = ( 0, 1 ); int i = 0; my_array_vector_set( arrayOfInts, i, string(i) ); no problem. However, doing essentially the same call, but to a method of a C++ class gives an error: ArrayPassingClass passingTest = new ArrayPassingClass(); passingTest.my_array_vector_set2( arrayOfInts, i, string(i) ); gives "Interpreter::parse_expression(): arrays can't be used in expressions" In C++: class ArrayPassingClass { public: // 2.1. Declare a class constructor that will be exported into Unigine script. ArrayPassingClass() { } ~ArrayPassingClass() { } void my_array_vector_set2(const Variable &id,int index,const Variable &v) { ArrayVector vector = ArrayVector::get( Interpreter::get(), id); vector.set(index,v); } };void my_array_vector_set(const Variable &id,int index,const Variable &v) { ArrayVector vector = ArrayVector::get( Interpreter::get(), id); vector.set(index,v); } ... ExternClass<ArrayPassingClass> *my_arrayPassingClass = MakeExternClass<ArrayPassingClass>(); my_arrayPassingClass->addConstructor(); my_arrayPassingClass->addFunction( "my_array_vector_set2", &ArrayPassingClass::my_array_vector_set2 ); Interpreter::addExternClass("ArrayPassingClass", my_arrayPassingClass ); Interpreter::addExternFunction("my_array_vector_set",MakeExternFunction(&my_array_vector_set,"[]")); Do things need to be done differently when calling a member function? or, is this a bug? If so, does it have a work-around? Link to comment
frustum Posted January 22, 2014 Share Posted January 22, 2014 Please add array declaration string into the addFunction() call: my_arrayPassingClass->addFunction( "my_array_vector_set2", &ArrayPassingClass::my_array_vector_set2, "[]," ); Link to comment
ed.koffeman Posted January 30, 2014 Author Share Posted January 30, 2014 Thanks, that worked. I could not find any documentation that would have pointed me to that solution. It looks like the C++ reference manual documentaion is lacking for the ExternClass addFunction method for member functions. It appears similar to the Interpreter::addExternFunction / MakeExternFunction type of call combination. The documentation for that just says "const char * args - Default arguments." I infer from that that the "[]" I needed to add simply provides an empty array if the argument is missing. It's not very clear, to say the least, that it is actually mandatory to supply a default argument when an array is to be passed. I actully do not at all wish a default argument to be supplied if it's accidentally left off, so this appears counterproductive as well as counterintuitive. Are there arguments besides the Unigine containers that require a hint like that? Link to comment
unclebob Posted February 21, 2014 Share Posted February 21, 2014 Hi ed! There are some useful samples in documentation about container interaction between C++ and Unigine Script and default arguments: 1) https://developer.unigine.com/en/docs/1.0/cpp_api/usage/arrays 2) https://developer.unigine.com/en/docs/1.0/cpp_api/usage/functions#args This should explain everything. Have a good time and sorry for late reply! :) Link to comment
Recommended Posts