lua-users home
lua-l archive

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


On Jul 17, 2006, at 2:44 PM, Nick Tourte wrote:
This seems like a simple thing to do, but I can't seem to get it to work or find the answer. You can call dofile in a script to load and execute the main body of another script, and you can call loadfile to load the main body into a function and run it upon a function call; but I can't seem to find a way to call a function that is registered in another such script. I have also tried using require, but I don't know enough about lua packages to properly set it up and call the function. How do you go about calling a function from another script in a running script? Is there a way through Lua directly? Routing it thru c++ isn't an option, because it's not always known what user types have to be pushed onto the stack (we're using a lot of different types).

Creating a global variable that points to a function object, including creating the function literal, is itself code that must be run in order to result in a function object that can be used.

[Sliver:~/Desktop] gkistner$ cat foo.lua
function foo( )
        print( 'yay' )
end

[Sliver:~/Desktop] gkistner$ lua
Lua 5.1  Copyright (C) 1994-2006 Lua.org, PUC-Rio
> =foo
nil
> dofile( 'foo.lua' )
> =foo
function: 0x309780
> foo( )
yay

Trickier people than me may be able to tell you how to change the environment so that dofile() fills out a different global, in which you may look for your function as a key.