SWIG Lua和传递数组

时间:2009-02-17 16:01:54

标签: c++ arrays lua swig

我目前有以下lua代码:

    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)

其中g:SetVertexes()在C ++中实现为:

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

导致此错误:

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

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

尝试写作:

void CGeometry::SetVertexes(double vertexes[]);
界面定义中的

。从文档来看,SWIG在指针和数组之间做了区别。

答案 1 :(得分:0)

你可以按照这里描述的那样carrays.i模块:http://www.swig.org/Doc1.3/Library.html

例如,你在C ++中有这样的功能:

void fun(double arr[]) { }

然后在.i文件中加载模块:

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

在Lua剧本中:

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