lua-users home
lua-l archive

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


Hi Wes

You can use an __index function in the object metatable to transform a function name into an argument of a generic function.

You might also want to look at the ObjCbridge for Lua, which does the same kind of thing that you're trying to do very well for Objective- C: http://www.pixelballistics.com/Wiki/index.php?page=LuaObjCBridge

G

On Sep 7, 2006, at 5:55 PM, Wesley Smith wrote:

Hi,
I'm working on integrating some C code with Lua. The C code is
dynamically bound, so I'm able to do things like the following:

void *obj = obj_new("name_of_obj");
obj_method(obj, "draw");
obj_method(obj, "scale", 2, [0.5, 0.5]);
...

The above C code will allocate an object, then call the object's draw
method, and then set the object's scale parameter.  What I'd like to
be able to do with Lua is the following

obj = Obj.new("name_of_obj");
obj:draw()
obj.scale = {0.5, 0.5}

(please excude my lua syntax if it's wrong, I'm still learning}.

In the Lua code, I'm calling the draw method and then setting the
object's scale property to a pair of x, y values.  Now, somehow I need
to translate obj:draw() into Lua such that Lua actually calls a
generic function that passes the name of the actual function that was
called as a parameter.  If I were to explicity write this in Lua, it
would look like:

obj:method("draw")  However, I'd like to have the more convenient
notation of obj:draw() map to this instead.  I'm sure there's a way to
do this in Lua, I just can't seem to figure it out right now.  Any
ideas?

thanks,
wes