2

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.

3
  • The answer was actually pretty simple, although not efficient. I created a function in C++ with a busy wait, and inside the busy wait I ran the functions to update the GUI. I'd still like to hear if anyone has a more efficient answer though. Commented Jan 8, 2011 at 18:27
  • why would you want to sleep the script? what do you want to accomplish with it? Commented Jan 10, 2011 at 9:47
  • Sorry for the late response. I'm just trying to make a simple Sleep() function that only affects the lua script, not the entire program. Basically, the lua script will use C++ created functions that look like CreateWindow(texture,x,y,width,height) RotateWindow(45) etc. I'm making the Sleep() function so that if I or the end user wanted a pause between Creating the Window and Rotating the Window, we could do it without interrupting the entire program. Commented Jan 12, 2011 at 15:30

2 Answers 2

3

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...
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.