How do I use Construct 2 arrays correctly?

Just started using Construct 2? Post your questions here

Post » Sun Jun 08, 2014 12:20 pm

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!
B
22
S
5
Posts: 107
Reputation: 2,354

Post » Sun Jun 08, 2014 1:46 pm

The manual does warn:
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.
You do not have the required permissions to view the files attached to this post.
ImageImageImage
B
71
S
22
G
240
Posts: 3,732
Reputation: 133,990

Post » Sun Jun 08, 2014 1:52 pm

blackhornet wrote:The manual does warn:
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.
B
22
S
5
Posts: 107
Reputation: 2,354

Post » Sun Jun 08, 2014 9:03 pm

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:
Code: Select all
0 0
0 0


And you push 1 on the X axis: (adds a new column of 1's at the end)
Code: Select all
0 0 1
0 0 1


Insert 2 at index 1 on the X axis: (adds a new column of 2's at position 1)
Code: Select all
0 2 0 1
0 2 0 1


Insert 3 at index 0 on the Y axis: (adds a row of 3's at row 0)
Code: Select all
3 3 3 3
0 2 0 1
0 2 0 1
B
55
S
29
G
19
Posts: 1,520
Reputation: 25,650

Post » Wed Feb 11, 2015 5:28 am

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?
B
25
S
4
G
2
Posts: 11
Reputation: 1,630

Post » Wed Feb 11, 2015 5:35 am

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.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Wed Feb 11, 2015 5:42 am

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.
B
25
S
4
G
2
Posts: 11
Reputation: 1,630

Post » Wed Feb 11, 2015 5:46 am

If you needed something more complex you could devise something, but for simple cases AFAIK you'd just loop and copy values.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596


Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 1 guest