Removing a Type object from a list
BlitzMax Forums/BlitzMax Beginners Area/Removing a Type object from a list
| ||
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 |
| ||
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 |
| ||
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. |
| ||
are you by any change doing a custom sort for items in the list? it has a know bug. |
| ||
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. |
| ||
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! |
| ||
Are you overriding the Compare method? You can make a custom Sort function, and pass that as a parameter to list's sort method. |