July 19, 2019, 01:29:45 AM

Author Topic: Types - Best way to remove item from type without getting out of bounds error?  (Read 210 times)

Offline Amon.

  • Full Member
  • ***
  • Posts: 114
  • What? There's no ceiling outside?
    • Amon.Pro
Code: [Select]
type Item
    id as integer
    x as float
    y as float
endtype
Global ItemList as Item[]

for x = 0 to 100
    local it as Item
    it.id = x
    it.x = x + 10
    it.y = 0
    ItemList.insert(it)
next
Taking a look at the above, what would be the best way to remove all items or just some?

If I did ItemList.remove(it) it removes the item but still gives me out of bounds errors when I try accessing the list again. Ideally I'd like to be able to store sprite id's etc in the type and not only remove the sprite id but delete the sprite also.

Can any help be provided?
Windows 10 Pro - 32GB DDR4 RAM - GEFORCE RTX 2070 8GB - AMD RYZEN 7 8 CORE - WATERCOOLING.

Offline Qube

  • Administrator
  • Hero Member
  • *****
  • Posts: 1961
Quickest way to remove all items is :

Code: [Select]
ItemList.length = -1

Quote
If I did ItemList.remove(it) it removes the item but still gives me out of bounds errors when I try accessing the list again.
If you are removing items in a for / next loop then always iterate backwards :

Code: [Select]
For x = ItemList.length To 0 Step - 1
   If ItemList[ x ].x > 999
      DeleteSprite( ItemList[ x ].id )
      ItemList.remove( x )
   EndIf
Next
Until the next time...

Offline Amon.

  • Full Member
  • ***
  • Posts: 114
  • What? There's no ceiling outside?
    • Amon.Pro
Now that is what I needed. Cool.
I wasn't aware that doing ItemList.length = - 1 clears the whole list.
Thanks, Qube. :)
Windows 10 Pro - 32GB DDR4 RAM - GEFORCE RTX 2070 8GB - AMD RYZEN 7 8 CORE - WATERCOOLING.

Offline MikeHart

  • Hero Member
  • *****
  • Posts: 588
  • Cerberus-X Dev Team
    • Cerberus X
Hehe i recomment to rtfm.  :P 
Seriouls, agk has an awesome api. To bad they killed classic.


Offline c0d3r9

  • Full Member
  • ***
  • Posts: 121
I hope AGK Studio donĀ“t mutate to an unity thing.
The new IDE have some very useful features.
I will test the scene editor the next days and i hope its not to much at all, but at last there is no need to use it.
Laptop: Win10@64bit - i3 2x2Ghz - 8GB Ram - 1TB HDD
Desktop: Win10@64bit - AMD Ryzen 5 2400G - MSI B450 Tomahawk - 8GB Ram - 240GB SSD