[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Newbie: How to make static pointers avaiblabe in Lua script from C++
- From: Roland Strålberg <rstralberg@...>
- Date: Sun, 26 Feb 2006 23:37:00 +0100
Hi
After have trying and reading and trying I still cant get following to work
(its a singleton)
I'm using Lunar.h. Sorry for this long post but I really need some starting
help.
class CCC
{
public:
static CCC* Instance() ;
void Print( const char* pText ) { }
CCC() {}
~CCC() {}
private:
static CCC* m_pInstance ;
// Lua declaration
public:
static const char className[];
static Lunar<CCC>::RegType methods[];
CCC( lua_State* ) ;
int getInstance( lua_State* ) ;
int print( lua_State* ) ;
} ;
CCC* CCC::m_pInstance = 0 ;
CCC* CCC::Instance()
{
if ( m_pInstance == 0 )
m_pInstance = new CCC ;
return m_pInstance ;
}
// Lua implenation
const char CCC::className[] = "CCC" ;
Lunar<CCC>::RegType CCC::methods[] =
{
{ "getInstance", &CCC::getInstance },
{ "print", &CCC::print},
{ 0, 0 }
} ;
CCC::CCC( lua_State* L )
{
}
int CCC::getInstance(lua_State* L )
{
lua_pushlightuserdata( L, Instance() ) ;
return 1 ;
}
int CCC::print(lua_State* L )
{
const char* p = luaL_checkstring( L, -1 ) ;
Print( p ) ;
return 0 ;
}
// CCC is registered somewhere else with
// Lunar<CCC>::Register( pLuaState ) ;
/** Lua test code
a = CCC:getInstance()
a.print( "Hello" ) ;
*/
I can compile without errors
but when i run my program and executes the lua.file
loaded by luaL_loadfile( L, "test.lua" ), and then lua_pcall( L, 0,
LUA_MULTRET, 0 )
i get this error when a = CCC:getInstance() is executed in script
"calling 'getInstance' on bad self (CCC expected, got table)'"
I would be really thankful for any help.
Roland Strålberg
mailto:rstralberg@msn.com
http://www.rstralberg.com