Unexpected Behavior using Interfaces

BlitzMax Forums/Brucey's Modules/Unexpected Behavior using Interfaces

seriouslee(Posted 2016) [#1]
I'm having unexpected behavior when using an Interface. I'll try to setup the procedure:

I have a defined Interface:

Interface IMouseListener
	Method MouseMoved(mousePos:Vector2, dx:Float, dy:Float)
End Interface


A Type:

Type TControl
	Field id:Int
	Field pos:Vector2
	Field active:Byte
	
	Method Activate()
		active = True
	End Method
End Type


And another Type:
Type TButton Extends TControl Implements IMouseListener
	Field collider:TCollider
	Field mouseOver:Byte
	Field state:Int
	
	Method MouseMoved(mousePos:Vector2, dx:Float, dy:Float)
		If (collider.IsPointIn(mousePos, pos))
			mouseOver = True
			If state = 0 Then state = 1
		Else
			mouseOver = False
			If state = 1 Then state = 0
		EndIf
	End Method
End Type


I also have a scene type which contains an array of type IMouseListener, which I access during the update loop when the mouse is moved. Using DebugStop(), when mouseListener[i].MouseMoved(mousePos, dx, dy) is called it accessed the Activate Method of the TControl Type.

Can I utilize Interfaces in this fashion with NG?


seriouslee(Posted 2016) [#2]
bcc version is 0.79, bmk version is 3.13; gcc 0501.

Edit: Further testing: Program execution also incorrectly executes wrong method if one interface is used on a type that is also extended.

So, this works:

Type A Implements Interface
Type B Extends A

This does not work:
Type A
Type B Extends A Implements Interface

And this does not work:

Type A Implements InterfaceA, InterfaceB
Type B Extends A


Brucey(Posted 2016) [#3]
Hi, I've just updated bcc in github, which should solve the array/method calling issue.

Does your "further testing" refer to the array/method calling problem or usage in general?


seriouslee(Posted 2016) [#4]
Thanks, Brucey. Your update fixed the issue. The "Further Testing" was also referring to accessing via array.

I will be glad to test without array. I'll post my findings.


seriouslee(Posted 2016) [#5]
I created a simple test of 2 Interfaces and 2 types:

Interface 1
Method Multiply()

Interface 2
Method Divide()

Type A Implements Interfaces 1 & 2
Method SetX()

Type B Extends A
Method SetY()

I then created 2 local vars, one of type A and the other of Type B and called SetX and Multiply or Divide. The test performed as expected.

I then changed the implementation of interfaces, Type A Implements 1 and Type B Implements 2. Test passed.

I also changed it to put 2 Type instances into an array, which also passed the test.

Thanks again, Brucey, for all of your work. I appreciate it!


Brucey(Posted 2016) [#6]
Cool :-)