[Lua] Retrieving custom-type global variable?

Started by
5 comments, last by Merick Zero 16 years, 8 months ago
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!
Advertisement
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 (http://luaplayer.org/staticpages/index.php?page=20070708120150241) would probably give you a better idea of how to use the macros than anything I could tell you.

*edit*

Gah, what is it with this board? the dang tags never work properly for me<br><br><!--EDIT--><span class=editedby><!--/EDIT-->[Edited by - phantom on August 5, 2007 6:31:24 PM]<!--EDIT--></span><!--/EDIT-->


If you want the top item of the stack, you need to use the index -1, not 1.


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!

[edit]
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
[/edit]
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
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)
Well it's ok, but if I'd known about it at the time I probably would have bought a GP2X instead of a psp

This topic is closed to new replies.

Advertisement