Sign in to follow this  

[Lua] Retrieving custom-type global variable?

Recommended Posts

Hello everyone! Okay, I've used toLua to write a Lua wrapper for my 'Image' class. I've written a function to retrieve the (Lua) global 'BottomLayer' of type Image, here it is:
Image *Map::GetBottomLayer()
	lua_getglobal(Script, "BottomLayer");
	return (Image*)(lua_touserdata(Script, 1));

It returns a non-null pointer, so far so good, but the Image's width is 13000-something when it should be 800. Everything works okay in Lua, the width is 800 and everything seems to work. It's somethig with lua_touserdata(...) it seems. Or maybe not? Can anyone aid me? Thanks in advance!

Share this post

Link to post
Share on other sites
I work mostly in FreeBasic, so I don't know much about C (or tolua for that matter), however I found some userdata macros in the source for lua player which one of the guys on the FreeBasic board was able to convert to FB code for me

Here's the original C/C++ code for the macros:

#define UserdataStubs(HANDLE, DATATYPE) DATATYPE *to##HANDLE (lua_State *L, int index) {   DATATYPE* handle  = (DATATYPE*)lua_touserdata(L, index);   if (handle == NULL) luaL_typerror(L, index, #HANDLE);   return handle; } DATATYPE* push##HANDLE(lua_State *L) { 	DATATYPE * newvalue = (DATATYPE*)lua_newuserdata(L, sizeof(DATATYPE)); 	luaL_getmetatable(L, #HANDLE); 	lua_pushvalue(L, -1); 	lua_setmetatable(L, -3); 	lua_pushstring(L, "__index"); 	lua_pushstring(L, #HANDLE); 	lua_gettable(L, LUA_GLOBALSINDEX); 	lua_settable(L, -3); 	lua_pop(L, 1); 	return newvalue; }

#define UserdataRegister(HANDLE, METHODS, METAMETHODS) int HANDLE##_register(lua_State *L) { luaL_newmetatable(L, #HANDLE); /* create new metatable for file handles */ lua_pushliteral(L, "__index"); lua_pushvalue(L, -2); /* push metatable */ lua_rawset(L, -3); /* metatable.__index = metatable */ luaL_openlib(L, 0, METAMETHODS, 0); luaL_openlib(L, #HANDLE, METHODS, 0); lua_pushstring(L, #HANDLE); lua_gettable(L, LUA_GLOBALSINDEX); luaL_getmetatable(L, #HANDLE); lua_setmetatable(L, -2); return 1; }

Basically, these macros let you register a userdata and link it with a set of metamethod functions. A look at the file "luagraphics.cpp" from the lua player source ( would probably give you a better idea of how to use the macros than anything I could tell you.


Gah, what is it with this board? the dang [ code] tags never work properly for me

[Edited by - phantom on August 5, 2007 6:31:24 PM]

Share this post

Link to post
Share on other sites
I used the macro Merick Zero gave me, and used it like that source file used it, but I get this error:

error: expected constructor, destructor, or type conversion before '*' token

Huh? :-/

And, Barius, if I use 1 it gives out a null pointer... Also, why are all of the examples -1?

Thanks you two!

Nevermind about the error, I was using that before the declaration of my datatype... grr... But now I'm getting multiple definitions even though it's inside an include guard... o_O

Share this post

Link to post
Share on other sites
Leo, when working with the stack, positive numbers count up from the bottom (first item put into the stack), and negative numbers count back from the top (last item in the stack)

Can you explain a bit more about your problems with the macros? Unless I'm completely confused about what you mean because I don't use C, I believe that they are supposed to create multiple definitions - I.E. UserdataStubs(Image, Image*) should create two functions: toImage and pushImage

Share this post

Link to post
Share on other sites
Oooh, so that's how it works.

But I solved it, I had to use tolua_tousertype(...) instead.

Thanks very much! :-)

(P.S: You said Lua Player? PSP owns! :-D)

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this