LuaBind:如何将类的特定实例绑定到Lua?

发布于 2024-12-06 01:55:33 字数 1012 浏览 3 评论 0原文

(旁注:这是游戏编程)

使用 LuaBind 将整个类绑定到 Lua 很容易:

class test
{
    test()
    {
        std::cout<<"constructed!"<<std::endl;
    }

    void print()
    {
        std::cout<<"works!"<<std::endl;
    }
}

然后我们可以这样做:

module[some_lua_state]
[
    class_<test>("test")
    .def(constructor<>())
    .def("print",&test::print)
];

现在我可以在 Lua 中创建该类的实例并使用它:

lua_example.lua

    foo = test() //will print "constructed!" on the console
    foo:print()  //will print "works!" on the console

但是,现在我想将一个特定的测试实例绑定到Lua。这将使我能够将对象传递给 Lua,例如 Player 类的实例,并执行以下

Player:SetPosition(200,300)

操作

SetPosition("Player",200,300)

: ;std::字符串,实体 *>来寻找玩家。

这是否可能,如果可以,我该如何在 LuaBind 中做到这一点?

(sidenote: This is game programming)

Binding entire classes to Lua using LuaBind is easy:

class test
{
    test()
    {
        std::cout<<"constructed!"<<std::endl;
    }

    void print()
    {
        std::cout<<"works!"<<std::endl;
    }
}

And then we can do:

module[some_lua_state]
[
    class_<test>("test")
    .def(constructor<>())
    .def("print",&test::print)
];

Now I can create instances of the class in Lua and use it:

lua_example.lua

    foo = test() //will print "constructed!" on the console
    foo:print()  //will print "works!" on the console

However, now I'd like to bind a specific instance of test to Lua. This would enable me to pass objects down to Lua, e.g. an instance of the Player-class and do something like:

Player:SetPosition(200,300)

As opposed to going the hard way and having something like

SetPosition("Player",200,300)

where the corresponding C++ SetPosition function needs to look up a std::map<std::string,Entity *> to find the player.

Is this even possible and if so, how can I do it in LuaBind?

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

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

发布评论

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

评论(1

蓝颜夕 2024-12-13 01:55:33

您不将类的实例绑定到 Lua。类的实例只是数据,你可以通过通常的方式将数据传递给Lua。不过,C++ 对象很特殊;因为它们是通过 Luabind 注册的,所以您必须使用 Luabind 方法将它们提供给 Lua 脚本。

向 Lua 提供数据的方法有多种,Luabind 涵盖了所有这些方法。例如,如果您有一个对象 x,它是一个指向 Luabind 注册的类 X 的指针,那么您可以通过多种方式为 Lua 提供 x< /代码>。

您可以将 x 的值设置为全局变量。这是通过 Luabind 的 object 接口和 globals 函数完成的:

luabind::globals(L)["NameOfVariable"] = x;

显然,您可以将其放在另一个表中,该表位于另一个表中,等等,可以从全局访问该表。状态。但您需要确保这些表都存在。

将这些数据传递给 Lua 的另一种方法是调用 Lua 函数并将其作为参数传递。您可以使用 luabind::object 接口将函数作为对象获取,然后使用 luabind::call_function 来调用它。然后,您只需将 x 作为参数传递给该函数即可。或者,如果您更喜欢 lua_pcall 风格的语法,您可以将 x 包装在 luabind::object 中,然后使用 <代码>luabind::object::push。

You don't bind instances of classes to Lua. Instances of classes are just data, and you can pass data to Lua via the usual means. C++ objects are special though; because they're registered through Luabind, you have to use Luabind methods to provide them to Lua scripts.

There are several ways to give data to Lua, and Luabind covers all of them. For example, if you have an object x, which is a pointer to class X that is registered with Luabind, there are several ways you can give Lua x.

You could set the value of x into a global variable. This is done through Luabind's object interface and the globals function:

luabind::globals(L)["NameOfVariable"] = x;

Obviously, you can put that in another table, which lives in another table, etc, which is accessible from the global state. But you'll need to make sure that the tables all exist.

Another way to pass this data to Lua is to call a Lua function and pass it as a parameter. You can use the luabind::object interface to get a function as an object, then use luabind::call_function to call it. You can then just pass x as a parameter to the function. Alternatively, if you prefer the lua_pcall-style syntax, you can wrap x in a luabind::object and push it into the stack with luabind::object::push.

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