Custom Compare() method causes problems...

BlitzMax Forums/BlitzMax Programming/Custom Compare() method causes problems...

Kistjes(Posted 2008) [#1]
Hi,
Below you find a very familiar piece of code for managing all elements in your game.
I was experimenting with the Compare() method to change the order of the list. In that way I'm able to draw elements in the correct order on the screen if I give each element a Z-value (higher values are on top of lower values). This works great (you can try) but if you have more than one element with the same Z-value (which should be possible) you can not remove that element from the list with the provided functions: Remove(), RemoveLink() or ListRemove() because these functions use the Compare() method to find the right link in the list.

I would like to keep the z-ordeing thing AND be able to manage the list properly.
Any ideas how to solve this?



Paposo(Posted 2008) [#2]
Hello.

Erase your method Compare() from TElement and change the method SortZ() with a pointer to compareFunc%( o1:Object,o2:Object )

Bye,
Paposo


Kistjes(Posted 2008) [#3]
Your english is fine :) but I don't think I understand your suggestion.
Do you mean: "write your own SortZ() and Compare() methods"?
Can you give an example?


Brucey(Posted 2008) [#4]
Seems to work fine here :

Your code with some test stuff added...



Paposo(Posted 2008) [#5]
Like this:

function sortZ(bAscending:int=true)
  gElements.sort(bAscending,comparar)
end function

function comparar:int(Object o1, Object o2)
  local e1:TElement = TElement(o1) 
  local e2:TElement = TElement(o2)
  return o2.piZ-o1.piZ
end function


You not need the method compare%

Bye,
Paposo


Kistjes(Posted 2008) [#6]
@Paposo: thanks!
I didn't know about the second argument in the list.Sort() function.

I made some corrections in your code:
	Function SortZ(bAscending% = True) 
		glElements.Sort(bAscending, CompareZ) 
	End Function
		
'	This function is used for sorting the glSprites list in Z-order (ascending)
	Function CompareZ:Int(o1:Object, o2:Object) 
		Local e1:TElement = TElement(o1) 
		Local e2:TElement = TElement(o2) 
		Return e1.piZ - e2.piZ
	End Function



tonyg(Posted 2008) [#7]
<edit> ... too late.
Kistjes, it is so much easier if you had provided a working example showing the issue. That way people are more inclined to change it and repost.


Kistjes(Posted 2008) [#8]
@tonyg: You're right. Combining the brucey's and Paposo code will result in a working example.
Ok, here it is (with some additions):



Paposo(Posted 2008) [#9]
ooopsss.

Sorry the little bug :) Copy ... paste... ejem

Bye,
Paposo