How do I have an array of arrays?

  • 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!

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

  • 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!

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

  • MollyMya Thank you for your input

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • However, it's not true in a Construct 2/3 context. Arrays aren't typed and are also dynamic.

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

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

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

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

  • Magistross thanks, man, you are a lifesaver! I had put a wait in there for a test, but guess it wasn't in the correct position. All works as it should now.

    This was one of the biggest things for my project and it's great to see it working just as expected (actually better).

  • MollyMya You are not a bot aren't you?

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