【发布时间】:2011-11-20 23:30:34
【问题描述】:
(旁注:这是游戏编程)
使用 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-class 的一个实例并执行以下操作:
Player:SetPosition(200,300)
而不是硬着头皮去做
SetPosition("Player",200,300)
其中对应的 C++ SetPosition 函数需要查找 std::map 才能找到播放器。
这甚至可能吗?如果可以,我如何在 LuaBind 中做到这一点?
【问题讨论】: