lua-users home
lua-l archive

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


The solution is easier than what you're thinking. If your app is using the same lua_State object, then you only need to call lua_dofile once.

Just call lua_dofile the very first time you want to load the script into your app. At that point, any globals the script creates will be globals in your app's lua_State object. At this point, you can call the script's getData1 and done functions as many times as you want without ever having to call lua_dofile again. The count variable will also be a global stored in your lua state, and won't get reset to zero every time.

In this approach, your script is "active" all the time simply by creating some global variables (values and functions) - these globals still live in the lua_State object after you call a function in the script.

Now the tricky part: I'm guessing that your app may want to be able to load and execute different scripts at different times. In that case, if each script defines a getData1 function, and each defines it as a global, they'll step on top of each other. The only getData1 left will be the one from the last script you loaded using lua_dofile.

One way to fix this would be to require each script to have a unique name. For example, if a script file is called "foo.lua", then it might define a function called "foo_getData1" instead of just plain "getData1". That way each script has its own version of getData1, and your app just needs to call the correct one depending on which script is being used at the time.

The problem is that the script might have a global variable (as in count
above). The app calls getdata1 & passes some data (we are ignoring it in
this example) to the script. At some point the app finds & calls the 'done'
script function - but count will always be zero (I think) because the script
is executed via a dofile call each time the app needs to get the appropriate
function.

I think the script needs to be running (or active at least) all the time -
probably in another thread? so that it can manage local memory, variables
etc based on the data which is passed to it by the app. How would I do this
(assuming its the correct approach). In that scenario, how does the app get
the function to call?

Anyway, i hope there is a simple existing lua solution....

help!:-)

Andy