to send Self to a function using tevent + hook

BlitzMax Forums/BlitzMax Programming/to send Self to a function using tevent + hook

SuperStrict

Type Ttest

	Field a:Int=1
	Field b:Int=2
	Field c:Float=3.5

	Field newevent:TEvent=New TEvent
	
	Function eventhook:Object(id:Int,data:Object,context:Object)
		If Ttest(context) Ttest(context).ev TEvent(data);Return data	
	EndFunction
	
	Method New()
		AddHook EmitEventHook,eventhook,Self
	End Method
	
	Method Free()
		RemoveHook EmitEventHook,eventhook
		GCCollect()
	End Method
	
	Method ev(event:TEvent)
	
		If event.id=$999666
			DebugLog "***"
			newevent.id=$999555
			newevent.data=Self ' <- the issue, ".source" works tho.
			EmitEvent newevent
		EndIf
	End Method
	
End Type

Function test2:Object(id:Int,data:Object,context:Object)
	Local ev:TEvent=TEvent(data)
	
	If ev.id=$999555
	
'		Local p:Ttest=ev.data ' <- the issue
'		debuglog p.a
'		debuglog p.b
'		debuglog p.c
	EndIf
	
	Return data
EndFunction

AddHook EmitEventHook,test2




Local t:Ttest=New Ttest

Local a:TEvent=New TEvent

a.id=$999666
EmitEvent a
End



ok, fair enough: how to get test "Self" to that test2 function so I can read out test's stuff there?

Either event.source or event.extra can contain an object, event.data is an integer for things like keycodes etc.

so, what's the solution?

"newevent.data=Self" results in an error..
.source and .extra works, but then how to get access to the object again in that test2 function?

Found it! May this code serve thou!

SuperStrict

Type Ttest
	Field a:Int=1
	Field b:Int=2
	Field c:Float=3.5

	Field newevent:TEvent=New TEvent
	
	Function eventhook:Object(id:Int,data:Object,context:Object)
		If Ttest(context) Ttest(context).ev TEvent(data);Return data	
	EndFunction
	
	Method New()
		AddHook EmitEventHook,eventhook,Self
	End Method
	
	Method Free()
		RemoveHook EmitEventHook,eventhook
		GCCollect()
	End Method
	
	Method ev(event:TEvent)
		If event.id=$999666
			newevent.id=$999555
			newevent.extra=Self
			EmitEvent newevent
		EndIf
	End Method
	
End Type

Function test2:Object(id:Int,data:Object,context:Object)
	Local ev:TEvent=TEvent(data)
	
	If ev.id=$999555
		Local p:Ttest=Ttest(ev.extra)
	
		DebugLog p.a
		DebugLog p.b
		DebugLog p.c
	EndIf
	
	Return data
EndFunction

AddHook EmitEventHook,test2

Local t:Ttest=New Ttest

Local a:TEvent=New TEvent

a.id=$999666
EmitEvent a
End