[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Storing global userdata in Lua 4
- From: Markus Ewald <Markus_Ewald@...>
- Date: Sun, 10 Mar 2002 19:32:22 +0100
Hello Again!
In Lua 3.1 I used the following code to store a pointer to a class in a
lua_State
// Store pointer to class in Lua
int nTag = lua_newtag(m_pLuaState);
lua_pushusertag(m_pLuaState, this, nTag);
lua_setglobal(m_pLuaState, "_CMyClassPtr");
// Retrieve pointer again
lua_getglobal(pLuaState, "_CMyClassPtr");
CMyClass *pThis = static_cast<CMyClass *>(lua_touserdata(pLuaState, -1));
Now I tried to convert this code to Lua 4.0
// Store pointer to class in Lua
*static_cast<CMyClass **>(lua_newuserdata(m_pLuaState, sizeof(this)))
= this;
lua_setglobal(m_pLuaState, "_CMyClassPtr");
// Retrieve pointer again
lua_getglobal(pLuaState, "_CMyClassPtr");
CMyClass *pThis = *static_cast<CMyClass **>(lua_touserdata(pLuaState,
-1));
It doesn't seem to clean to me to allow the script to modify the pointer
at will.
Is there a better (and maybe quicker) way to do it (it's always just a
single pointer) ?
What about Lua 4.1w ?
-Markus-