How do I have an array of arrays?

Get help using Construct 2

Post » Thu Aug 31, 2017 12:56 pm

Hi, all!

Array and Dictionary objects are quite nice, but I have a need for basically having an array of arrays - the arrays are two dimensional and can get rather sizable - say, 10x300 each. The catch is that these arrays are dynamically generated by Construct 2 and there is no preset amount.

In theory I could use a three dimensional array, but that comes with the problem of adding or deleting rows of data - changing one will have an impact on all of them, unless I have misread the documentation.

Ideally it would be something that's a nice list of Arrays, like [Array 1][Array 2][Array 3] and lets you insert and delete members just like the regular array does. Also saving and loading the contents to JSON is necessary.

Using the search I didn't really find a directly suitable solution. Is there some way to do this with the regular objects or perhaps there is a plugin that does this? Thanks!
B
6
S
1
G
1
Posts: 14
Reputation: 883

Post » Thu Aug 31, 2017 3:27 pm

Well, if you need a simple list of arrays, you could create multiple instances of an array type and treat each instance separately according to its IID (or preferably, an instance variable you set yourself).
B
75
S
31
G
27
Posts: 1,000
Reputation: 20,735

Post » Thu Aug 31, 2017 3:30 pm

Hmm, hadn't thought of an array object the same way as the "usual" objects... That would require some small utility function to change their "index" variables when deleting or adding one in the middle, but that doesn't sound too hard.

Thanks for the idea, I'll look into it!
B
6
S
1
G
1
Posts: 14
Reputation: 883

Post » Thu Aug 31, 2017 3:45 pm

Hello Everyone,
This is Sophia and newly register with your community. I am glad to see that some of the knowledgeable people are helping out. Glad to be a part of this lovely group.
Array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.
B
2
Posts: 1
Reputation: 152

Post » Thu Aug 31, 2017 4:49 pm

MollyMya Thank you for your input
B
256
S
39
G
15
Posts: 1,983
Reputation: 41,036

Post » Thu Aug 31, 2017 5:24 pm

However, it's not true in a Construct 2/3 context. Arrays aren't typed and are also dynamic.
B
75
S
31
G
27
Posts: 1,000
Reputation: 20,735

Post » Sat Sep 02, 2017 8:01 am

Hi again!

Perhaps you can help me out a little more - I used @Magistross suggestion to have dynamic arrays and made a little "flipbook" test - basically you have "pages" (arrays of coordinates) and you can add them and flip between them.

Here's a demo capx - http s : //files. fm/u/prtvuhm8#_ (Please remove spaces as I'm new here - it's just a file hosting service, one of the cleanest I have seen).

The problem part is on the bottom, where we try to save and load to files - saving seems to work ok - I'm using a Dictionary to sequentially add all the arrays to it using their index as key.

But loading seems to not load anything, although it sounds like it should. Probably I'm just using the data format wrong. Any suggestions?
B
6
S
1
G
1
Posts: 14
Reputation: 883

Post » Sat Sep 02, 2017 7:11 pm

You need to use AJAX to load the file. The FileChooser.FileURLAt(0) expression will only give you the URL that AJAX can use to load the file.

It seemed to somewhat work when I added AJAX file loading, however something was not quite right with the first page. Did not investigate further, you seem pretty capable, you will surely figure it out on your own. :)
B
75
S
31
G
27
Posts: 1,000
Reputation: 20,735

Post » Sun Sep 03, 2017 11:23 am

Thanks again! Everything is fixed now! Since I'm planning to use NW.js I just added the nice NWjs.ReadFile function and it all works out!

First page was not working correctly, because I had added array saving to page changes, but not before saving the file - so wherever you would save just being on an untouched page the array would be empty (or represent an older version of that "page").

One small "bug" still left is that if we have an empty page upon adding that page its array is already there, so we get one [0,0] junk entry. But in real life the array would have more data, some of which would never be 0 so I can just check for that. Or I could add a function that only adds an array on the first click, if needed.

Edit: Some strange behavior, but perhaps I just don't understand sequence of events or such. Anyhow, I have a " On open dialog OK" event that triggers the loading of a JSON file into a Dictionary and from there into Arrays. It seems to work, but for some reason when in the same set of actions I call the function to redraw the sprites based on array contents it just does not fire at all.

If you are willing to have a look it's right at the bottom here, under the last comment:

https ://files. fm/u/3zpa9n6k#_ (Still need to remove space, it seems).

Any clue why the function does not work there? It works for all other things we do, like flip pages, delete or add them etc.
B
6
S
1
G
1
Posts: 14
Reputation: 883

Post » Sun Sep 03, 2017 6:52 pm

It's the classic case of "newly created objects can't be picked until the next top-level event". This old post from R0J0hound sums it up.

What happens is that no array "exists" when the Retrieve function is called after the loading is done. The quickest and dirtiest trick to circumvent this problem is to add a "Wait 0" just before the function call so that it get postponed to the next tick, when the newly created array will be available.

You could still do it all in one tick, but that would mean adding a lot of logic to your existing to code to now work with the UIDs of the array. Object picking by UID works even with newly created object.
B
75
S
31
G
27
Posts: 1,000
Reputation: 20,735

Next

Return to How do I....?

Who is online

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