How do I - Arrays with For Loops Help

Get help using Construct 2

Post » Wed May 07, 2014 5:47 am

I read Ashley's comment in this post that helped w/ 1 issue (viewtopic.php?f=147&t=66070&p=563719&hilit=For+loop+not+pausing#p563719) but having a similar / separate issue.

I build an array that corresponds to each level... the level shows a light and plays a sound. I've tried several ways to make this work, but I'm missing something (I think prob very similar to the issue mentioned in the post listed above).

Here is my example of what happens (and pic of my most recent logic).

Level 1 adds the color Red. Level 2 adds Purple. Level 3 adds White. So, my array is pretty simple:

0 Red
1 Purple
2 White

But, when I "play" the array (the lights/sound), I'm only getting the last entry. What happens is that my "Play" function will play Red for level 1. For level 2 it will play Purple, Purple. For level 3 it will play White, White, White.

What I want to happen is: Level 1 will play Red. Level 2 will play Red, Purple. Level 3 will play Red, Purple, White... and so on.

I've tried the various .CurValue, .CurX, loopindex, etc. It feels like the same issue that Ashley talked about... just not sure how to get past it. Any help is greatly appreciated.

Thanks.
Stephen
You do not have the required permissions to view the files attached to this post.
B
20
S
6
Posts: 27
Reputation: 1,878

Post » Wed May 07, 2014 5:53 am

It looks like your trying to access Array_Color from inside a function that is in the foreach loop. I dont beleive the instance reference for Array_color persists through the function call. try it in the original foreach usage and see if that works
B
20
S
7
G
1
Posts: 221
Reputation: 2,077

Post » Wed May 07, 2014 6:09 am

Thx for reply. I might be missing where to put it... I've added more of my "logic" (which might be the wrong way to do it). My thinking is that when I hit the Play Button (for my testing purposes), it will "build array" meaning add 1 to the CurrentLevel, add the color to the array stack... then, I want all of the lights/sounds to play (i.e. if there are 10 items, it will loop through all 10).
You do not have the required permissions to view the files attached to this post.
B
20
S
6
Posts: 27
Reputation: 1,878

Post » Wed May 07, 2014 6:19 am

Im not sure what choose() function is returning .. but i think i see it... the MySounds for loop does not loop through the array keys its just a standard for loop. you would need to do a Array_Color "foreach element" on Axes X for it to return the CurVal of what was stored in X
B
20
S
7
G
1
Posts: 221
Reputation: 2,077

Post » Wed May 07, 2014 1:21 pm

I've tried the Array_Color "foreach element" as well... but, it only plays the last sound... I'm thinking it might be so fast and just ends on that last color/sound. The Choose() function just builds the array w/ a random color... which does work (screen pic of my Array).

ArrayHelpv3.png


Here is my logic using the Array "for each element"

ArrayHelpv4.png


Thanks for your help. Seriously. I must be missing something simple here but just can't figure it out.
You do not have the required permissions to view the files attached to this post.
B
20
S
6
Posts: 27
Reputation: 1,878

Post » Wed May 07, 2014 2:34 pm

Try to use wait system-function at the end of "for each x".
B
16
S
5
G
4
Posts: 74
Reputation: 3,311

Post » Wed May 07, 2014 2:53 pm

Tried that. But, here is the update. Press btnPlay first time adds a color (i.e. Red), appends the CurValue to the Text, and plays the Red light/sound. Press btnPlay second time adds color (i.e. Green) to the array, appends Red Red to the already Red to the Text, and plays the Red light/sound once.

I'm feeling pretty stupid here.
You do not have the required permissions to view the files attached to this post.
B
20
S
6
Posts: 27
Reputation: 1,878

Post » Wed May 07, 2014 9:56 pm

The system loops (for/repeat) work with loopindex. The array loop (for each element) works with Array.CurX, Array.CurValue etc. You can't mix the two and use Array.CurValue with a for loop or use loopindex with an array loop.

When you use a value after x seconds, you need to think what will the value be at the time the action is run.
If you have
Code: Select all
set value to 5
wait 10 seconds
set text to value

then the text will be set to whatever value is after the 10 seconds - it's not necessarily 5 any more.
If you're using loopindex or Array.CurValue then it certainly won't be the value you want after x seconds.

So maybe a different approach like:
Code: Select all
every x seconds [or on timer]
if array is not empty
    get and remove first value from array


or if you want to keep the array intact:
Code: Select all
global number index = 0
every x seconds [or on timer]
    color = array.at(index)
    add 1 to index
You do not have the required permissions to view the files attached to this post.
B
55
S
29
G
19
Posts: 1,520
Reputation: 25,670


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 50 guests