lua-users home
lua-l archive

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



On 6-Aug-05, at 8:23 PM, Uli Kusterer wrote:

yes, looks like this was unnecessary, because this still doesn't change anything. Still the same error message. The code I'm running is:

for i=100,1,-1 do
    aRoom = Room.new()
    print( tostring( aRoom ) )
    aRoom = nil
end

Any particular reason you didn't choose to write:

for i = 100, 1, -1 do
  local aRoom = Room.new()
  print(tostring(aRoom))
end

-- or even
for i = 1, 100 do print(tostring(Room.new())) end
-- ;)

Leaving that aside, Room.new() must contain the following:

  RoomData *self = lua_pushuserdata(L, sizeof(RoomData));
  luaL_getmetatable(L, "PACEmaker.Room");
  lua_setmetatable(L, -2);

(with appropriate substitution of your xHungarianNamingConvention.) If you don't have this code, or something that looks like it, that's why it's not working. If you're using a lightuserdata rather than a pushuserdata, that is why it's not working (light user data don't have metatable). If none of the above is the reason, try checking the return values from luaL_getmetatable and lua_setmetatable, and perhaps you'll get a hint.

Since you mention c++ constructors, there are two common ways of dealing with the RoomData object.

The "simplest" (or at least most common) is to simply use a pointer to the actual object; the statement after self = lua_pushuserdata(...) would be *self = new Room; and RoomData would either be typedef'd or replace with Room*, depending on your style preferences.

The more elegant way, which works as long as you're ok with letting Lua manage your memory for you, is to use "placement new" syntax, in which case the self = statement would look something like this:

Room self = new(lua_pushuserdata(L, sizeof(Room)) Room(...);

One of the nice things about placement-new is that the __gc method will typically simply be an explicit call to the class destructor; furthermore, it can be omitted completely under a specific but not uncommon case, where no member variable of the class needs to be destructed. The placement expression can be supplied by a template function if you're careful; I think there is an example on the Lua Wiki.

R.