[英]How to bind a std::map to Lua with LuaBind
我正在嘗試將我的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);
return table;
}
這樣我就能在Lua中做到這樣的事情:
player.scripts["movement"].properties["stat"] = "idle"
print(player.scripts["movement"].properties["stat"])
但是,我在C ++中提供的代碼沒有編譯。 它告訴我在這行metatable["__index"] = &this->GetMeta;
有一個模糊的調用重載函數metatable["__index"] = &this->GetMeta;
和它后面的線。 我不確定我是否正確地這樣做了。
錯誤信息:
error C2668: 'luabind::detail::check_const_pointer' :
ambiguous call to overloaded function
c:\libraries\luabind-0.9.1\references\luabind\include\luabind\detail\instance_holder.hpp 75
這些都是SetMeta
和GetMeta
在FakeScript
:
static void GetMeta();
static 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;
}
return obj;
}
這工作正常,但它不允許我使用setter方法。 例如:
player.scripts["movement"].properties["stat"] = "idle"
print(player.scripts["movement"].properties["stat"])
在這段代碼中,它只是在兩行中觸發getter方法。 雖然如果它讓我使用setter,我將無法從這里的["stat"]
屬性中獲取密鑰。
LuaBind上有專家嗎? 我見過大多數人說他們之前從未使用過它。
您需要使用(未記錄的) make_function()
來從函數中創建對象。
metatable["__index"] = luabind::make_function(L, &this->GetMeta);
metatable["__newindex"] = luabind::make_function(L, &this->GetMeta);
不幸的是, make_function
這個(最簡單的)重載被破壞了,但你只需要在make_function.hpp
插入f
作為第二個參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.