Working with 2 dimensions array

Get help using Construct 2

Post » Fri Nov 04, 2011 10:59 am

There's something I can't get to work : 2d arrays.
How can you build two loops (one inside the other) to access (X,Y) cells in an array ?

In other languages, you can do something like that (complete pseudocode...) ;):

[quote]
for i from 1 to 10
    for j from 1 to 5
        do something at(i,j)
[/quote]

How can you do the same with Construct 2 ? (Since there's only one loopindex when you use a 'For each' loop)Pode2011-11-04 10:59:28
B
33
S
9
G
6
Posts: 709
Reputation: 6,704

Post » Fri Nov 04, 2011 11:03 am

There's a handy (yet somewhat hidden!) expression to access the index of the outer loop when inside a nested loop. You can say loopindex("name of loop") to access the index of a specific loop. You should be able to do what you want to do using that!
B
20
S
9
G
6
Posts: 607
Reputation: 6,112

Post » Fri Nov 04, 2011 11:16 am

@SullyTheStrange : Wow ! Nice trick :).
It's very helpful. I'm always asking myself the question, when I stumble on a dev block inside Construct 2 : "If it doesn't work, is it because I'm not wrapping my head around C2 way of doing things, or am I right in my trying ?"...
B
33
S
9
G
6
Posts: 709
Reputation: 6,704

Post » Sun May 13, 2012 12:12 am

I had to check around a bit to find how to name a loop; to save others some time I figured I add to this thread:

You can only name System "For" loops (or at least, that's the only one I figured out); when you choose the "For", the first field allows you to name it.

There's an example in this thread: http://www.scirra.com/forum/grid-of-sprites_topic49074.html?KW=nested+loopsceharold2012-05-13 00:14:25
B
5
S
1
G
1
Posts: 2
Reputation: 746

Post » Sun May 13, 2012 1:52 am

I just posted an example yesterday for someone showing how to get random values out of 1 dimensional, 2 dimensional and 3 dimensional arrays. Uses nested loops to insert values into the 2 and 3 dimensional arrays.

GetRandomArrayValues.capxkittiewan2012-05-13 01:52:56
B
29
S
11
G
12
Posts: 281
Reputation: 11,888

Post » Mon May 27, 2013 11:11 am

Hi,

Oh, wow! this is very helpful ... as i am trying to do an end-less loop with an array content, where each array element needs to pause for about 45 second before displaying the next element or cell.

I will try to work on this ...

Thank you for the sample.

Cheers!

Vins
B
5
S
1
Posts: 20
Reputation: 545

Post » Sat Jan 03, 2015 9:56 am

Something new learnt!
B
4
Posts: 4
Reputation: 208


Return to How do I....?

Who is online

Users browsing this forum: Yahoo [Bot] and 25 guests