deleting objects from lists
BlitzMax Forums/BlitzMax Beginners Area/deleting objects from lists
| ||
how would you go about deleting an object from a list that may represent an enemy that has been destroyed? |
| ||
tlist.remove |
| ||
Or keep the TLink which is returned when you add it to the list in the first instance. Then it's Link.Remove() |
| ||
I can't seem to get the tlist.remove method to work, what am I doing wrong?Type point Global _list:TList=New TList Method New() Add(_list) End Method Method Add(t:TList) t.Addlast(Self) End Method Field x:Int, y:Int 'the point's coordinates Field selected:Int 'if the point is part of the current selection End Type this is the part of the code where points are deleted Function Delete_Selection() For p:point=EachIn p._list If p.selected=1 p._list.remove(p) EndIf Next End Function |
| ||
this is the part of the code where points are deleted It's probably not a good idea to remove from a list while you are iterating through it. FYI |
| ||
It's probably not a good idea to remove from a list while you are iterating through it. =\ Hrm, I'm at a loss for how else I could do it. Maybe I could make another list... this is getting complicated. EDIT: This is what I came up with, I'm still failing. Function Delete_Selection() Local selection:TList=New TList For p:point=EachIn p._list If p.selected=1 p.Add(selection) EndIf Next For p:point=EachIn selection p._list.remove(p) Next selection.Clear() End Function |
| ||
in post #4 the for line should be like this:For p:point=EachIn point._list although, I thing your problem is where you set the "selected". it might be what is failing to set the object for removal. |
| ||
Thanks, I'll try that. |
| ||
I recommend you start using strict or superstrict it will help you solve a lot of problems in the future. |
| ||
i usually remove things when iterating through TBH. Unless your objects are changing things outside of the list in some meaningful way you shouldn't have too much of a problem. I don't tend to use static fields in types though... Cheers Charlie |
| ||
I recommend you start using strict or superstrict it will help you solve a lot of problems in the future. You mean like this? The program runs fine, just doesn't delete points when I tell it to. |
| ||
yes, exactly what I meant. I didn't suspected you were using so many globals(yak!) that is why I assumed. I see what I can figure out for you no guarantee. Others code is always so hard for me to figure out, specially procedural. |
| ||
The important bits are what I posted before, the code is a bit messy overall 'cause it began life in B+ and has since been converted. I figured I'd post the whole thing though, because running it once to see what it does should clear up a lot. (this version doesn't quite work with vertical lines yet, but that's a minor edit) |
| ||
I kind of went through your code and tested your delete function like this:Function Delete_Selection() Local c:Int = 0 For p:point=EachIn p._list c:+1 If p.selected=1 Print "removed a point" p._list.remove(p) EndIf Next Print c End Function I found out that p.selected is never set to 1(or set for deletion) hance the reason for not removing the point. when I create a line with two points it stores three points in the list which makes sense to me that only two points be stored in the list but you might have other reasons for storing three. Look somewhere else for the problem. I know you changed your function because what Brucey said but it works fine the way you had it even though it is not the best way of doing it so I changed it back. |
| ||
Hrm, I get the "removed a point" message, but the point doesn't go away. The reason there is always +1 point then there appears is because of endpoint:point, which is used in drawing new lines. http://blitzbasic.pastebin.com/m71abe3 |
| ||
I normally do something like this:Type TSprite Global list:TList Function Create:TSprite() local s:TSprite = new TSprite if list = Null list = CreateList() list.addlast s return s End Function Method Kill() If Not list Return list.Remove(self) End Method Function KillAll() if Not list Return For Local s:TSprite = EachIn list s.Kill() Next End Function |
| ||
I found out that the list is getting corrupted by something. it might be the sorting in the "add_point" function but it is just a guess. reason: I was unable to remove any items from the list that were accessed by the for/eachin loop. However, I was able to remove it by creating Tlinks and removing it that way but even after removing the object, the list give an incorrect item count plus it gave another error. Anyway, here it is so you can try it: edit : I added asterisks to show what I modified. |
| ||
Type TSomething Field me:TLink Method New() me = somelist.AddLast(Self) End Method Method Kill() me.Remove() End Method End Type This is the BEST and FASTEST way to delete things from a list. :D:D:D A TLink removes it from the list immediately (in the way linked lists are meant to work), no need to search through the whole list to find a single object. |
| ||
Found the problem, TList.Remove() uses the overrided Compare() method. |
| ||
what? |
| ||
Yes, TList.Remove() uses Compare to find the item to remove. A better thing to do is not to override the Compare method. When calling sort, you can pass a function to use for the comparing while sorting. |
| ||
A better thing to do is not to override the Compare method. When calling sort, you can pass a function to use for the comparing while sorting. Yup, that's what I did. And thank you for the TLink code, I will be sure to use that method more frequently. |
| ||
I never used the build in function for sorting that was what confused me. I knew it was there just never bothered to use it. |
| ||
Yep, that bit me a while back as well: http://www.blitzbasic.com/Community/posts.php?topic=87418 |
| ||
just doing a bit of thread resurrection but thanks for the kill hint. |