Skip to main content
GameDev.net gamedev.net
🔒 Locked

Problem with luabind and singleton methods

Started by kyon_kun Jun 16, 2010 at 10:23 PM 6 replies 2.2k views
Original Post
kyon_kun
kyon_kun
Hi, I'm new in this forum and excuse me for my english.

I'm read the last thread about this topic seeking help for the problem of register a singleton/static methods (follow this link http://www.gamedev.net/community/forums/topic.asp?topic_id=469655) using luabind.

In that thread being a solution using a scope and I try to use these, but that
doesn't work.

my code is this.

Manager* Manager::mSingleton = 0;

Manager* Manager::getInstance()
{
if(mSingleton == 0)
mSingleton = new Manager;
return mSingleton ;
}

void Manager::registerManager(lua_State *rgt)
{
open(rgt);
module(rgt)[
class_("Manager")
.def("createRoot",&Manager::getCreateRoot)
.def("createBehavior",&Manager::getCreateBehavior)
.def("createSelector",&Manager::getCreateSelector)
.scope[
def("getInstance",&Manager::getInstance)
]

];

}

and the script

test = Manager:getInstance()
test:createRoot("Root")


when I try to run it, the application launch this message

"treeLua.lua:3:attempt to index global 'Manager'(a nil value)"

Where am I going wrong

thanks
mrwonko
mrwonko
Quote:
Original post by kyon_kun
test = Manager:getInstance()
[...]
Where am I going wrong

I'm pretty sure it should be Manager.getInstance()
kyon_kun
kyon_kun
Now try to use your suggestiont but the error persists

thanks
rip-off
rip-off
Has registerManager() been called on the lua_State in question?
kyon_kun
kyon_kun
I use this code to call the registerManager() in the main function


int main(int argc, char** argv)
{
lua_State *data = lua_open();

int err = luaL_loadfile(data, "treeLua.lua");

Manager::getInstance()->registerManager( data );

if (lua_pcall(data,0 , 0, 0) != 0){
MessageBox(0,lua_tostring(data, -1),"Error",MB_OK);
}

lua_close(data);

system("pause");

return 0;

}

But I remember the line where the use registerManager() was in comments.When I try to use it, the application show this message in the MessageBox.

"error Manager* getInstance()"

will I need to change the singleton method that returns a reference to the instance?

thanks
mrwonko
mrwonko
I tested your code (this is what I wrote):
#include <iostream>#include <lua.hpp>#include <luabind/luabind.hpp>#include <string>using namespace luabind;class Manager{    public:        static Manager* getInstance();        void registerManager(lua_State *rgt);        void getCreateRoot(const std::string& ignored) {}        void getCreateBehavior() {}        void getCreateSelector() {}    private:        static Manager* mSingleton;};Manager* Manager::mSingleton = 0;Manager* Manager::getInstance(){    if(mSingleton == 0)        mSingleton = new Manager;    return mSingleton ;}void Manager::registerManager(lua_State *rgt){    open(rgt);    module(rgt)[        class_<Manager>("Manager")        .def("createRoot",&Manager::getCreateRoot)        .def("createBehavior",&Manager::getCreateBehavior)        .def("createSelector",&Manager::getCreateSelector)        .scope[            def("getInstance",&Manager::getInstance)        ]    ];}int main(){    lua_State *data = lua_open();    int err = luaL_loadfile(data, "test.lua");    if(err)    {        std::cout<<"Error: "<<lua_tostring(data, -1)<<std::endl;        lua_close(data);        return 0;    }    Manager::getInstance()->registerManager( data );    if (lua_pcall(data,0 , 0, 0) != 0)    {        std::cout<<"Error: "<<lua_tostring(data, -1)<<std::endl;    }    lua_close(data);    return 0;}


You need to call registerManager() or the functions are not registered.

The Error "Manager* getInstance()" is caused by writing
Manager:getInstance()

instead of
Manager.GetInstance()


I would do the luabind::open outside of register manager and change the order a little bit, like this:
int main(){    lua_State *data = lua_open();        //initialize luabind    open(data);        //register the manager (remove open(rgt) from there)    Manager::getInstance()->registerManager( data );    int err = luaL_loadfile(data, "test.lua");    //...

because even if you register multiple classes luabind::open must not be called multiple times.
kyon_kun
kyon_kun
thanks for your help mrwonko!!!

I'm tried using your suggestion but now the application send this Message

"Assertion failed: crep && you are trying to use an unregister type"

This is my script now

test = Manager.getInstance()
test:createRoot("Root")
test:createSelector("Root","Selector1")

and the c++ code is the same with this change

lua_State *data = lua_open();
///use "open" outside of "registerManager"
open(data);
int err = luaL_loadfile(data, "treeLua.lua");
Manager::getInstance()->registerManager( data );

I think the problem is the way in which register the static function in lua,
maybe i need change the scope for register my singleton method.

thanks mrwonko
mrwonko
mrwonko
Quote:
Original post by kyon_kun
and the c++ code is the same with this change

lua_State *data = lua_open();
///use "open" outside of "registerManager"
open(data);
int err = luaL_loadfile(data, "treeLua.lua");
Manager::getInstance()->registerManager( data );

I'd change that to
        lua_State *data = lua_open();        ///use "open" outside of "registerManager"	open(data);	Manager::getInstance()->registerManager( data );	int err = luaL_loadfile(data, "treeLua.lua");

I'm not sure if this is your problem - Is that failed assert by you? I.e. did you write something like this?
assert(crep && you are trying to use an unregitered type);

If not then it's probably from luabind - well, try changing the order as I just said and remember to remove the open from registerManager if you didn't do that allready.

[Edited by - mrwonko on June 21, 2010 1:14:43 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.