Working with 2 dimensions array

  • 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...) ;):

    for i from 1 to 10

        for j from 1 to 5

            do something at(i,j)

    How can you do the same with Construct 2 ? (Since there's only one loopindex when you use a 'For each' loop)

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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!

  • 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 ?"...

  • 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+loops

  • 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.capx

  • 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

  • Something new learnt!

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