Event hooks

BlitzMax Forums/BlitzMax Programming/Event hooks

cps(Posted 2016) [#1]
Thanks to these forums I have a event hook driven scrolling game board in a canvas, the event hook itself is in the form of a function.
My question is how do you (can you) put an event hook into a type ? This would avoid making variables/methods/types used by the event hook global.

IE This works
AddHook EmitEventHook, MyHook1

Function MyHook1:Object(iId:Int,tData:Object,tContext:Object)
' do something to some vars (these have to be global)
End Function

This does not work
Local zz:hookcon =New hookcon
AddHook EmitEventHook, zz.TheHook

Type hookcon
Method TheHook:Object(iId:Int,tData:Object,tContext:Object)
' do something to some vars (these could be local to the type)
End Method
End Type
Any advice appreciated, have fun cps


cps(Posted 2016) [#2]
Just found Henri's contribution to the thread 'BlitzMax Forums/MaxGUI Module/Refresh canvas while scrolling' which seems to show a way of putting an event hook function into a type. Thanks cps


Bobysait(Posted 2016) [#3]
You just need to set the Context of the function then cast it in the hook function
So it's not a method but it works the same

Local zz:hookcon =New hookcon
AddHook EmitEventHook, hookcon.FilterEvent, zz

Function FilterEvent:Object(iId:Int,tData:Object,tContext:Object)
    If hookcon(tContext)<>Null
        ' do stuff here
    EndIf
End Function



cps(Posted 2016) [#4]
Thanks 'bobysait', after a brief fiddle I can put the function into a type and initialise it with a method which allows the function to access the local variable. IE

Local But1:Tgadget(,,,,,)' the gadget I want the event hook for
Local zz:hookcon =New hookcon' the type
zz.initilise(But1)' send gadget name to type (and any other local vars)
AddHook EmitEventHook, hookcon.FilterEvent, zz

Type zz
Global Gad:TGadget[1]

Method initilise(Tv0:Tgadget)
Gad[0]=Tv0
End Method

Function FilterEvent:Object(iId:Int,tData:Object,tContext:Object)
If hookcon(tContext)<>Null
If Event.source=Gad[0] And Event.ID=EVENT_GADGETACTION
' do stuff here
Return Null
else
Return tData
end if
EndIf
End Function
End Type
So vars can be kept within the type (even if they are global and not fields). I'll fiddle on a bit more and see if I can access the var as a field variable. Thanks again, have fun cps