Delay

I am trying to do something and I really don’t know wherever it is possible at all or not. Well… In C language we can use delay() function within a loop so each iteration can be delayed by some time. I know codea doesn’t work like other languages and everything needs to be put in draw function to display. So I m just wondering is it possible to do in codea to do the similar effect as in C? May be its a wiered idea… Maybe not.

Codea is evented. Look at ElapsedTime.

Function draw()
   If (ElapsedTime <5) then drawtitlepage() else drawmainapp() end
End

I guess the different approach here is to ask whether you really want it to do nothing for 5 seconds or you are really after something like ‘draw this screen for 5 seconds, then do something else’.

Another way to look at it is - if you want to “do nothing” for 5 seconds - you still want to draw the screen, right?

The trick is to think of it as a TV screen (refreshing every fraction of a second) rather than a chalkboard (where what you draw sticks). If you want “nothing to happen” - draw the same thing.

I’ll give an example here what I wanna do.
For example I have an array of number and I wanna show the values of array and each value will display after 2 sec where the previous displayed value is still on the screen.

Arr = {1,2,3,4,5}
for i=1,#arr do
     text(i, 300,i*50)
end

Above code will display all 5 values at the same time and will remain in the screen all the time.
What I am trying to do is display the first value and then display the second values after few sec but first value will be in the screen in between the times.

I can do it within the draw function using if and else and some comparison but my query is how can I implement it using for loop if it’s possible.

so, like a countdown (or up, I guess)?

function setup()
   elementstodisplay = 1
   timeout = ElapsedTime + 3
   Arr = { 1, 2, 3, 4, 5 }
end

function draw()
   if (ElapsedTime > timeout) then
      timeout = ElapsedTime + 3 -- 3 more seconds
      elementstodisplay = elementstodisplay + 1
      if (elementstodisplay > 5) then elementstodisplay = 5 end
   end
   for i=1 to elementstodisplay do
      text(Arr[i], 300, i*50)
   end
end

There’s another interesting concept for delays here using coroutines: http://stackoverflow.com/questions/5128375/what-are-lua-coroutines-even-for-why-doesnt-this-code-work-as-i-expect-it

but I’m having trouble wrapping my head around it.

Thanks Bortels :slight_smile: I think that piece of code is a good example.