Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Removing an element from an array

Author
Message
george++
AGK Tool Maker
17
Years of Service
User Offline
Joined: 13th May 2007
Location: Thessaloniki, Hellas
Posted: 2nd Jun 2016 10:46
What is the correct way to remove an element from an array from within a loop.
Is this correct?
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 2nd Jun 2016 11:35
That looks good to me. You are looping through all the elements in the array, but only removing index 1. You could replace that entire code with: myarray.remove(1) to do the same job (unless of course you intend doing other processing within the loop).
V2 T1 (Mostly)
Phone Tap!
Uzmadesign
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 2nd Jun 2016 12:28
You need to do it like this...



Otherwise you will get an array out of bounds if you delete an element.
Your code could actually be reduced to this, based on your logic...

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 2nd Jun 2016 12:52
Quote: "Otherwise you will get an array out of bounds if you delete an element."


I did a quick test for that, and the For Next loop checks myarray.length at each iteration preventing an out-of-bounds (at least in my test, which I though was quite cool!).



V2 T1 (Mostly)
Phone Tap!
Uzmadesign
Digital Awakening
AGK Developer
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Sweden
Posted: 2nd Jun 2016 13:03
Quote: "I did a quick test for that, and the For Next loop checks myarray.length at each iteration preventing an out-of-bounds (at least in my test, which I though was quite cool!)."


That's what C++ would do, so it makes sense to me. Also you should be able to change the values it's checking anyway so again, makes sense.
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 2nd Jun 2016 14:09
I would check the array.length once and move it into a variable, then process the elements backwards.
Otherwise, you are repeating a function call in every loop to get the length.
For a lot of array elements the difference could be significant.

Geeky fact alert: I don't know if it is the case for PC technology, but on IBM iSeries servers it is also quicker to populate and read an array backwards, due to the way it is stored.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
george++
AGK Tool Maker
17
Years of Service
User Offline
Joined: 13th May 2007
Location: Thessaloniki, Hellas
Posted: 2nd Jun 2016 14:27 Edited at: 2nd Jun 2016 14:32
Quote: "
unless of course you intend doing other processing within the loop
"

I need to keep track an array of bullets and remove one each time a bullet explodes
I think, but I am not sure, it can be:


or better
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 2nd Jun 2016 14:50
You don't need the dec i in that code.
Digital Awakening
AGK Developer
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Sweden
Posted: 2nd Jun 2016 15:54
Quote: "I would check the array.length once and move it into a variable, then process the elements backwards."


That would be the fastest way of doing it. And especially when coding in Tier 1 you should do it this way if you are running the code each frame for a large array.
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 3rd Jun 2016 03:27
if you just want to clear the array you could do this;
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 3rd Jun 2016 04:49
Do you mean this, which makes more sense...



Otherwise, as mentioned, you can directly delete the array element without the loop.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
george++
AGK Tool Maker
17
Years of Service
User Offline
Joined: 13th May 2007
Location: Thessaloniki, Hellas
Posted: 3rd Jun 2016 05:04 Edited at: 3rd Jun 2016 05:17
@BatVink: Yap! you are right
I need the loop because cannot think any other way to update all the bullets.
Imagine a loop like this (pseudo code):

What are you using to update many similar sprites, like bullets, in the main game loop?
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 3rd Jun 2016 06:05 Edited at: 3rd Jun 2016 06:06
my bad
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 3rd Jun 2016 07:01
I think it would be faster to simply clear the array value, rather than delete the element. As soon as you start deleting and adding elements you run the risk of having to allocate and deallocate memory. It would be better to have an array big enough to hold as many active bullets as you anticipate having. When you "delete" a bullet, move the last bullet in the array into the element you have just freed up, so you are always checking only the first array elements, and always know where the next available element is to add a new one.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
george++
AGK Tool Maker
17
Years of Service
User Offline
Joined: 13th May 2007
Location: Thessaloniki, Hellas
Posted: 3rd Jun 2016 12:31
@BatVink: You gave me ideas, thanks a lot!

Login to post a reply

Server time is: 2024-09-29 15:30:17
Your offset time is: 2024-09-29 15:30:17