lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


The manual and book are only confusing me more and more and my love/hate realtionship with Lua simmers on, hopefully somebody can clarify this for me... I have a table with values plus a hidden one that contains the name of an alias to the table. People write scripts in lua using the alias, I know the real table name. I want to delete both and make sure that both the table and alias are garbage collected.

a7b45j3 = { a=1, b=2, alias=BossTank }
BossTank = a7b45j3

1. How do I delete from C side a7b45j3 and alias so that they are both garbage collected? Currently I'm trying to do it like this:

char table = { "a7b45j3" };
char field = { "alias" };

 // get table
 lua_pushstring(L, table);
 lua_gettable(L, LUA_GLOBALSINDEX);
  // get field
 lua_pushstring(L, field);
 lua_gettable(L, -2);
// the string "Tank" is now at top of stack, push nil
 lua_pushnil(L);
 lua_settable(L, LUA_GLOBALSINDEX);
// this should have changed Tank = a7b45j3 to Tank = nil in the globals table?
// now remove external reference from table
 lua_pushstring(L, field);
 lua_pushnil(L);
 lua_settable(L, -3);
// this should have removed the field alias from the table?
// now delete the table, first get it off the stack
lua_pop(L,1);
// put up the name, nil and set it back to globals
 lua_pushstring(L, table);
 lua_pushnil(L);
 lua_settable(L, LUA_GLOBALSINDEX);

// DONE! ??????

2.  Do I have this correct? Do I need to set the values to nil, and _then_ the symbols?

Frustrated,
Brett