How do I use Wait within a For Each loop?

0 favourites
  • 7 posts
From the Asset Store
SynthWave Loop Pack includes 68 seamless loops, founded on 11 original melodies.
  • If it's even possible. Or, how can I insert a delay within a For Each loop that works for each object?

    The premise: I have several sprites moving in a chain, the logic for which is basically the same as in the default Pin Chain Style example. I want to make it so the leader sprite shoots, then after a short delay the 2nd sprite in line shoots, then the 3rd, then the 4th, etc. thus creating a sort of "stuttering" volley of shots. I've been trying to make this happen by making the sprites spread a value (a "shoot" signal) amongst themselves using a For Each loop, starting from the leader and working down the line, but I can't seem to figure out how to delay said value's spread. Right now all the sprites effectively shoot at the same time because the value spreads instantly.

    I tried using the Wait function but this doesn't seem to repeat itself within the loop. Instead it waits X number of seconds at the very start, then runs the loop continuously without ever Waiting again. I was hoping it would repeat the Wait on every pass, so that each object had to wait a while before shooting. How can I do this (with or without the Wait function)?

    I mean, maybe I'm coming at this from the wrong angle, so if anybody has a better solution I'm all ears.

    Thanks for any help.

  • You usually don't want to use wait in loops.

    Try every x seconds along with a state variable "active".

    Every x seconds, state is active - fire, set next sprite to active.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • You can use the Timer behaviour on each sprite, just with a different delay on each one, set in order.

  • You need to put stg like this as the 1st line of the loop cycle:

    System -> Wait -> 0.01*loopindex

    This fires the loop in every 0.01 sec

    Hope this helps,

  • Thanks for the advice guys, I had varying degrees of success but it seems the more complex the project got the more trouble I was having with the loops. Right now I've got a kinda ghetto setup where each sprite spawns a "breadcrumb" after an action, so that the next sprite in line moves over it, does the same action and spawns another crumb for the next sprite, etc. Surprisingly it's giving the best results (visually) out of all my other attempts, even if it's kinda inelegant. Oh well, I'll see what I can do.

  • You can also have a set of functions...

    Call 1st function

    --- Shoot

    --- Wait 0.2 seconds

    --- Call function 2

    Function 2

    --- Shoot

    --- Wait 0.2 seconds

    --- Call function 3


    Being functions though, it may cause un-intended results.

    You could do the same thing without funtions, if you use a variable to control which thing is shooting...

    [GlobalVar.ShootOrder = 0]

    [Left Mouse Clicked]

    --- Shoot

    --- Wait 0.2 seconds

    --- Set ShootOrder to 1

    [ShootOder = 1]

    --- Shoot

    --- Wait 0.2 seconds

    --- Set ShootOrder to 2


    This would probably be better actually.


  • That sounds like it'd work, I'll try it out and report back if I get any problems. Thanks!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)