0 Favourites

How do I use Construct 2 arrays correctly?

  • Hi,

    I consider myself a fairly advanced Construct 2 coder and have spent the past week programming some fairly advanced functions, yet for some reason I can't get arrays to work right even after reading the documentation. I spent all morning trying to set up a 6x2x1 array, and it just won't come out right.

    Can somebody take a look at my capx and tell me what I'm doing wrong?

    https://dl.dropboxusercontent.com/u/317 ... ytest.capx

    Much appreciated!

  • The manual does warn: [quote:i3kluqiv]The actions in the Manipulation category (e.g. Push, Pop) allow one-dimensional arrays to be used like other data structures. (These actions work with multidimensional arrays, but are intended for the one-dimensional case.)

    I wouldn't use the push/insert operations on a multi-dimensional array. This example just sets them explicitly. You may want to use a dictionary, to index by name.

  • The manual does warn: [quote:3gzohxnx]The actions in the Manipulation category (e.g. Push, Pop) allow one-dimensional arrays to be used like other data structures. (These actions work with multidimensional arrays, but are intended for the one-dimensional case.)

    I wouldn't use the push/insert operations on a multi-dimensional array. This example just sets them explicitly. You may want to use a dictionary, to index by name.

    Yeah but I need to create complex hierarchies of data and contain it all within one JSON file. The Dictionary really isn't ideal for that.

    The way I'm reading it the manual states that push and pop does work with multidimensional arrays, so I fail to see what the issue is.

  • When you push/pop/insert/delete you're adding or removing an entire row/col to the array. The value you insert is used as the value for every new element.

    For example, if you have a 2d array:

    0 0
    0 0[/code:1kxxfbo6]
    
    And you push 1 on the X axis: (adds a new column of 1's at the end)
    [code:1kxxfbo6]0 0 1
    0 0 1[/code:1kxxfbo6]
    
    Insert 2 at index 1 on the X axis: (adds a new column of 2's at position 1)
    [code:1kxxfbo6]0 2 0 1
    0 2 0 1[/code:1kxxfbo6]
    
    Insert 3 at index 0 on the Y axis: (adds a row of 3's at row 0)
    [code:1kxxfbo6]3 3 3 3
    0 2 0 1
    0 2 0 1[/code:1kxxfbo6]
  • Sorry to necro post to this but what you're saying is if I want to move a row with unique values in each column from one array to another, I need to move it one value at a time?

  • Unique values or not, you can't exactly move a row anywhere, you will just have to copy the values across from one array to another.

  • I was thinking if the array depths were the same, you could just push a whole row from one array to the other. Not that it's tough to iterate over a row and copy the values individually.

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • If you needed something more complex you could devise something, but for simple cases AFAIK you'd just loop and copy values.

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