passing object to lua

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

passing object to lua

Noel Frankinet
Hello,
This is my first post on the list,

I'm writing a GUI framework for windows using lua.
I use Luna binding between C++ classes and lua, everything works fine.
Now I want to call back from C++ to lua, passing back the C++ object (eg a "Dialog") in order to be able the use class qulaified method inside the callback
How do I do that ?

Thank you.

--
Noël Frankinet
Gistek Software SA
http://www.gistek.net


Reply | Threaded
Open this post in threaded view
|

Re: passing object to lua

Achromat
Lol sorry u need the other way to call into lua *g*

here


//------ create functiontable and func IDoff -------
lua_newtable (m_pState);m_iThisRef = luaL_ref(m_pState, LUA_REGISTRYINDEX);
lua_rawgeti (m_pState, LUA_REGISTRYINDEX, m_iThisRef);
lua_pushlightuserdata (m_pState, (void *) this);
lua_rawseti (m_pState, -2, 0);´
lua_setglobal (m_pState, "this");


//test if an function exist btw. u need only one function to call
//named  OnEvent(string)    //load,close,refresh,info and so on
//u can make an register event so the user can choise the msg
//u should to send

this.OnEvent(this,event)  <---let show so
if event==load then
 do anything
elseif event==save
 dont load
end
end
//Check for OnEvent exists in script
bool CLuaScript::IsFunction(char * pFname)
{
lua_rawgeti (m_pState, LUA_REGISTRYINDEX, m_iThisRef);
lua_pushstring (m_pState, pFname);
lua_rawget (m_pState, -2);
lua_remove (m_pState, -2);
lua_rawgeti (m_pState, LUA_REGISTRYINDEX, m_iThisRef);

if (!lua_isfunction (m_pState, -2))
{
lua_pop (m_pState, 2);
return false;
}

return true;
}



OnEvent("load")  and so on fastest way

//let call lua the OnEvent  Message handler in luascript :-)
bool CLuaScript::OnEvent(char *strEvent)
{
if(!m_pState) return false;
lua_rawgeti (m_pState, LUA_REGISTRYINDEX, m_iThisRef);
lua_pushstring (m_pState, "OnEvent");
lua_rawget (m_pState, -2);
lua_remove (m_pState, -2);
lua_rawgeti (m_pState, LUA_REGISTRYINDEX, m_iThisRef);
lua_pushstring(m_pState,strEvent);
int iErr = lua_pcall(m_pState, 2,-1, 0);
if(iErr) return LuaErr(iErr);
return true;
}




----- Original Message ----- From: "Noel Frankinet" <[hidden email]>
To: "Lua list" <[hidden email]>
Sent: Monday, March 13, 2006 11:31 AM
Subject: passing object to lua


Hello,
This is my first post on the list,

I'm writing a GUI framework for windows using lua.
I use Luna binding between C++ classes and lua, everything works fine.
Now I want to call back from C++ to lua, passing back the C++ object (eg a "Dialog") in order to be able the use class qulaified method inside the callback
How do I do that ?

Thank you.

--
Noël Frankinet
Gistek Software SA
http://www.gistek.net