How to delay between iterations of a for loop!

I have the following issue.

I need to spawn 4 characters at 1 second time difference. I have set a for loop to spawn them but I can’t seem to put the delay with timer.performwithdelay in it. Can anyone suggest how to do this? [import]uid: 8192 topic_id: 2330 reply_id: 302330[/import]

How do you do it now? Can you post that piece of code? [import]uid: 5712 topic_id: 2330 reply_id: 7125[/import]

Here you go. Let me give you a preface.

I have multiple lots of ships. These ships are in tables with their coordinates. Some have 4 some more. So I spawn a lot of ships. Each ship should have a delay of being spawned by “tempoHalf” which is my time variable. Once all of them are spawned it goes to the nextlot().
My tables are called: lt1, lt2 etc.
I am having a problems in two spots. I labeled them in the code.

Thanks for your help.

function spawnLot (lotCounter)  
 lot = "lt"..lotCounter -- I am having problems here. I don't think my syntax is right  
 local lot = "lt"..lotCounter  
 onScreen =#lot  
 for n=1,#lot do  
 x,y = lot[n][1],lot[n][2]  
 x=x+25  
 y=y+16  
 ship.new(x,y)  
 --Need to put a time delay  
 --timer.performWithDelay(tempoHalf, ship.new(x,y) ,onScreen)  
 end  
 nextLot ()  
end  

[import]uid: 8192 topic_id: 2330 reply_id: 7127[/import]

Anyone else on this? I am really stuck. [import]uid: 8192 topic_id: 2330 reply_id: 7149[/import]

Sorry, I had no time to look into this. Maybe tonight when I am back from work. [import]uid: 5712 topic_id: 2330 reply_id: 7160[/import]

No worries Mike. Whenever you can. :slight_smile: Thanks [import]uid: 8192 topic_id: 2330 reply_id: 7162[/import]

Ok, checked your code. First about dynamically building the table name. I found no way of doing this. What I would do is to store the tables inside a table and index it from there.

Next the event listener of your timer. When you add it with a parameter, the timer doesn’t repeat. And I think this is by design. Eventlistener for other fucntionalities can’t have a parameters either. [import]uid: 5712 topic_id: 2330 reply_id: 7185[/import]

@MikeHart Thanks for your reply.
So you mean create another table of tables and then index it from there. That should allow me to dynamically changing the name?

I lost you on the event listener. I don’t have one on the code. Man I am lost.

Make the for loop wait s seconds and then continue. Isn’t there a break function or can’t I send the loop to another function and then return to it after s time? [import]uid: 8192 topic_id: 2330 reply_id: 7187[/import]

No you can’t change the name. But as you give an index anyway (lotCounter) you can use that to select the index of the table that stores the other tables (Lt1, LT2).

With event listener I ment the function name you want to use in your timer.perform. There you can’t set parameters.

And no, you can’t pause a loop. If you do that, your whole game pauses. Everything you do in your code manually, runs within one frame. Doing something automatically other several frames need timers or transitions.

OR… look up coroutines. These allow you to jump out and then jump in at the same point at will. [import]uid: 5712 topic_id: 2330 reply_id: 7189[/import]

Hey amigoni!

I’m running into the same problem here. I’ve got a table with several colors values. I have to highlight some buttons in the same order, so I’m making an iteration over the table to get the colors and run a “highlight” function.

Problem is that the for goes at full speed ( :smiley: ) and all my buttons are getting highlighted at the same time. I need the same. A way to delay the execution of the next loop iteration.

Any luck? [import]uid: 36639 topic_id: 2330 reply_id: 30296[/import]

Well for starters, don’t use a for loop. As mike explains, the game will freeze until the for loop completes.

Instead, you can set the number of iterations for your delay timer:
http://developer.anscamobile.com/reference/index/timerperformwithdelay [import]uid: 12108 topic_id: 2330 reply_id: 30301[/import]

Oh, thanks!

I’ve been struggling with this problem for days (or weeks) and now it works like magic.

:smiley: [import]uid: 36639 topic_id: 2330 reply_id: 30308[/import]