How to bind std::map to Lua with LuaBind

Posted by MahanGM on Game Development See other posts from Game Development or by MahanGM
Published on 2013-07-08T16:50:23Z Indexed on 2013/11/06 10:13 UTC
Read the original article Hit count: 348

Filed under:
|

Is this possible in lua to achieve?

player.scripts["movement"].properties["stat"] = "stand"
print (player.scripts["movement"].properties["stat"])

I've done getter method in c++ with this approach:

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;
}

But I'm stuck with setter. The first line in lua code which I'm trying to set value "stand" for key "stat" is not going to work and it keep redirecting me to the getter method. Setter method only works when I drop ["stat"] from properties.

I can do something like this for setter in my script:

player.scripts["movement"].properties = {stat = "stand"}

But this isn't what I want because I have to go through my real keys in c++ to determine which key is placed in setter argument table value.

This is my map in class:

std::map<std::string, std::string> properties;

© Game Development or respective owner

Related posts about c++

Related posts about lua