SWIG Lua and passing arrays

I currently have the following lua code:

g = engine.CGeometry() vertexes = {} vertexes[1] = 0 vertexes[2] = 0 vertexes[3] = 0 vertexes[4] = 0 vertexes[5] = -1 vertexes[6] = 0 vertexes[7] = -1 vertexes[8] = 0 vertexes[9] = 0 print "adding vertexes" g:SetVertexes(vertexes) 

where g: SetVertexes () is implemented in C ++ as:

 void CGeometry::SetVertexes(double* vertexes){ this->vertexes = vertexes; } 

leads to this error:

 adding vertexes PANIC: unprotected error in call to Lua API (Error in SetVertexes (arg 2), expected 'double *' got 'table') Press any key to continue . . . 

Any ideas?

+4
source share
2 answers

Try to write:

 void CGeometry::SetVertexes(double vertexes[]); 

in the defintion interface. Judging by the documentation, SWIG makes the difference between pointers and arrays.

+1
source

You can use the carrays.i module as described here: http://www.swig.org/Doc1.3/Library.html

For example, you have a function like this in C ++:

 void fun(double arr[]) { } 

Then you load the module into the .i file:

 %include "carrays.i" %array_functions(double, doubleArray); void fun(double arr[]); 

And in the Lua script:

 a = new_doubleArray(10) # Create an array doubleArray_setitem(a,0,5.0) # Set value 5.0 on index 0 fun(a) # Pass to C delete_doubleArray(a) # Destroy array 
0
source

All Articles