How to remove a group of array elements?

0 favourites
  • 6 posts
From the Asset Store
Advanced inventory mechanics for your RPG game (Array-based). Take Items, split them, pick up them, read the description
  • Hello everyone

    There is an array (in the screenshot). The elements are constantly changing. I need to remove rows with value 0.

    When I run a loop and remove by Y index. Elements deletes incorrectly. When removed, the array is shifted. I tried different ways but they all didn't work.

    It's no problem to remove 1 element but it doesn't work in a loop.

    Tagged:

  • It's a little tricky but what I do for this is have the loop inside a function and stop the loop if the matching value is found and then run the function again.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You scared me :) I thought there was an easy way that I didn't think of.

  • Well it's kinda easy, try it out :) it should fix the skipping issue

  • You have to loop over the array backwards.

    Var i=0
    Var count= Array.width
    
    Repeat count times
    — set i to count-loopindex-1
    — array.at(i) == 0
    — — remove array at index i

    Or you can

    Var i=0
    Every tick
    — set i to 0
    
    While
    i<array.width
    — array.at(i) == 0
    — — remove index i
    — — subtract 1 from I
    — add 1 to i
  • Thanks for the advice, I figured it out. This works with pauses in the loop and with the array backwards(although there were difficulties).

    I had quite a lot of experience in PHP programming, there were no problems with arrays :)

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