newbie: parameterised scripts?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

newbie: parameterised scripts?

Paul Dixon
(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


Reply | Threaded
Open this post in threaded view
|

RE: newbie: parameterised scripts?

Tony Wetmore
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:

#include <stdio.h>
#include "lua.h"

int main(int argc, char* argv[])
{
   int ret;
   lua_Object testFunction;

   ret = lua_dofile("test.lua");
   printf("Back from lua_dofile(), ret = %d\n", ret	);

   printf("Getting TestFunction()\n");
   testFunction = lua_getglobal("TestFunction");
   if (testFunction)
   {
      printf("Going to lua_callfunction(TestFunction)\n");
      ret = lua_callfunction(testFunction);
      printf("Back from lua_callfunction(TestFunction), ret = %d\n", ret);
   }

   return 0;
}

Here is my test Lua script:

--
-- test.lua
--
-- A program written to test Lua and figure out it works.
--

print('Starting global execution space.');

function TestFunction()
   print('--> TestFunction');
   print('<-- TestFunction');
end

print('Back to global execution space.');


Reply | Threaded
Open this post in threaded view
|

RE: newbie: parameterised scripts?

Paul Dixon
In reply to this post by Paul Dixon
Thanks for your help. That seems like the cleanest way to go about things!

Regards,

Paul

<<application/ms-tnef>>

Reply | Threaded
Open this post in threaded view
|

Re: newbie: parameterised scripts?

Tomas-14
	Hi Paul

	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_pushnumber (x);
	lua_pushnumber (y);
	lua_pushnumber (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)
   else
      return 0			-- The C function expects an integer
   end
end

	Which will call another Lua function...

	Tomas

Reply | Threaded
Open this post in threaded view
|

Re: newbie: parameterised scripts?

Luiz Henrique de Figueiredo
In reply to this post by Paul Dixon
>From [hidden email] Mon Apr  6 13:31:34 1998
>
>(I hope newbie questions are OK.....)

sure.

>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.
--lhf