如何使用LuaBind将std :: map绑定到Lua

我正在尝试将我的std::map<std::string,std::string>作为Lua的类属性公开。 我为我的Getter和Setter设置了此方法:

luabind :: object FakeScript :: GetSetProperties()
{
    luabind :: object table = luabind :: newtable(L);
    luabind :: object metatable = luabind :: newtable(L);

    metatable [“__index”] = & this-> GetMeta;
    metatable [“__newindex”] = & this-> SetMeta;

    luabind :: setmetatable <luabind :: object,luabind :: object>(table,metatable);

    返回表;
}

这样,它使我能够在Lua中执行此操作:

player.scripts [“movement”] .properties [“stat”] =“idle”
print(player.scripts [“movement”] .properties [“stat”])

但是,我提供给C ++的代码并没有得到编译。 它告诉我这一行metatable [“__index”] =&this->GetMeta;和之后的行中有函数重载的重载函数的模棱两可的调用。 我不确定我正在正确地执行此操作。

错误信息:

错误C2668:'luabind :: detail :: check_const_pointer':
重载函数的调用存在歧义
c:\ libraries \ luabind-0.9.1 \ references \ luabind \ include \ luabind \ detail \ instance_holder.hpp 75

这些是FakeScript中的“SetMeta”和“GetMeta”:

静态void GetMeta();
静态void SetMeta();

以前我在getter方法中执行了这个操作:

luabind :: object FakeScript :: getProp()
{
    luabind :: object obj = luabind :: newtable(L);

    for(auto i = this-> properties.begin(); i!= this-> properties.end(); i ++)
    {
        obj [i-> first] = i-> second;
    }

    返回对象;
}

这很好用,但是它不让我使用setter方法。 例如:

player.scripts [“movement”] .properties [“stat”] =“idle”
print(player.scripts [“movement”] .properties [“stat”])

在这段代码中,它只会在两行中触发getter方法。 虽然如果它让我使用setter,我就无法从属性中获取关键字,因为它是此处的[“stat”]

有没有LuaBind专家? 我见过大多数人说他们以前从未使用过它。

点赞
用户2128694
用户2128694

你需要使用(未记录的)make_function() 来从函数创建对象。

metatable["__index"] = luabind::make_function(L, &this->GetMeta);
metatable["__newindex"] = luabind::make_function(L, &this->GetMeta);

不幸的是,这个(最简单的)make_function 的重载是有问题的,但你只需要在 make_function.hpp 中将 f 作为第二个参数 插入 即可。

2013-07-18 17:02:04