for loop help.

Hello guys

I ve this code

for i = 1 , 8 do print(i) end

in this way the Sys returns me back 1 2 3 4 5 6 7 8 simultanely.

But my task is to print these numbers every 2 sec… How can I implement a timer in this ForLoop

Okay. Your problem here is that you need to really understand event programming first. If you were programming in sequential coding - things like the old Home Computer BASICs - you would write something like.

for i=1,8 do print(i) delay(2) end

but with event driven programming you don’t work like that. What you do is have an event in the program - which could be clicking on something, a timer elapsing, a collision happening etc. and responding to that, and you sort of ‘synchronise’ all these events together to make the program work.

If you are (doing) say a countdown the normal method would be to create a timer (look at timer.performWithDelay which waits a while and then does something) and print out each time.

But honestly, rather than solve this precise problem I would read up on event programming first.

Yes I know about listener…

My bad english probably made everything more complicated… With “timer” I meant that I was simply searching for something that allowed me to print each numbers every 2 seconds using the For loop instead of print every numbers instantanely

That’s the point ; you can’t. You have to do something like - fire a timer 8 times, each time printing one more than last time. 

I know - I think - what you want to do. The problem is that you have to ‘think events’.

Oh I got it.

Sorry then xD… Thanks for the help

You could do it like this, but it’s going to be more efficient to set up a timer that repeats 8 times. 

[lua]

for i=1,8 do

  local delayIt = function ()

     print(i)

  end

   timer.performWithDelay(2000*i, delayIt)

end

[/lua]

thanks it worked…

Okay. Your problem here is that you need to really understand event programming first. If you were programming in sequential coding - things like the old Home Computer BASICs - you would write something like.

for i=1,8 do print(i) delay(2) end

but with event driven programming you don’t work like that. What you do is have an event in the program - which could be clicking on something, a timer elapsing, a collision happening etc. and responding to that, and you sort of ‘synchronise’ all these events together to make the program work.

If you are (doing) say a countdown the normal method would be to create a timer (look at timer.performWithDelay which waits a while and then does something) and print out each time.

But honestly, rather than solve this precise problem I would read up on event programming first.

Yes I know about listener…

My bad english probably made everything more complicated… With “timer” I meant that I was simply searching for something that allowed me to print each numbers every 2 seconds using the For loop instead of print every numbers instantanely

That’s the point ; you can’t. You have to do something like - fire a timer 8 times, each time printing one more than last time. 

I know - I think - what you want to do. The problem is that you have to ‘think events’.

Oh I got it.

Sorry then xD… Thanks for the help

You could do it like this, but it’s going to be more efficient to set up a timer that repeats 8 times. 

[lua]

for i=1,8 do

  local delayIt = function ()

     print(i)

  end

   timer.performWithDelay(2000*i, delayIt)

end

[/lua]

thanks it worked…