Why does my "For each" keep going

Get help using Construct 2

Post » Tue Jan 10, 2012 2:44 am

I am trying to make an array which will generate random numbers for all of it's values and display those values one at a time in a text box, one second between showing each. My array is a 4X9. At first, I used:
>For each XY Element
        >set value at(Array.CurX, Array.CurY) to random(100)
To show the results, I used:
>For each XY Element
        >Set text to Array.CurValue
        >wait 1 second
What I got was a text box which flashed with random numbers, as if it was simply running the loop over and over, ignoring the "wait." I might just be dong something obviously wrong and stupid, but my understanding is that the "for each" loop will only run once for every value in an array... if anyone would be so kind as to impart their knowledge upon a lesser being, it would be greatly appreciated :)

B
4
S
1
G
1
Posts: 8
Reputation: 894

Post » Tue Jan 10, 2012 2:49 am

The 'wait' has to come before the 'set text' I believe, and you should replace the 'for each' with 'for each element', an array-only expression
B
83
S
21
G
15
Posts: 1,039
Reputation: 15,134

Post » Tue Jan 10, 2012 2:55 am

alas, I tried what you suggested and yet it still spit out numbers at lightning speed... and you're right I meant to say "for each element"
B
4
S
1
G
1
Posts: 8
Reputation: 894

Post » Tue Jan 10, 2012 3:44 am

Any loop will run its full course in a single tick (discarding the action wait).

What you could do would be to use global variable to keep the index you currently are (X and Y for the array), and in an event "Every 1 second" display the current value.
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 6,999
Reputation: 57,793

Post » Tue Jan 10, 2012 3:55 am

hmm, that's a good idea and I'm sure that would work if my whole purpose was to create a bunch of random numbers, yet it defeats the whole purpose of an array... I guess i'm really trying to put numbers into an array and having them remain constant without imputing each as separate random numbers (for that would also defeat the point of an array)... yet that might be what I end up having to do...
B
4
S
1
G
1
Posts: 8
Reputation: 894

Post » Tue Jan 10, 2012 5:42 am

You asked a timed display of the values
That's what I proposed.
The array is filled once on start of the layout.
And then you use the events "Every X second" to display the values that are stored in the array at timed intervals.

From what I understood in your first post that's what you were asking.

Running an event "For each elements" without any other condition/filter will just run continuously over time. This is how events work.

Else I may have misunderstood what your question really is. Please forgive me and please reformulate it so I finaly get it
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 6,999
Reputation: 57,793

Post » Tue Jan 10, 2012 9:51 am

See How to use the System 'Wait' action. 'Wait' does not suspend the entire event sheet - it would really not be at all useful if it did. So the events carry on running as normal (e.g. loops finish instantly), it just schedules the rest of the event to be run after a delay.

Usually what you really want to do is something like

+ For each Sprite
-> Wait loopindex seconds
-> Do something with Sprite

This spreads out each iteration one second apart. If you do this:

+ For each Sprite
-> Do something with Sprite
-> Wait 1 second

It means "run the loop instantly, wait 1 second, then do nothing".
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,580

Post » Tue Jan 10, 2012 8:49 pm

Thank you guys so much for helping me out!!! To figure out what was going on, I tried:
+For each XY element
   -> Set value at(Array.CurX,Array.CurY)to random (100)
+Every 1 second
   -> Set Text to Array.at(1,1)
yet I was still frustrated because it kept giving me different numbers for the same place in the array. then I added that pesky little "while current value is zero" after the "for each element" and instantly it gave me the same number for the same position (1,1) over and over!!!

Ashley, thank you so much for clearing that up for me, It was a really big help!
chitter2012-01-10 20:49:47
B
4
S
1
G
1
Posts: 8
Reputation: 894

Post » Wed Jan 11, 2012 9:01 am

you have to
[code]System: Start of layout
System: Foreach XY element
-> Array: set value at(Array.CurX,Array.CurY) to random (100)
Every 1 second
-> Text: set Text to Array.At(1,1)[/code]

if you don't limit your foreach to a start of layout, it will run each tick, thus reseting your value to another random(100) multiple times each secondsYann2012-01-11 09:02:34
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Wed Jan 11, 2012 7:52 pm

Ahh thank you very much!
B
4
S
1
G
1
Posts: 8
Reputation: 894

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 1 guest