(I hope newbie questions are OK.....)
The way I would like to use Lua is to have a script that just contains functions, and have my controlling application (with is Windows GUI based) invoke those functions in response outside events.
One way to do this would be to load a script into memory and append the required function call to it, then pass it to lua_dostring (although it looks like lua_dostring was only indented for small scripts - my test app had to save the altered script to a temp file and use lua_dofile).
Another way would be set some global variables before calling the script, such that the 'body' of the script read the variables and called the appropriate function.
Are there other ways? After a call to lua_dofile on a script with no body, can I use lua_callfunction to select the desired function?
If anyone else is using Lua in a similar way, I'd love to hear what technique is preferable and why.
Thanks in advance,
Paul Dixon [SMTP:[hidden email]] wrote:
> After a call to lua_dofile on a script with no body, can I use
lua_callfunction to select the desired function?
When I was experimenting with Lua, this is exactly what I did. Any code
outside of the functions was executed during the lua_dofile(), while the
functions were simply defined for later reference via lua_callfunction().
Here is the main() driver source from my initial prototype -- the output
that is generated demonstrates how Lua's global (outside function)
statements are executed during the lua_dofile() call, but the code inside
function definitions is not:
int main(int argc, char* argv)
ret = lua_dofile("test.lua");
printf("Back from lua_dofile(), ret = %d\n", ret );
testFunction = lua_getglobal("TestFunction");
printf("Going to lua_callfunction(TestFunction)\n");
ret = lua_callfunction(testFunction);
printf("Back from lua_callfunction(TestFunction), ret = %d\n", ret);
Here is my test Lua script:
-- A program written to test Lua and figure out it works.
print('Starting global execution space.');
print('Back to global execution space.');
you can load the script with all your functions at the very beginning
of your application (just after the initialization of Lua libraries). After
that you have to associate each event to a C function that will call the
corresponding Lua function.
For example, this function can be associated to the "mouse click" event:
int mouse_click_event (int x, int y, int buttons)
lua_callfunction (lua_getglobal ("mouse_click"));
return lua_getnumber (lua_getresult (1));
The corresponding Lua function will be called:
function mouse_click (x, y, buttons)
local obj = objects_list:pick_object (x, y)
if obj then
return obj:mouse_click (buttons)
return 0 -- The C function expects an integer
Which will call another Lua function...
>From [hidden email] Mon Apr 6 13:31:34 1998
>(I hope newbie questions are OK.....)
>Are there other ways? After a call to lua_dofile on a script with no body, can I use lua_callfunction to select the desired function?
yes, that's the best thing to do.