0 Favourites

Array delete failing

  • Hi everyone,

    as the subject suggests, I am having trouble with the array object. I cannot report this as a bug since the problem only happens with a huge project I am working on, it has a lot of events, loops and some other stuff, however, the array loop and delete part is not tied to this stuff but I suppose the overall performance of this project, somehow, is affecting the delete part. I have and array of width 20 and height 2, depth 1. First, I fill the array with Infinity values, then I fill the the array with values depending on the activation of certain functions, lastly, when certain event occurs, I delete all the rows that have a the infinity value, when I do this, the rows containing Inifinity values are deleate but only partially. For example, 4 out of 40 of the array´s elements are filled with arbitrary values, the rest the values are filled with inifiny value, then the array deletes only 20 values o 10 rows that have the infinity value, so there are 8 rows with infinity value and 2 rows with the arbitrary values used before. I tried executing the delete loop 2 and 3 times in a row, and the elements were deleted successfully ,that way I could confirm is more of a performance problem more than a programming one (it is just a supposition), if I were having wrong code or something, the values in the array would never be deleted or would be a delete mess, but values are being deleted just that a slow pace if you will. I would really appreciate if you could help me with this one, thank you.

  • It sounds like you are deleting from the array while looping through it.

    Imagine you want to loop through an array deleting any "A"s:

    array = ["A", "A", "B"]

    index = 0:

    it's an "A" so delete it

    array = ["A", "B"]

    now the "A" that was at position 1 has moved to position 0.

    index = 1:

    that's a "B"

    index = 2... done!

    array = ["A", "B"]... you missed an "A"

    Is that the problem you have?

  • it sounds like it, however, I did a test with a smaller project and there all the elements were deleted without a problem, however, if what you posted is the problem, do you have any suggestion?, and yes, I am deleting the elements from the array at the same time a iterate through it.

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • The solution is to loop backwards, which you can't do with the array 'for each' but you could with a for loop. Or you can loop through the array storing the indices of the elements you want to delete in another array and then delete them in reverse order.

  • Thank you man, it was quite easy to solve the problem using a system for loop backwards.

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