lua-users home
lua-l archive

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

  Thanks for your reply.  Let me clarify what I'm doing.  The functions
aren't actually being passed to my C function directly, but they are already
within a table.  I'm trying to allow the script to define a class (table
containing methods and variables), register that within my host, and then I
can create as many additional instances of the table as needed.  So I
iterate the table, store away the elements, and in another routine push them
back on the stack when new instances of the class are created.

MyClass = {
  OnSpawn = function() end;
  Health = 100


  Then as entities are spawned within the game engine it will create
instances of the LUA class to handle events.

  There will be cyclical references between objects, where they call methods
in one another.  So I was concerned that the script would first have to
define each class with empty functions, then the implementation would
follow.  However I was making things too complicated - I thought the LUA
compilation phase would complain about non-existent indices to the tables,
however I now see that everything is looked up dynamically real-time.  So
allowing redefinition of the functions after registration is not a
requirement, and can be restricted if necessary.

  Dan East

-----Original Message-----
[]On Behalf Of Jamie Webb
Sent: Saturday, June 12, 2004 9:13 AM
To: Lua list
Subject: Re: Passing LUA functions in C

On Saturday 12 June 2004 13:14, Dan East wrote:
>   I've read many posts in the archive about determining function names,
> I don't know if that is even necessary in this case.
>   A LUA script passes a LUA function as a parameter to my C function.
> Later I need to push that LUA function back on the stack (or more
> specifically just a reference to the function).  It seems like this should
> be a very simple task.  Must I determine the name of the function in this
> case?  One caveat is that the function will be redefined between the time
> it is passed to my C function and when it is pushed back on the stack by
> another C function.

Ok, your first problem is that it does not make sense to talk about a
being redefined. A function just is. It doesn't have a name, and it can't be
changed once created (except for values held by closures). You can create a
different function and assign it to the same variable, but that's like
assigning 5 to a variable instead of 3: the number isn't somehow changed,
just the value held by that particular variable.

So, what you're really asking for is call by reference. Lua doesn't support
this per se, but you can get the effect using tables:

-- Create a table to use as a reference,
-- and set its initial value to my_function
ref = { my_function }

-- Pass the table to your C function
-- The C function should save the table in the registry or something

-- Note that changing the value of the my_function variable here will
-- not affect the value stored in the table.

-- Change the reference
ref[1] = my_other_function

-- Note that ref = { my_other_function } would NOT work, because that
-- would create a different table.

-- Call the C function that's going to recover the table from the registry.

-- Jamie Webb