Jump to content
  • Advertisement
Sign in to follow this  

Problem returning lua object references

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, I've been trying to integrate lua into my game but I've been having some difficulties, I hope you can help me because I'm stuck.

In my code I create metatables in Lua and store them in c with


lua_call(L,0,1);//lua creates a new metatable and returns it

int luaRef = luaL_ref(L,LUA_REGISTRYINDEX);

newTable is a function that creates a new metatable and returns it, then I call it and get the reference from the stack and store it in luaRef

Once I have this reference, if I need to call a function  of this metatable from c I do:

lua_getglobal(L,name.c_str());//Get the table name

lua_getfield(L,-1,"function");//set the function from the table

lua_rawgeti(L, LUA_REGISTRYINDEX, luaRef);//set the table object

lua_call(L,1,0);//call the function

And it works. My problem is that when I try to push this reference back to lua so that I can call these functions I get a

attempt to index field 'object' (a 'nil' value)

My code is

int Scripted::LgetObject(lua_State *L){


    lua_rawgeti(L, LUA_REGISTRYINDEX, tmp.luaRef);

    return 1;


and from Lua:



As I understand, the avobe C code pushes de table into the stack, but all lua receibes is a nil value.

I've been trying to solve this for days and I don't know what to do next, please point me in the right direction

Share this post

Link to post
Share on other sites

Going on with the problem, if I change my function to this:

lua_rawgeti(L, LUA_REGISTRYINDEX, tmp.luaRef);
luaL_checktype(L, -1, LUA_TTABLE); 
return 1; 

Checktype says that a lua table is on top of the stack, but if I print this return value from lua it says it is a nil value

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!