Array delete failing

Discussion and feedback on Construct 2

Post » Tue Feb 24, 2015 8:29 pm

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.
B
10
S
3
Posts: 75
Reputation: 864

Post » Tue Feb 24, 2015 9:08 pm

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?
B
55
S
29
G
19
Posts: 1,520
Reputation: 25,610

Post » Tue Feb 24, 2015 9:15 pm

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.
B
10
S
3
Posts: 75
Reputation: 864

Post » Tue Feb 24, 2015 9:19 pm

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.
B
55
S
29
G
19
Posts: 1,520
Reputation: 25,610

Post » Tue Feb 24, 2015 9:36 pm

Thank you man, it was quite easy to solve the problem using a system for loop backwards.
B
10
S
3
Posts: 75
Reputation: 864


Return to Construct 2 General

Who is online

Users browsing this forum: technofou and 4 guests