Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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;
share|improve this question
    
Take a look at Sol –  Bartek Banachewicz Sep 2 at 14:27

2 Answers 2

One way to do this is to maintain the table entirely in C++. You wouldn't have any actual properties in the Lua table at all; you'd just have a placeholder table with its __index and __newindex metamethods mapped to C++ functions that get and set properties from the underlying std::map. I'm not familiar enough with LuaBind to show you the exact code to make this happen, but it shouldn't be hard to do.

share|improve this answer
    
Actually right now my table is entirely in C++. The getter method which is named as getProp() has been set as getter for the properties property of FakeScript class with LuaBind. This means properties is bind as a pointer in real Lua script which works with mine getter and setter. I've worked with metamethods, but it would ruin whole purpose of LuaBind if I do that and LuaBind itself is setting __index & __newindex fields. I can't do that. –  MahanGM Jul 8 '13 at 20:00
    
But your properties getter is creating a Lua table that has the properties as actual entries in it, right? That's what your getProp() function that you posted is doing, IIUC. I'm saying it shouldn't put the actual properties in the Lua table at all - it should return a fake table, that has no actual entries, but just forwards to the std::map via metamethods (however that is done in LuaBind). –  Nathan Reed Jul 8 '13 at 22:00
    
I got it. I should give this a try, but again it ruins the purpose of LuaBind. I can do this in plain Lua itself. –  MahanGM Jul 8 '13 at 22:30

I think Lua can access and create objects on tables with array-like syntax. If it doesnt, Squirrel (which is based on Lua, and have much more OOP sugars) can, here it is: http://squirrel-lang.org/doc/squirrel3.html

Your script would be looking like this:

player.scripts["movement"] <- new PlayerClass()
player.scripts["movement"].properties["stat"] = "stand"
print (player.scripts["movement"].properties["stat"])
share|improve this answer
1  
I've tried squirrel language before and I say it's fantastic. Though I'm nearly finishing my engine and I can't change scripting language since I had to adapt my engine with it. I've done most of the things in squirrel that I wasn't able to do them in Lua. Although Lua is powerful, I wish I could get to know squirrel before using Lua. –  MahanGM Jul 9 '13 at 15:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.