通过现有的 C++对象传递给 Lua 并调用传递的对象成员函数

发布于 2024-10-09 04:13:52 字数 1714 浏览 0 评论 0原文

我正在开发一个小型模拟项目,该项目使用 Lua 来驱动各个单元(蚂蚁)的行为,并使用 Luabind 将 C++ 和 Lua 端粘合在一起。每个单独的蚂蚁(有不同的类型,从基类 Ant 派生)都有一个 Run() 函数,它调用适当的脚本;然后,脚本执行需要采取的任何操作,调用公开的类函数和可能的自由函数。我已经获得了 Run 函数(在 C++ 中),可以成功执行 Lua 脚本中匹配的 Run 函数(目前仅打印一些文本)。

void AntQueen::Run()
{
    lua->GetObject("QueenRun")(GetID());
}

lua 只是一个管理器类,它从脚本中检索函数。上面在 Lua 文件中调用以下代码:

function QueenRun(ID)
    print("The Queen is running!")
    print(ID)
end

对于 AntQueen 类,Luabind 注册看起来像这样:

void Register(lua_State *luaState)
{
    using namespace luabind;
    module(luaState)
    [
        class_<AntQueen, Ant>("AntQueen")
        .def("Eat", &AntQueen::Eat)
        .def("ExtractLarvae", &AntQueen::ExtractLarvae)
        .def("GetMaxLarvaeProduced", &AntQueen::GetMaxLarvaeProduced)
        .def("GetNumAvailLarvae", &AntQueen::GetNumAvailLarvae)
    ];
}

现在的设置方式是,通过一个简单的工厂/管理器创建、删除和查找 ant。每只蚂蚁都可以通过调用 static Ant* AntFactory::GetAntByID(const int ID) 来检索,它只是在哈希映射中查找蚂蚁并返回指向该蚂蚁的指针。我想做的是让 Lua 能够做如下的事情:

function QueenRun(ID)
    ant = GetAntByID(ID)
    larvae = ant:GetNumAvailLarvae()
    print(larvae)
    ant:Eat()
end

上面只是一个虚构的例子,但希望它能展示我想要实现的目标。我不希望 Lua 对对象进行垃圾收集,因为它们已经在 C++ 端进行管理。在测试所有内容时,任何在 Lua 中执行以下操作的尝试

ant = GetAntByID(ID)

都会导致调用 abort() 并导致程序崩溃并烧毁。

R6010
-abort() has been called

我似乎只是缺少一些关于如何来回穿梭一切的东西(这是我第一次尝试将 Lua 和 C++ 粘合在一起,超越玩具程序)。我很确定传递一个简单的指针不是这样做的方法; lightuserdata 似乎是我正在寻找的,但它也有很多限制。

总结一下:这里发生了什么导致 abort 被调用,以及如何使用 Luabind/Lua C API 获取传递给 Lua 的 C++ 对象的指针并调用成员函数那个指针就好像它是一个对象(不让 Lua 垃圾收集它)?

I'm working on a little simulation project which uses Lua to drive the behavior of individual units (ants) and using Luabind to glue the C++ and Lua sides together. Each individual ant (there are different types, derived from the base class Ant) has a Run() function, which calls the appropriate script; the script then carries out whatever actions need to be taken, calling the exposed class functions and possibly free functions. I've gotten the Run function (in C++) to successfully execute the matching Run function in the Lua script (which just prints some text at the moment).

void AntQueen::Run()
{
    lua->GetObject("QueenRun")(GetID());
}

lua is just a manager class which retrieves the function from the script. The above is calling the following in a Lua file:

function QueenRun(ID)
    print("The Queen is running!")
    print(ID)
end

And Luabind registration looks like this for the AntQueen class:

void Register(lua_State *luaState)
{
    using namespace luabind;
    module(luaState)
    [
        class_<AntQueen, Ant>("AntQueen")
        .def("Eat", &AntQueen::Eat)
        .def("ExtractLarvae", &AntQueen::ExtractLarvae)
        .def("GetMaxLarvaeProduced", &AntQueen::GetMaxLarvaeProduced)
        .def("GetNumAvailLarvae", &AntQueen::GetNumAvailLarvae)
    ];
}

The way it's set up now, ants are created, removed, and found through a simple factory/manager. Each ant can be retrieved by calling static Ant* AntFactory::GetAntByID(const int ID) which just finds the ant in a hash map and returns a pointer to the ant. What I'm trying to do is get Lua to be able to do something like the following:

function QueenRun(ID)
    ant = GetAntByID(ID)
    larvae = ant:GetNumAvailLarvae()
    print(larvae)
    ant:Eat()
end

The above is just a made up example, but hopefully it shows what I'm trying to achieve. I don't want Lua to garbage collect the objects, because they are managed already on the C++ side. While testing everything out, any attempt to do the following:

ant = GetAntByID(ID)

in Lua resulted in abort() being called and the program crashing and burning.

R6010
-abort() has been called

I just seem to be missing something with how everything gets shuttled back and forth (this is my first foray into gluing Lua and C++ together beyond toy programs). I'm pretty sure passing a plain pointer isn't the way to do it; lightuserdata seems to be what I'm looking for, but it also has a bunch of restrictions.

So to sum up: What is going on here that causes abort to be called and how can I use Luabind/the Lua C API to get a pointer to a C++ object passed to Lua and call member functions on that pointer as if it were an object (without letting Lua garbage collect it)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡莣 2024-10-16 04:13:52

这个问题的解决方案似乎与 AntFactory 类/成员函数是静态的有关。一旦我从注册和使用 this: 切换

//C++

static int AntFactory::CreateAnt(foo, bar)
{}

//Lua

factory:CreateAnt(foo, bar)

到实例化对象和像这样的常规成员函数:

//C++

int AntFactory::CreateAnt(foo, bar)
{}

//Lua

factory:CreateAnt(foo, bar)

一切都没有任何问题(在还将工厂推送到全局表之后)。我认为原因是尝试在非实例化的 C++ 对象上调用静态成员函数会失败,因为 Lua(或 Luabind?)无法有对象来查询调用。

The solution to this problem seemed to be tied to the AntFactory class/member functions being static. As soon as I switched from registering and using this:

//C++

static int AntFactory::CreateAnt(foo, bar)
{}

//Lua

factory:CreateAnt(foo, bar)

to an instantiated object and regular member functions like this:

//C++

int AntFactory::CreateAnt(foo, bar)
{}

//Lua

factory:CreateAnt(foo, bar)

everything worked with no problems at all (after also pushing the factory to the global table). I think the reason for this is that trying to call static member functions on a non-instantiated C++ object fails due to Lua (or Luabind?) not being able to have an object to query for calls.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文