Jump to content
  • Advertisement
Sign in to follow this  
rammel

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

This topic is 3497 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
Advertisement
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:
luaopen_debug(L);

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;
begin
if not FScriptRunning then
begin
plua_pushstring(L, 'Script execution halted by user.');
lua_error(L);
end;
Application.ProcessMessages;
end;

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!

-ddn

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!