I am trying to embed a lua-based script system into my game engine. I would like the scripting to be able to have both blocking and non-blocking commands, for example:
character.walkTo(24, 359); // Blocks until character arrives
c = 35; // Non blocking, execution goes on to the next statement
Since the "walkTo" needs to be "active" for more than 1 frame of execution, I would like to be able to run 1 statement at time from the Java host instead of the whole function. This is because it would be overkill to have real multithreading, which is not needed.
If I could execute just 1 statement, and keep the execution state "paused" until next statement execution, I would be able to implement blocking commands like "walkTo" by checking if the command is finished in the host, and if it is, go on to the next statement, otherwise, wait until the next frame iteration.
Is there any way to execute 1 statement a time from the Java host with LuaJ(or with any other Lua api), or am I forced to develop my own script engine with lex and yacc?
Any good idea is welcome, thanks!
Seems like you are missing asynchronous pattern. If
c=35
has to be executed oncecharacter
occurs at(24,359)
, then the correct way is to passfunction() c=35 end
as third argument towalk
method and your engine (that performs actual 'walking') will call that callback when appropriate.Otherwise,
walk
may schedule walking to engine and yield immediately, resuming on correct event. In this case you have to setup script worker-coroutine (you cannot yield in main state).You may cancel the script anytime with
script=nil
.yield() has some limitations, consult the manual.