C++ 和 Lua 函数之间的接口(带有 3D 向量参数)

我有一个 C++ 函数(在我的游戏引擎中运行一些物理模拟),它看起来像这样:

void doSomePhysics( int nIters, Vec3d pos, Vec3d vel ){ /*...不管...*/ }

我想从一个 Lua 脚本中调用这个函数,就像这样:

doSomePhysics( 100, {1.0,-2.0,3.0},{-8.0,7.0,-6.0} )

我正在尝试弄清楚如何将 Lua 接口到此函数。

我想为传递二维、三维和四维向量和矩阵编写几个通用的帮助函数,因为我将大量使用它们。

这是我目前正在尝试做的草图(但我知道它不正确):

void lua_getVec3(lua_State *L, Vec3d& vec ){
 // 将 Vec3 函数参数从 Lua 获得到 C++ 函数的通用帮助函数
 luaL_checktype(L, 1, LUA_TTABLE);
 lua_settop(L, 1);
 int a_size = lua_rawlen(L, 1);
 lua_rawgeti(L, 1, 1); vec.x = lua_tonumber(L, -1);
 lua_rawgeti(L, 2, 2); vec.y = lua_tonumber(L, -2);
 lua_rawgeti(L, 3, 3); vec.z = lua_tonumber(L, -3);
 lua_pop(L, 3);
}

int l_doSomePhysics(lua_State* L){
 // doSomePhysics 的 Lua 接口
 Vec3d pos,vel;
 int n = lua_tointeger(L, 1);
 lua_getVec3(L, pos );
 lua_getVec3(L, vel );
 doSomePhysics(n,pos,vel);
 lua_pushnumber(state, 123);
 return 1;
}

我读了几篇短文教程[1](http://www.lua.org/pil/27.1.html)[2](https://csl.name/post/lua-and-cpp/)[3](https://stackoverflow.com/questions/25940366/passing-array-to-c-as-argument-in-the-stack),但它看起来非常复杂、令人困惑和容易出错......我完全迷失在栈索引中(我在栈中的当前位置是什么?什么是正确的相对索引?等等)。我不愿相信最著名的游戏脚本语言需要如此多的样板代码和如此多的痛苦,才能将每个小函数与接口联系起来。


编辑:在 Vlad 的帮助下,我能够为 3D 向量和矩阵完成它

void lua_getVec3(lua_State *L, int idx, Vec3d& vec){
 // 将 Vec3 函数参数从 Lua 获得到 C++ 函数的通用帮助函数
 luaL_checktype(L, idx, LUA_TTABLE);
 lua_rawgeti(L, idx, 1); vec.x = lua_tonumber(L, -1); lua_pop(L, 1);
 lua_rawgeti(L, idx, 2); vec.y = lua_tonumber(L, -1); lua_pop(L, 1);
 lua_rawgeti(L, idx, 3); vec.z = lua_tonumber(L, -1); lua_pop(L, 1);
}

void lua_getMat3(lua_State *L, int idx, Mat3d& mat){
 // 将 Mat3 函数参数从 Lua 获得到 C++ 函数的通用帮助函数
 luaL_checktype(L, idx, LUA_TTABLE);
 lua_pushinteger(L, 1); lua_gettable(L, idx); lua_getVec3(L, -1, mat.a ); lua_pop(L, 1);
 lua_pushinteger(L, 2); lua_gettable(L, idx); lua_getVec3(L, -1, mat.b ); lua_pop(L, 1);
 lua_pushinteger(L, 3); lua_gettable(L, idx); lua_getVec3(L, -1, mat.c ); lua_pop(L, 1);
}

extern "C"
int l_doSomePhysics2(lua_State* L){
 // doSomePhysics 的 Lua 接口
 Vec3d pos;
 Mat3d mat;
 int n = lua_tointeger(L, 1);
 lua_getVec3(L, 2, pos );
 lua_getMat3(L, 3, mat );
 doSomePhysics2(n,pos,mat);
 //lua_pushnumber(L, 123);
 return 3;
}

适用于这个 lua 函数:

mat = {{1.1,-0.1,0.1},{-0.2,2.2,0.2},{-0.3,0.3,3.3}}
doSomePhysics2( 100,{-7.7,8.8,9.9},mat )
点赞
用户5675002
用户5675002

你的 void lua_getVec3(lua_State *L, Vec3d& vec) 函数缺少关键信息-在 Lua 堆栈上参数表的位置。重写为:

void lua_getVec3(lua_State *L, int idx, Vec3d& vec){
    // 通用的帮助函数,从 Lua 到 C++ 函数获取 Vec3 函数参数
    luaL_checktype(L, idx, LUA_TTABLE);
    lua_rawgeti(L, idx, 1); vec.x = lua_tonumber(L, -1);
    lua_rawgeti(L, idx, 2); vec.y = lua_tonumber(L, -1);
    lua_rawgeti(L, idx, 3); vec.z = lua_tonumber(L, -1);
    lua_pop(L, 3);
}

并像这样调用它:

int l_doSomePhysics(lua_State* L){
    // doSomePhysics 的 Lua 接口
    Vec3d pos,vel;
    int n = lua_tointeger(L, 1);
    lua_getVec3(L, 2, pos );
    lua_getVec3(L, 3, vel);
    doSomePhysics(n,pos,vel);
    lua_pushnumber(lua, 123);
    return 1;
}
2017-08-03 08:03:44