How do you pass a Callback as a parameter?

I’m trying to create a custom trigger zone that using mutator zone device

trigger_zone := class():

    MutatorZoneDevice: mutator_zone_device = mutator_zone_device{}
var EventAgentEntersCancellation: ?cancelable = false

RegisterOnAgentEnters(Callback: agent -> void):void=
        CancelIfExists(EventAgentEntersCancellation)
        set EventAgentEntersCancellation = option{MutatorZoneDevice.AgentEntersEvent.Subscribe(Callback)}

how it used

Init<override>():void=
        TriggerZone.RegisterOnAgentEnters(OnTriggerEnter)

OnTriggerEnter(Agent: agent): void =

I got this error:

Using a value of type type{(:t->void):cancelable} as a value of type type{(:agent->void):cancelable} is not yet implemented.

How do I solve this problem?

Hey, it looks like this will be supported in the future but in the meantime the workaround I’ve found is this:

callback_param_handler := class():

    Callback: agent -> void

    On(Agent: agent): void = Callback(Agent)


trigger_zone := class():

    MutatorZoneDevice: mutator_zone_device = mutator_zone_device{}

    var EventAgentEntersCancellation: ?cancelable = false

    RegisterOnAgentEnters(Callback: agent -> void):void=

        set EventAgentEntersCancellation = option{MutatorZoneDevice.AgentEntersEvent.Subscribe(callback_param_handler{Callback:=Callback}.On)}

I couldn’t find a way to make that handler class use a generic type so you need one for every callback signature but it does work.

1 Like

That’s worked, thank you. I was trying to create a class that wraps around a device instead of using the device directly.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.