lua-users home
lua-l archive

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


Um, you certainly <can> use lightweight userdata, but why?

Why not just make NewPlayer() return a full userdata with the methods you
want.
You'll have to "box" your CPlayer pointers in the full userdata (or allocate
them in place, but storing the object pointer inside the userdata is easier
to quickly code up), but that's no big deal, AND this gives you GC
capability so that you don't have to rely on users calling a delete function
(I understand that you <may> still want them to, but it IS better to handle
GC events as a last line of defense against leaks).

As for having to "go to all the trouble of handling indexing of the methods
off the full userdata", just make a lua table of your functions (like you
were going to anyway), and set that as the __index member of the userdata's
metatable (it can even BE the metatable by just setting the __index member
of the metatable to the metatable itself).  This can be coded extremely
quickly.  When you get time to actually look around and take stock of things
I suggest you look through the code for the auxiliary library.  There is a
lot of stuff there for parameter validation and even a quickie method for
making tables of functions/methods from an array of structs.


-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br]On Behalf Of Jose Marin
Sent: Friday, January 09, 2004 7:49 AM
To: Lua list
Subject: ****SPAM**** Using lightweight userdata to deal with objects


I think that Lightweight userdata will solve my
problem.

All I need is to create functions to:
Create the object
  player = NewPlayer()
or
  player = Player:new()

this creates a Lightweight userdata, perhaps like this
  userdata = new CPlayer();
  lua_pushuserdata(userdata);

Manage the object
  player:move(dx, dy)
  player:fire(target)

Destroy the object
  player:delete()

  perhaps like this
  delete userdata;

all functions ( new, move fire, delete) are C
functions, that take the Lightweight userdata and
casts it to CPlayer*


How do this in Lua?

I thank this will be fast!


______________________________________________________________________

Conheça a nova central de informações anti-spam do Yahoo! Mail:
http://www.yahoo.com.br/antispam