Types - Best way to remove item from type without getting out of bounds error?

Started by Amon, April 01, 2019, 00:51:42

Previous topic - Next topic

Amon

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?

Qube

Quickest way to remove all items is :


ItemList.length = -1


QuoteIf 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 :


For x = ItemList.length To 0 Step - 1
   If ItemList[ x ].x > 999
      DeleteSprite( ItemList[ x ].id )
      ItemList.remove( x )
   EndIf
Next
Mac Studio M1 Max ( 10 core CPU - 24 core GPU ), 32GB LPDDR5, 512GB SSD,
Beelink SER7 Mini Gaming PC, Ryzen 7 7840HS 8-Core 16-Thread 5.1GHz Processor, 32G DDR5 RAM 1T PCIe 4.0 SSD
MSI MEG 342C 34" QD-OLED Monitor

Until the next time.

Amon

Now that is what I needed. Cool.
I wasn't aware that doing ItemList.length = - 1 clears the whole list.
Thanks, Qube. :)

MikeHart

Hehe i recomment to rtfm.  :P 
Seriouls, agk has an awesome api. To bad they killed classic.


plenatus

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.