Method compare. Why it behaves like that?

BlitzMax Forums/BlitzMax Beginners Area/Method compare. Why it behaves like that?

Takuan(Posted 2006) [#1]
Hi, I dont understand whats going on while using method compare.

In short:
I put Image instances in the imagelist
Draw images in imagelist on screen
Call "Method collide"

Works, i click on that images on screen and they get removed.

The Problem:
It only works if i dont have "Method compare" in my Type.
With "Method compare" in Type Image:
Imagescollide gets true but it doesnt listremove the images i have clicked on (but removes other images).

I dont even call "method compare" by myself.
And shouldnt it just return the int value of "muh-s.muh"?
If "Method compare" would mix up some fields or Imagelist, why everything is drawn correctly on screen (loops through imagelist and draws them)?

Any compare gurus out there?



type Test
field imagelist:tlist=new TList
end type

type Image
Field Bitmap
Field X=random
Field Y=random
Field muh

Method compare(obj:Object)
Local t:tile = tile(obj)
If t=null Then Return 1
Return muh - t.muh
End Method
End Type

Method collide
For i:image=eachin Test.imagelist
if imagescollide (mousecursor, i) then
ListRemove test.list, i;
endif
next
end method


tonyg(Posted 2006) [#2]
You have overwritten the compare method used by Findlink method, used by list.remove method used by ListRemove function?
<edit> I think there's a post by sswift documenting dangers of using compare override.
P.S. That's not the code you're using either... is it?


Takuan(Posted 2006) [#3]
No, i wrote that "pseudo" Code only to document my problem a little better:D

Thank you, looks like i have to find a way to sortlist things by field value without overriding compare method.


tonyg(Posted 2006) [#4]
You might have missed the '?' at the end of my initial response.
Post your actual code and somebody can give it a try, see the output and suggest something.


Dreamora(Posted 2006) [#5]
To remove the object from list, the overwritten compare must return 0 if the object is the searched one as TList removes an object basing on compare(otherObject) = 0


degac(Posted 2006) [#6]
I have a similar 'error': once I introduced a compare method the only way to eliminate the object is keep track by its link and use the method LINK.remove()...


Oddball(Posted 2006) [#7]
I found this really irritating too. There should be two compare methods. One that is only used by the sort command and can be overridden so that we can have user defined sorts, and another compare method that is used by all the other commands like .Remove(). Is there any sane reason why it is the way it is?


Chris C(Posted 2006) [#8]
yeah I found compare problematic too, I touched on it in a tutorial on my site, there really ought to be two types of compare method it think Oddball has a good point.


Dreamora(Posted 2006) [#9]
1.20 introduced a seperate compare function pointer for sorting. (its a function pointer that takes 2 objects and returns an Int that does the same as compare).

So you can use compare for "remove tests" and the compare function pointer for sorting.

That happened due to a looooong discussion on the sense or nonsense of using compare for sort and find :-)


Oddball(Posted 2006) [#10]
Do you have an example of how to do this? I really didn't understand any of that function pointer mumbo jumbo.