Advertisement Jump to content
Sign in to follow this  

is it possible to break/continue a lua script from Engine side?

This topic is 3652 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Reason is I don't want to execute the behavior routines of every agent on concurrently. Instead agents are executed round-robin like. For now I'm writing the Lua behavior scripts in a fashion so that they terminate themselves asap for example like this: -check what action we executed in the last frame -continue that action, for example update position along path, maybe fire -return That makes the scripting a bit unintuitive in my eyes. If Lua had an possibility to halt a running script from engine/C++ side (maybe after 5ms) and continue it in the next cycle for another 5ms, the scripting would me bit easier I guess. Is there something like that? Or Id I misunderstand something completely? Thank you in advance!

Share this post

Link to post
Share on other sites
Short answer, yes. Look at the Debug library and lua_sethook() method.

Longer answer:
While this can be accomplished within your engine and application it should be avoided at all costs except when debugging source code. Calling the Debug library GREATLY slows down the execution of a script.

Psudo Pascal:
For debugging, you will need to enable the Lua Debug Library:

Setup a flag for the debug call to test against:
var FScriptRunning : Boolean = true;

Create a method for handling the calls on every line executed:
procedure lua_debug(L : Plua_State; ar : Plua_Debug); cdecl;
if not FScriptRunning then
plua_pushstring(L, 'Script execution halted by user.');

Register the method:
lua_sethook(L, @lua_debug, LUA_MASKCALL or LUA_MASKRET or LUA_MASKLINE, 0);

This will force a closure to execute on EVERY script line, thus slowing your scripts execution time down greatly (I've seen up to 5x slower).

If that doesn't help, read a bit in the manuals and online references about the Debug library :)

Share this post

Link to post
Share on other sites
Proably a better way would be to do a line count and once a threshold is reach then create the corotuine and break exectuion at that point, this will be faster but you'll still inccure the penalty of the debug hooks.

It's similar to early attempts are preemptive threading in Lua, people did this using a modified Lua VM in the past but with the current Lua incarination it's possible to do it in the debug hooks. However if it's usually not recomeneded to expose the debug functioanlity to wild scripts, so you'll have to do some work to prevent unauthotized access to the debug library.

There have been threads discussing using co-rotuines for each enttiy which are then exectued by a scheduler, which meets your needs more so. Though there is no guarentee of exectuion time, as it's a cooperative threading model. Usually this isn't problem, however malacious/broken scripts can block your programs exection. You can get around this several ways, but it further complicates the system ( ie use debug hooks to do line counts as before, but terminate execution instead of break exection if it is exceeded, or run the VM on 2nd thread, terminate it if 2nd thread is locked, etcc.. )

Good Luck!


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!