[Lua] Retrieving custom-type global variable?

This topic is 3779 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

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 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 (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 [ code] tags never work properly for me

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

Share on other sites

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

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
[/edit]

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 on other sites
Oooh, so that's how it works.

Thanks very much! :-)

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

Share on other sites
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

Share on other sites

This topic is 3779 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Create an account

Register a new account

• Forum Statistics

• Total Topics
628645
• Total Posts
2984020

• 9
• 9
• 10
• 21
• 20