lua-users home
lua-l archive

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


Hmm... shouldn't this work:

var
  TestObj : ^TTestObj;
begin
  TestObj := lua_newuserdata(luaVM, sizeOf(TTestObj));
  TestObj^.property := 500;   //    <--- this causes an exception
end;

What am I doing wrong?

Also, I think I figured out how to use metatables for exposing
functions (eg. lua code:    myObj:setSize(10)), but how do I expose
properties?  (for example, in lua code:  myObj:size = 10)

Thanks again for all of your help!



On 12/8/05, herviou <herviou@enib.fr> wrote:
> hello,
> 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 http://www.lua.org/pil/28.1.html 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...)
>
> dh
>
> 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?
> >
> >Thanks!!
> >
> >
> >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;
> >>  end;
> >>  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.
> >>
> >>
> >>
> >>
> >>
>
>
>