How do I remove and item from a 2d array and have all proceeding values shift up.

0 favourites
  • 8 posts
From the Asset Store
Customize the animation of character when item changed
  • I have a 2d array of data. A grid x and y which is 16 by 16. Each element contains some data ( a string).

    I want to remove a single value from the grid and everything after if shift up so there are no spaces. Imagine removing an entry from a list of items, you want everything to shift up and not display empty spaces.

    Because this is a 2d grid I would want everything on the same row to the right to shift left and the same for all rows below it with values moving up a row to fill spaces at the right hand side of each row.

    I have been trying to figure this out for hours with my 2d grid array but cant seem to do it. Is there no command for the array which splices them like this automatically?

    The delete seems to delete an entire axis which is not what I want. Just a single element needs to be removed.

    Note: I want the array size to stay the same (16 by 16) all the time. but ones with null/zero values be put at the end.

    Tagged:

  • Your question is confusing, how can they shift both up and left ? If you remove a value I would expect the values below it in the column to move up and that's it.

  • Do you mean something like this?

    1 2 3
    4 5 6
    7 8 9

    Say you remove the 2.

    1 0 3
    4 5 6
    7 8 9

    Then it shifts like this?

    1 3 4
    5 6 7
    8 9 0
  • Yes that's exactly what I mean. Apologies for the confusion.

    Overnight I had the idea that a good approach to this might be to create a temporary 16 by 16 array and then for each element in the original array; copy values across IF they are not null/zero. The new array would then be arranged as in the above example. ie no gaps in the middle.

    Testing now...

    UPDATE. Not possible because it seems there is no simple way to just push something to the end of a 2d array. ?!?

    It looks like another approach maybe to convert everything down to a 1d array, do the edits and then turn it back into a 2d array.

    Another idea is to have 16 1 dimensional arrays of 16 elements each...

    ...although I suspect this is waaay overcomplicated for what it needs to be.

    Accepting all ideas here :)

  • In the end I just used two - one dimensional arrays.

    I transferred only elements to the 2nd 'temporary array' that had values greater than 0, cleared the initial array and then transferred them back.

    I was original using the 2d array layout to dictate the visual (like a chess board) but because I switched to a 1d array instead I had to control the layout using instance ids of each array element.

    To conclude; This experience made me scared to use anything other than a 1d array in future as there seems to be very little control to pop/push over individual elements in 2d or 3d arrays .(only complete axis)

  • You can use a single 1D array and just think of it as a 2D array :)

    Make an array with width=256 and use it to store 16x16 values. If you need an entry, say, for (X=5,Y=1), it's Array.At(1*16+5)

    Or Array.At(Y*16+X)

    Then you can easily remove entries in the middle and shift the rest of the array using "pop" action.

  • You should be able to treat the 2d array as a 1d one. If 0 means empty do:

    on function “shiftArray”
    Repeat 16*16-1 times
    Array: at (loopindex%16, int(loopindex/16)) =0
    — array: set at (loopindex%16, int(loopindex/16)) to Array.at((loopindex+1)%16, int((loopindex+1)/16))
    — array: set at ((loopindex+1)%16, int((loopindex+1)/16)) to 0

    So the process would be to 1st set the array location to 0 that you’d want to delete and then run that to shift. It only shifts by one space so if you delete multiple then you have to run that the same number of times.

    I’m assuming the array starts fully shifted but if it isn’t there is a way to modify it to do that. Or a brute force way to do it is to just run the function 256 times.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks guys. Yeah, i realised I could just use a single array as soon as I posted. I guess I got my head so tied into doing multiple arrays that I forgot I could pop and push easily with a 1d array. Thanks

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