Advertisement

How do you create a new Lua function...

Started by March 23, 2004 07:35 PM
1 comment, last by hmon007 20 years, 10 months ago
from within another Lua function? To illustrate what I mean: function func1() hostFunction() -- This host C function wants to create a -- new Lua function end Is it possible to create a new Lua function from here without doing something like: lua_dostring( L, "function newFunc() end" ); btw, the reason I want to create new functions from within other functions is so that I can dynamically create object interfaces in Lua. For instance, I would call a registered host function like createObject() and createObject() would define all the Lua functions needed for the interface to that object. Thanks in advance!
You can use luabind
or toLua or some other wrapper that can help you with this.

From within a Lua script it''s straight forward to create
new functions:

function returnObject()   object = {}   object.execute = function (self, x, y)      -- do something   end   -- add other functions or variables   return objectend-- examplenewObj = returnObject()newObj.execute(newObj, 12, 20) 


If you want to register a new C or C++ interface you will have to
use Lua''s stack mechanisms to register the function name within
the Lua state (see Lua docs for that).
Advertisement


Hey thanks darookie!

My program does what I need now!



cheers!

This topic is closed to new replies.

Advertisement