How do I use Wait within a For Each loop?

Get help using Construct 2

Post » Wed Aug 03, 2016 7:42 am

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.
B
50
S
23
G
17
Posts: 291
Reputation: 12,432

Post » Wed Aug 03, 2016 7:45 am

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.
Mistakes were made.
B
49
S
24
G
107
Posts: 1,581
Reputation: 60,303

Post » Wed Aug 03, 2016 1:39 pm

You can use the Timer behaviour on each sprite, just with a different delay on each one, set in order.
ImageImageImage
B
71
S
21
G
221
Posts: 3,649
Reputation: 124,227

Post » Wed Aug 03, 2016 5:40 pm

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,
B
19
S
4
Posts: 32
Reputation: 1,044

Post » Thu Aug 04, 2016 2:49 am

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.
B
50
S
23
G
17
Posts: 291
Reputation: 12,432

Post » Thu Aug 04, 2016 3:43 am

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

Etc

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

Etc

This would probably be better actually.

~Sol
Tired of crappy file hosts that are crappy? Get DROPBOX - https://db.tt/uwjysXJF
Moderator
B
81
S
34
G
40
Posts: 3,031
Reputation: 30,422

Post » Thu Aug 04, 2016 6:32 am

That sounds like it'd work, I'll try it out and report back if I get any problems. Thanks!
B
50
S
23
G
17
Posts: 291
Reputation: 12,432


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 10 guests