lua-users home
lua-l archive

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


Russell Y. Webb wrote:


On 15/07/2005, at 3:09 PM, Ive wrote:

In my .lua file I got the following:

actor = CreateActor("ActorCubeName");
primitive = CreateCube("CubeName", 1, 1, 1);
ActorSetPrimitive(actor, primitive);
...
now... I ll rather do something like this:

actor = CreateActor("ActorCube");
primitive = CreateCube("CubeName", 1, 1, 1);
actor->SetPrimitive(primitive);


Assuming actor is a table (if it it's then your need to use a table), try this instead of your last line above:

actor.SetPrimitive = ActorSetPrimitive
actor:SetPrimative(primative)

Otherwise,
a = { }
a.ptr = actor
a.Set = function(a, prim) ActorSetPrimitive(a.ptr, prim) end
a:Set(primative)

Basically x:y(...) expands to x.y(x, ...)

Hope that helps. I may have missed the point since your actor->xyz looks more like C++ than Lua.

Russ

Hi,
I have done such jobs last week, exactly to do the same (wrapping C++ methods to lua and have an object style notation in Lua). I have choosen an other solution (more complicated than the one above) in which you can have inheritance (on the lua side) of your wrapped c++ class. To do so you need (in c++) to create metatable for your wrapped c++ class (see http://www.lua.org/pil/16.html) in which you will store all the methods you need to wrapp (here you will have to use lua_newuserdata instead of lua_lightuserdata). You will also need to set 2 special field in this metatable to enable the object style notation and also the garbage collection of this share object when needed. Those fields are __index and __gc. This solution is a little bit tricky (see the online documentation for help) but is not without creating problems. I explain: when you use lua_lightuserdata to push your object pointer this is easy to have the equality in lua (same pointer means equality) but when you use lua_newuserdata lua create a new memory block in which you will store your ptr (encapsulate your object ptr in a struct) and the next time you will share the same object ptr with the help of lua_newuserdata you will have another memory block (not the same)... so, the equality will not be possible easily... The solution is to create a metatable in which you store the table create with lua_newuserdata with a unique key that is your object pointer. So the next time you will need to share an object you just have to look in this metatable for the key and get the value (the table of your already shared object).

With all this hard work, you will be able to write in Lua without adding anything else:
actor=CreateActor:new("ActorCube")
primitive = Primitive:createCube("CubeName", 1, 1, 1);
actor:setPrimitive(primitive);

and you will also be able to look for methods of your actor (a c++ class wrapp!!) by doing:
mthd = getmetatable(actor)
for i in pairs(mthd) do
   io.write(i.."\n")
   if i=="setColor" then
       primitive:setColor(1,0,0)
   end
end
Groovy baby...

Have a look on the online documentation to learn more about the object oriented programming in lua, also have a look on the user wiki. But you have to know that it will take some times to build a system like the one above: you will also have to build a look up system for you c++ class and so on and so forth. Have a look on tolua++.

Hope that helps.

dave
begin:vcard
fn:David HERVIOU
n:HERVIOU;David
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
email;internet:herviou@enib.fr
title;quoted-printable:Centre Europ=C3=A9en de R=C3=A9alit=C3=A9 Virtuelle
tel;work:+33 (0)2 98 05 89 47
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.
url:http://www.cerv.fr/~herviou
version:2.1
end:vcard