开发者

Sleep Lua script without halting entire program?

I'm writing a GUI that's meant to be easily customizable by 开发者_开发百科the end-users. The functions are in C++ and are called from Lua. I'm trying to make a Sleep() type function that will pause the script but not the program itself.

I was able to get it working by using threads and making one for each function. However, I want it to be an individual function. As in, instead of having it part of the CreateButton function and every other function, simply having a Delay or Sleep function that only halts the script, not the entire program.

Me being a novice at Lua, I really don't know how to go about this. Any help is appreciated.


I'd look into making a state machine using coroutines and message passing. Treat each button push like a c++ string that gets passed into coroutine resume. You can then build a little state machine that switches on the message. You can then do some UI work and then put the coroutine back to sleep till something sends it another message.

This is pretty handy if you have a state machine that does UI.

pseudo code:

c_obj:wait_for_message("mouse_down");
local message = coroutine.yield();
if(message == "mouse_down") then
  update draw function.
end
c_obj:wait_for_message("mouse_up");
local message = coroutine.yield();
if(message == "mouse_up") then
  Update UI..
  update draw function.
end 
etc...


To make your busy-waiting solution more efficient, how about using select() or similar to wait for some GUI events to process, rather than spinning? It seems like something you would need to do in the GUI regardless of the scripting side of things.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜