Lua scripting

Post your questions, suggestions and experiences regarding game design, integration of external libraries here. For irrEdit, irrXML and irrKlang, see the
ambiera forums
Post Reply
Virion
Competition winner
Posts: 2148
Joined: Mon Dec 18, 2006 5:04 am

Lua scripting

Post by Virion »

Hi guys I have been adding Lua scripting to my game lately but I got some problems with it

Code: Select all

int game::addActor_lua(lua_State* luaVM)
{
    stringw file;
    file = (stringw)lua_tostring(luaVM, 1);

    smgr->addAnimatedMeshSceneNode(smgr->getMesh(file));

    //std::cout<<file.c_str()<<std::endl;

    return 1;
}
when I compile I got an error that says
error: invalid use of member 'game::smgr' in static member function
how to solve this problem? i'm fairly new to Lua (and never used a static function before) i would like to know how you guys manage to bind it with your game. thanks!
Last edited by Virion on Wed Sep 08, 2010 8:02 am, edited 1 time in total.
Mikhail9
Posts: 54
Joined: Mon Jun 29, 2009 8:41 am

Post by Mikhail9 »

I don't know about Lua, but the error indicates that addActor_lua is a static method of the class game. Static methods can only access static members of the class (i.e. those that are declared static in the class definition).

The thing about static variables is that there is only one copy of them for all instances of the class. If your game object is going to be a singleton, then that's fine, you can just declare smgr to be static too. But then you may as well make addActor_lua a regular member function and leave static out of it entirely.

The typical way to fix your problem is to pass a pointer to the instance of the game object you wish to modify to the static method when you call it. That way it knows which game object's smgr to work with.

Code: Select all

int game::addActor_lua(lua_State* luaVM, game* gameObj)
{
   gameObj->smgr->addAnimatedMeshSceneNode(...)
}
Luben
Posts: 568
Joined: Sun Oct 09, 2005 10:12 am
Location: #irrlicht @freenode

Post by Luben »

It's Lua, not LUA ;] http://www.lua.org/about.html#name

It's as Mikhail9 writes. But since you can''t pass extra parameters from lua as he proposes, you have to find another solution.
I'd recommend reading about metamethods ( http://www.lua.org/pil/13.html ), simple OO-techniques ( http://www.lua.org/pil/16.html ) and then how to combine it with objects from outside Lua ( http://www.lua.org/pil/28.html ).

I'd recommend reading the whole PiL ( http://www.lua.org/pil/index.html ) as well.

Checking http://lua-users.org/wiki/BindingCodeToLua for tools to speed up your C++ <-> Lua might be worthwhile as well (Example, Luabind)
If you don't have anything nice to say, don't say anything at all.
teto
Posts: 159
Joined: Thu Dec 03, 2009 9:37 pm
Location: /home
Contact:

Post by teto »

I higly recommand luabind to bind since it's very well designed with an active mailing list.
Post Reply