lua-users home
lua-l archive

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

I'm not using delphi (c++ instead), but the idea is the same. When using a lua_newuserdata(luaState,pObject) you actually ask lua to allocate a memory block and a metatable is created for your data. If you take a look at you will see that userdata doesn't have table, but only metatables. So the only way to add methods to that data (or object) is to set fields in the metatable of this object. Also have a look at chapter 28, which describes very well what you are looking for (sharing mecanism...)


John Klimek wrote:

Thanks for the help!  I'm using Delphi so I really appriciate the Delphi code!

I understand how to create userdata (lua_newuserdata(luaState,
pObject)), but I don't understand metatables or how (or why) you set
methods to manipulate the object.

Can you possible give me a full example of an object and then how you
expose it's methods and properties to lua?


On 12/8/05, Dušan Majkić <dmajkic@sezampro.yu> wrote:
1) Create an object in C++ (or Delphi) and share it with lua.
 Create new userdata, attach metatable with methods you want to be
 accesible from lua. Push that userdata to lua state.

 Note that I'm using Delphi.
 (And still searching a way to staticaly link lua)

 I use this userdata for all delphi objects:

 TObjectData = record
   Obj: TObject;
 PObjectData = ^TObjectData;

 Then you could set __gc method in metatable to call Obj.Free
 Or you dont't. If you want your object to live after lua_close().

 I do lua binding manualy. Delphi does not include RTTI with every
 class and there is no preprocesor - so tools like tolua or swig.

2) Create a variable in C++ (or Delphi) and share it with lua.
 You can do it in two ways.

 First is to push your variable value to lua state, execute some
 lua code and then return value to your C++/Delphi variable.

 Second is to push your variable as userdata with metatable, and
 handle changes to it as they happen, in whatever way you like.

fn:David HERVIOU
org;quoted-printable:Ecole Nationale d'Ing=C3=A9nieurs de Brest
adr;quoted-printable;dom:BP 38;;25, rue Claude Chappe;Plouzan=C3=A9;France;29280
title;quoted-printable:Centre Europ=C3=A9en de R=C3=A9alit=C3=A9 Virtuelle
tel;work:+33 (0)2 98 05 89 45
tel;fax:+33 (0)2 98 05 89 79
note;quoted-printable:Doctorant En Informatique au laboratoire LISyC (Laboratoire d'Informatiqu=
	e des Syst=C3=A8mes Complexes) dans le cadre du projet AR=C3=A9Vi.