deleting objects from lists

BlitzMax Forums/BlitzMax Beginners Area/deleting objects from lists

yzzm(Posted 2009) [#1]
how would you go about deleting an object from a list that may represent an enemy that has been destroyed?


GfK(Posted 2009) [#2]
tlist.remove


Gabriel(Posted 2009) [#3]
Or keep the TLink which is returned when you add it to the list in the first instance. Then it's

Link.Remove()


Zakk(Posted 2009) [#4]
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



Brucey(Posted 2009) [#5]
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


Zakk(Posted 2009) [#6]
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



Jesse(Posted 2009) [#7]
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.


Zakk(Posted 2009) [#8]
Thanks, I'll try that.


Jesse(Posted 2009) [#9]
I recommend you start using strict or superstrict it will help you solve a lot of problems in the future.


jkrankie(Posted 2009) [#10]
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


Zakk(Posted 2009) [#11]
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.


Jesse(Posted 2009) [#12]
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.


Zakk(Posted 2009) [#13]
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)


Jesse(Posted 2009) [#14]
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.


Zakk(Posted 2009) [#15]
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


therevills(Posted 2009) [#16]
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



Jesse(Posted 2009) [#17]
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.


Czar Flavius(Posted 2009) [#18]
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.


Zakk(Posted 2009) [#19]
Found the problem, TList.Remove() uses the overrided Compare() method.


Jesse(Posted 2009) [#20]
what?


TaskMaster(Posted 2009) [#21]
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.


Zakk(Posted 2009) [#22]
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.


Jesse(Posted 2009) [#23]
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.


Muttley(Posted 2009) [#24]
Yep, that bit me a while back as well: http://www.blitzbasic.com/Community/posts.php?topic=87418


Snixx(Posted 2010) [#25]
just doing a bit of thread resurrection but thanks for the kill hint.