Buttons to control a complex function.

I’m quite new to Corona and lua but really enjoying exploring a great programming context.

I have a long complex function called “play” which prints short phrases with timed pauses in between - a sort of live instruction feed appearing on the standard output. (I say “long complex function” because it’s not just working it’s way through an array: it has various if-thens, repeat untils/ nested functions)
I’ve tested this and it works fine.

I now want to make it interactive - the user will press various onscreen buttons to affect what is happening.
So I wrote a function called “getCommand” which gathers any commands from buttons and puts them in a buffer. I did this very simply by making each buttons touch event put a different string into a variable lastButton.

When I first ran my updated program, the screen stayed black. I’m guessing that because my program ran “play” which called “getCommand” and “getCommand” was sitting in a loop waiting for something to appear in lastButton, the program never got to processing display refreshes and so the screen stays black.
Does that sound like a plausible explanation?

I come from a Visual Basic Background and in VBA you can use “DoEvents” to give the screen or any other operating system routines some space to do some processing. So I wondered if there was an equivalent in Corona but couldn’t find anything?

So…
I thought one possibility would be to rewrite “play” as a state machine but this would be too complex - I’d end up with loads of functions and loads of setting up and clearing eventlisteners which would be pretty unreadable.

I then found section 9.3 of PiL called “Coroutines as Iterators” and thought great! - whenever “play” needs to get input from a button, use coroutine.yield. And then whenever a button is pressed use coroutine.resume.
Does that sound like a sensible approach?
Or am I missing something about the way corona works?
Just want to check I’m thinking about this in a sensible way.
Any help/guidance would be appreciated.
In particular any documentation/articles about the App lifecycle and how screen refresh/touch handling is interleaved with a running function would be useful.

Thanks
Suhada
[import]uid: 67842 topic_id: 12839 reply_id: 312839[/import]

@Suhada,
you will have to change your way of thinking a bit here. DoEvents was a good way to let the system idle for a bit. However if you look at the Javascript way of doing things, you will realise that it uses timers. While there is no DoEvent equivalent in Corona, Coroutines are available in Lua, they are not in CoronaSDK.

So you might have to use timers to run after a pre-defined interval to give you a similar thing as DoEvents

[lua]timer.performWithDelay(DELAY,FunctionToCall)[/lua]

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 12839 reply_id: 47142[/import]

jayantv - thanks for your reply.
I’m a little confused though about a couple of things:

You said:
“if you look at the Javascript way of doing things”.
Did you mean lua or am I missing something?

You also said:
“Coroutines are available in Lua, they are not in CoronaSDK.”
But I’ve written a lua program with coroutines and it runs fine on the simulator. Are you saying it won’t run on an android or iphone hardware?

Suhada :-S
[import]uid: 67842 topic_id: 12839 reply_id: 47370[/import]

I did mean JavaScript, it was to suggest let us say there was no lua, how is the same thing handled in another language.

If you got co-routines to work for you, that’s good. Last time I tried a couple of versions ago, I could not get that to work, did not know that it is now working.

Cheers,

?:slight_smile: [import]uid: 3826 topic_id: 12839 reply_id: 47392[/import]