Callbacks

Decision making with callbacks, implement if-else logic to your macros

Basic

This topic involve Image Detection and Functions so you should check them first.

Callbacks in this case means something that run after an event.

That something is our Function and the event is when Image Detection is Success or Fail

Assign Callbacks
Assign Callbacks

Assign a callbacks is simple

You just have to create a Function and select them from the menu.

Callback Function receive two arguments. The first argument is the result of the Image Detection, the 2nd argument is the original Region that was used.

Use case

While create and assign a Callback is simple. Making use of them is different story and totally depend on your use-case. Here some common scenarios that you might need callbacks

Stop when Fail

When you want to click the "Start" button. What if that button does not appear within the Timeout period?

In the case of time based macro, it'll just continue and destroy your units, spend all your gems. Worst if the game crashes, it'll dial your old friends and you'll have an awkward conversation with them explaining why you didn't keep in touch.

Now you want to do something in the case of failure. Assign a Fail Callback to handle such case.

It's recommended to just use Sys.stop() to stop the macro immediately.

There are a million of things that could cause that "Start" button not to show up (connection error, game crashes, popup, notification, etc...). You can try to recovery and handle all cases but it's best to just stop the macro and alert the user so they can restart.

If-else Logic

You can use Callback to make decision.

For example if you're making a reroll macro. You would want to check if the unit you want is on the screen or not, if not then roll again else continue.

Since Callback is a Function and Function can have Image Detection in it. You can chain Callback together to create a complex if-else logic.

Start Image Detection, call one of the Callback which in turn start another Image Detection that will run another Callback.

Code Equivalent

    //Create a Region
    var region = Region(100, 100, 500, 500)

    //Click on the template
    var match = region.click("template name", FParam.timeout(3000).sRate(2).mScore(0.8))

    //As you can see it is not really callback in programing term
    if (match) {
        //run something when success or just continue
        onTemplateClicked()
    } else {
        //Try to recover
        checkError()
        checkCrash()

        //Or just stop
        Sys.err("Unknown Error occured while searching for template name")
    }

© 2021 - Macrorify by KoK-CODE