Array entry removal problem

0 favourites
  • 5 posts
From the Asset Store
A minimal theme studied and built for both professional & new entries
  • I've noticed in my code that deleting array entries doesn't always seem to work.

    Since I'm new-ish to arrays, I didn't want to assume this is a bug. If anyone has a moment to test this, I've attached a file that reproduces the problem.

    It creates an array of random numbers (0-1). Then it scans the array for entries with the number 1, and deletes them. But it only deletes a few (it should delete them all), so it makes another scan, and deletes a few more (again, should delete them all).

    If you open the console you can see the results. Refreshing it a few times shows the inconsistency

    Note: There's an option in here to overwrite the array entry, instead of deleting it. Overwriting the entries works every time, but deleting the entry is inconsistent.

    Thanks for any insight!

    https://drive.google.com/file/d/11wroFtK9fc-pG82ruW2j4dWti96KhdEy/view?usp=sharing

    Tagged:

  • Without looking at the file I'm going to guess you've done for each row of the array if you find a 1 delete it, the problem with deleting a row is it moves the positions of the array so say you find a 1 at row 6 and delete it, the next row you would check would be row 7, this is now row 6 because it moved down, therefore it will seem to skip some 1s. If this isn't the problem then I can check the file later.

  • Hey, I think you're right! That would explain why overwriting works, but not deleting.

    It didn't even cross my mind. So to clear an array of certain entries, you can't write:

    ----

    Array -> For each X element

    Array -> If value at CurX = 1 ... Delete index CurX from X axis

    ----

    because the element index number is out of sync with the entries?

    I suppose you'd have to use loops and a variable that keeps up with the deleting of entries?

  • The trick is to start from the bottom of the array:

    + System: For "" from Array.Width-1 to 0

    ----+ Array: Value at LoopIndex = 1

    -----> Browser: Warn in console: "Removing entry #" & (LoopIndex + 1)

    -----> Array: Delete index LoopIndex from X axis

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Genius, that did it! Thanks to you both for the fast help.

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