从 C++ 调用 Lua 表中的函数
例如,我有一个 Lua 表/对象:
bannana
这个 Lua 表有一个其中的函数名为 chew
,它采用一个
bannana.chew(5)
我也使用过的参数 SWIG,并且有一个类CPerson
:
class CPerson {
public:
// ....
void Eat();
// ....
};
我可以从Lua获取这个对象的实例:
person = engine:getPerson()
我需要做的是以下Lua代码:
person = engine:getPerson()
person:Eat(bannana)
其中person:eat< /code> 将调用
bannana
表中的 chew
函数,并传递一个参数。
由于 CPerson
是用 C++ 实现的,假设 CPerson
类已经有一个 Lua 状态指针,那么实现 Eat()
需要进行哪些更改?
Edit1:我不想知道如何将C++类绑定到Lua,我已经有SWIG为我做这件事,我想知道如何从C++调用Lua表内的Lua函数。
Edit2: CPerson
类和 bannana
表都是一般示例,可以假设 CPerson
类已经有一个 LuaState 指针/引用,并且 Eat
方法的函数签名可以由应答者更改。
I have for example, a Lua table/object:
bannana
And this Lua table has a function inside it called chew
, that takes a parameter
bannana.chew(5)
I have also used SWIG, and have for example a class CPerson
:
class CPerson {
public:
// ....
void Eat();
// ....
};
I can obtain an instance of this object from Lua:
person = engine:getPerson()
What I need to be able to do is the following Lua code:
person = engine:getPerson()
person:Eat(bannana)
Where person:eat
would call the chew
function in the bannana
table, passing a parameter.
Since CPerson
is implemented in C++, what changes are needed to implement Eat()
assuming the CPerson
class already has a Lua state pointer?
Edit1: I do not want to know how to bind C++ classes to Lua, I already have SWIG to do this for me, I want to know how to call Lua functions inside Lua tables, from C++.
Edit2: The CPerson
class and bannana
table, are both general examples, it can be assumed that the CPerson
class already has a LuaState pointer/reference, and that the function signature of the Eat
method can be changed by the person answering.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
忽略任何错误检查...
Ignoring any error checking ...
也许“更简单的 Cpp 绑定”会有所帮助。
Maybe "Simpler Cpp Binding" will be helpful.