我应该如何将 lua 函数绑定到 C++功能?
我有一个名为 Entity 的类,它有许多函数,如 onPickup、onDrop、onUse 等。我想要做的是编写一个脚本来定义所有这些函数并使它们可以从 C++ 函数调用。因此,C++ 中定义的函数只会调用具有某些功能的相应 Lua 函数。
但这是问题,我希望我编写的每个脚本,程序中的每个实体都在其自己的范围内工作。
我正在使用 LuaBind,并且我之前没有使用 Lua 的经验,所以我在这里有点迷失。
I have a class called Entity, which has many functions like onPickup, onDrop, onUse etc. What I want to do is, write a script that defines all of these functions and make them callable from the C++ functions. So the functions defined in C++ would just be calling their corresponding Lua functions that have some functionality.
But here's the problem, I want every script that I write, for every Entity in the program to be working in it's own scope.
I'm using LuaBind, and I have no prior experience with Lua, so I'm a little lost here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不使用 lua 绑定,但这可能会有所帮助。这个想法是在 C++ 类中注册 lua 函数,并在 C++ 类中保留对 lua 函数的引用。
为了定义可从 C/C++ 调用的 lua 函数,我使用 luaL_ref 在我的 C++ 对象中存储对回调函数的引用。
然后您可以在 C++ 事件中获取 lua 方法,
现在您可以调用此函数,并将 self 设置为您之前保存的表。哈
I don't use lua bind but this may help. The idea is to register the lua functions in your C++ class and keep a reference to the lua function in your C++ class.
To define a lua function that is callable from C/C++ I use luaL_ref to store a reference to the callback function in my C++ object.
and then you can get the lua method in your C++ event
now you can call this function with self set to the table you saved earlier. hth
您可以调用 Lua 函数,例如,
如果 Lua 函数返回一个 int 并采用 1 个参数。
我非常确定您可以根据需要创建任意数量的 lua_State。只需将实体的正确值传递给
call_function
即可。You can call a Lua function with, e.g.
if the Lua function returns an int and takes 1 parameter.
I'm pretty sure you can make as many
lua_State
's as you want. Just pass the correct one for the entity intocall_function
.要以您可能想要的方式完全实现这一点,需要深入研究 Lua 的一些更深奥的部分。不过,这是非常值得的。我将展示我如何处理这个问题的一个非常精简的版本。请注意,这里有很多小部分一起工作 - 主要是保存和调用保存的函数以及使用 C++ 对象作为 Lua 用户数据。
首先我们需要一个 C++ 类,它将事件处理程序(lua 函数)存储为简单整数的 lua 引用。我在这里使用一个数组,但你可以使用任何有意义的东西。这里发生的主要事情是你希望能够调用由 int 引用引用的 lua 函数。
现在您想要向 Lua 公开您的实体类。如果您不熟悉这是如何完成的,有一篇很好的文章
现在设置 Lua 状态并创建实体表并创建测试。该测试将创建一个实体并将其事件处理程序设置为传递给它的 Lua 函数。最后测试Lua函数是否被c++对象调用。
这还远未完成。首先,如果您需要两次设置事件处理程序,则需要先使用 luaL_unref 清除旧引用。其次,您可能希望将有关发生的事件的一些数据传递给事件处理程序。当前的事件处理程序不获取任何数据,因此 API 用户几乎无法继续操作。它可能至少应该传递对调用事件的对象的引用。这可以用来在 Lua 中创建非常强大且可用的 API。祝你好运!
To fully implement this the way you will probably want to will require digging around a bit in some of the more esoteric bits of Lua. It is well worth the time though. I'll show a very trimmed down version of how I have handled this. Be warned, there are a lot of little bits all working together here - mainly saving and calling saved functions and using c++ objects as Lua user data.
First we need a c++ class which will store events handlers (lua functions) as lua references which are simple ints. I am using an array here but you could use whatever makes sense. The main thing happening here is that you want to be able to call a lua function which is referred to by the int reference.
Now you want to expose your Entity class to Lua. If you are not familiar with how this is done there is a good article here. Essentially what is going on is that we are setting the user data returned from Entity.new() to a pointer to a pointer. This is so Lua does not garbage collect your object. Then create a meta table for "Entity" which will
hold all of the methods exposed to Lua.
Now set up the Lua state and create the Entity table and create a test. The test will create an Entity and set its event handler to a Lua function passed to it. Finally test that the Lua function is being called by the c++ object.
This is far from complete. First of all if you ever need to set an event handler twice you will need to use luaL_unref to clear out the old reference first. Second you will probably want to pass some data about the event which occurred to the event handler. The current event handler does not take any data so gives the user of the api very little to go on. It should probably at least pass a reference to the object which is calling the event. This can be used to create very powerful and usable Apis in Lua. Good luck!