To kill a mocking type...

BlitzMax Forums/BlitzMax Beginners Area/To kill a mocking type...

Crinkle(Posted 2010) [#1]
This type is mocking me, BMax says "remove not found" this did work in the program I re-typed the code from:

***********************************
Type TBar

	Field x#,width
	Global Bar:TList = New TList
	
	Method update() 'if gone away, remove it
		x:+.5
		If x+width<-2
			remove '<<<<<<<<< why not working?
			Return
		EndIf
	' draw it	
	glLoadIdentity()
	glColor3f 0.5,0.5,1.0
		glBegin GL_QUADS
		glVertex3f -1.0, 0.2, 0.0          ' Top Left
		glVertex3f  1.0, 0.2, 0.0          ' Top Right
		glVertex3f  1.0,-0.2, 0.0          ' Bottom Right
		glVertex3f -1.0,-0.2, 0.0          ' Bottom Left
	glEnd		
		
	End Method
	
	Function CreateBar:TBar(x#,width) 'make one
 
		Bar.x=x
		Bar.width=64
		Return Bar
		
	End Function
	
End Type

***********************************

If "remove" isn't how you delete a type entry how do you do it?


Jaydubeww(Posted 2010) [#2]
method RemoveIt(O:TBar)
listremove(O.list,self)
end method


Czar Flavius(Posted 2010) [#3]
You can use [ code ] [ /code ] tags.

listremove (or list.remove) is slow as it searches through the list one object at a time until it finds your object. There is a shortcut.

When you add something to a list, it returns a TLink. a TLink is like a shortcut straight to your object in that list. You can tell the TLink to remove itself and it will do so quickly.

If you call a method New, it will always automatically run when a new object is created so you can add the object to the list then, assuming you want every object in the list.

I don't understand your CreateBar function. Bar is the name of the list, but you are treating it as a TBar? You don't even create a New TBar. That shouldn't even compile.

Type TBar
	Global AllBars:TList = New TList
	
	Field link:TLink
	
	Field x:Float, width:Int
	
	Method New()
		'add this new bar to the list, and save its tlink for later
		link = AllBars.AddLast(Self)
	End Method

	Method update() 'if gone away, remove it
		x:+.5
		If x+width<-2
			'remove this bar from the list
			link.Remove()
			Return
		EndIf
	End Method
		
	Method draw()
		' draw it
		glLoadIdentity()
		glColor3f 0.5,0.5,1.0
		glBegin GL_QUADS
		glVertex3f -1.0, 0.2, 0.0 ' Top Left
		glVertex3f 1.0, 0.2, 0.0 ' Top Right
		glVertex3f 1.0,-0.2, 0.0 ' Bottom Right
		glVertex3f -1.0,-0.2, 0.0 ' Bottom Left
		glEnd
	End Method

	Function Create:TBar(x#,width) 'make one
		'create a new TBar object and store it in this temp variable
		Local Bar:TBar = New TBar
		Bar.x=x
		Bar.width=width
		Return Bar
	End Function
	
	Function update_all()
		For Local bar:TBar = EachIn AllBars
			bar.update()
		Next
	End Function
	
	Function draw_all()
		For Local bar:TBar = EachIn AllBars
			bar.draw()
		Next
	End Function
End Type


For Local i = 0 To 10
	TBar.Create(Rand(0, 100), 64)
Next

Graphics 640, 480

While Not KeyHit(KEY_ESCAPE)
	TBar.update_all()
	TBar.draw_all()
	Delay 1
WEnd
End



_Skully(Posted 2010) [#4]
When you add something to a list, it returns a TLink

Thanks for that... I didnt realize that. I was just suffering from list removing lag in TileMax (as linked sprites move from Tile to Tile)


Czar Flavius(Posted 2010) [#5]
I'm glad I could help!
One more thing, it's a bit safer to say
If link Then link.Remove()
for example, just in case it hasn't been assigned a link for any reason.