This topic is now archived and is closed to further replies.

How do you create a new Lua function...

This topic is 5016 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post

Link to post
Share on other sites
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

-- add other functions or variables
return object

-- example
newObj = 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).

Share this post

Link to post
Share on other sites