LuaBind:如何将类的特定实例绑定到Lua?
(旁注:这是游戏编程)
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不将类的实例绑定到 Lua。类的实例只是数据,你可以通过通常的方式将数据传递给Lua。不过,C++ 对象很特殊;因为它们是通过 Luabind 注册的,所以您必须使用 Luabind 方法将它们提供给 Lua 脚本。
向 Lua 提供数据的方法有多种,Luabind 涵盖了所有这些方法。例如,如果您有一个对象
x
,它是一个指向 Luabind 注册的类X
的指针,那么您可以通过多种方式为 Lua 提供x< /代码>。
您可以将
x
的值设置为全局变量。这是通过 Luabind 的object
接口和globals
函数完成的:显然,您可以将其放在另一个表中,该表位于另一个表中,等等,可以从全局访问该表。状态。但您需要确保这些表都存在。
将这些数据传递给 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 classX
that is registered with Luabind, there are several ways you can give Luax
.You could set the value of
x
into a global variable. This is done through Luabind'sobject
interface and theglobals
function: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 useluabind::call_function
to call it. You can then just passx
as a parameter to the function. Alternatively, if you prefer thelua_pcall
-style syntax, you can wrapx
in aluabind::object
and push it into the stack withluabind::object::push
.