Removing a Type object from a list

BlitzMax Forums/BlitzMax Beginners Area/Removing a Type object from a list

blackwater(Posted 2010) [#1]
Hello everyone, I came across an interesting problem that I can't figure out. I have a list that contains several Type objects, and whenever I remove one of those objects, it always removes the first one in the list regardless of which one I removed (for example, if I remove the third one, it still removes the first one). Here is my function, edited to make it easier to read. Can anyone tell me where I'm going wrong?





* I create a local object of my type and go through the list I created

For Local obj:SceneObjectType = EachIn ObjectList

* here is where I go through some checks to see if the object matches and it's the one I want to delete

If x >= xorigin And x <= xend And y >= yorigin And y <= yend

* here I remove the object from the list
ListRemove(ObjectList, obj)

* And I redraw the canvas

RedrawGadget(canvas)
End If


Next


Perturbatio(Posted 2010) [#2]
I would suggest you create a link field in your types and then simply use obj.link.remove() instead (it's faster and will ensure you're removing the correct one).

The laser beam code that I posted in the thread below this one uses this.

Last edited 2010


GfK(Posted 2010) [#3]
My first thought would be that you're not adding objects into the list correctly, and every object in the list is in fact an instance of the same object.


Jesse(Posted 2010) [#4]
are you by any change doing a custom sort for items in the list?
it has a know bug.


Czar Flavius(Posted 2010) [#5]
I agree that using the link method is a better way, but your code should work, and if it doesn't you may have a bigger problem. As Gfk says, you may be adding the same object into the list.


blackwater(Posted 2010) [#6]
Mystery solved!

@ Jesse, as a matter of fact I am doing a custom sort, once I commented it out (located in the type declaration) the deletion worked fine

But I need to do a sort so I tried the link method and that worked just as well, thanks guys!


Czar Flavius(Posted 2010) [#7]
Are you overriding the Compare method?

You can make a custom Sort function, and pass that as a parameter to list's sort method.