Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to make lua script wait/pause/sleep/block for a few seconds?

Tags:

lua

I cant figure out how to get lua to do any common timing tricks, such as

  • sleep - stop all action on thread

  • pause/wait - don't go on to the next command, but allow other code in the application to continue

  • block - don't go on to next command until the current one returns

And I've read that a

while os.clock()<time_point do  --nothing end 

eats up CPU time.

Any suggestions? Is there an API call I'm missing?

UPDATE: I wrote this question a long time ago trying to get WOW Lua to replay actions on a schedule (i.e. stand, wait 1 sec, dance, wait 2 sec, sit. Without pauses, these happen almost all in the same quarter second.) As it turned out WOW had purposely disabled pretty much everything that allows doing action on a clock because it could break the game or enable bots. I figured to re-create a clock once it had been taken away, I'd have to do something crazy like create a work array (with an action and execution time) and then register an event handler on a bunch of common events, like mouse move, then in the even handler, process any action whose time had come. The event handler wouldn't actually happen every X milliseconds, but if it was happening every 2-100 ms, it would be close enough. Sadly I never tried it.

like image 444
MatthewMartin Avatar asked Jun 23 '09 18:06

MatthewMartin


People also ask

How do you make a Lua script wait?

wait() in lua function wait(seconds) local start = os. time() repeat until os. time() > start + seconds end --Exactly the same as the roblox one!

Is there a wait function in Lua?

The Lua wait is a function used when multiple processes simultaneously and want the primary process to stop for a given time. It is a method useful for stopping or delays the parent process until the child process is not working completely.

How do you comment on Lua?

A comment starts with a double hyphen ( -- ) anywhere outside a string. They run until the end of the line. You can comment out a full block of code by surrounding it with --[[ and --]] . To uncomment the same block, simply add another hyphen to the first enclosure, as in ---[[ .


1 Answers

[I was going to post this as a comment on John Cromartie's post, but didn't realize you couldn't use formatting in a comment.]

I agree. Dropping it to a shell with os.execute() will definitely work but in general making shell calls is expensive. Wrapping some C code will be much quicker at run-time. In C/C++ on a Linux system, you could use:

static int lua_sleep(lua_State *L) {     int m = static_cast<int> (luaL_checknumber(L,1));     usleep(m * 1000);      // usleep takes microseconds. This converts the parameter to milliseconds.      // Change this as necessary.      // Alternatively, use 'sleep()' to treat the parameter as whole seconds.      return 0; } 

Then, in main, do:

lua_pushcfunction(L, lua_sleep); lua_setglobal(L, "sleep"); 

where "L" is your lua_State. Then, in your Lua script called from C/C++, you can use your function by calling:

sleep(1000) -- Sleeps for one second 
like image 149
zslayton Avatar answered Sep 19 '22 12:09

zslayton