I have found that the problem is in the ":set" lines, but I just can't get what is wrong, can someone help ?
Is there a way to localize script errors except extrasensory perception ?
The issue is global variables need to be "declared" in the function before use. Even if they are assigned in the same script. Once declared, like in the following, it should work:
:global activeCh first
:global auxCh second
:global setActiveCh do={
:global activeCh
:global auxCh
:if ($activeCh = $"ch") do={
:log info "Keep $activeCh"
} else={
:log info "Switch $activeCh to $ch"
:set auxCh $activeCh
:set activeCh $ch
}
}
The docs speak of "scope":
https://help.mikrotik.com/docs/display/ROS/Scripting – and the function block is same as local scope, thus different from "global" scope.
Now what's mildly annoying, and your need for "ESP", is there is
no error by using or even attempting to set an un-declared variable in local scope – as you see from your original code: it sets nothing, but doesn't produce an error even though it's not defined.
Basically you need add ":global <varname>" for ANY variable/function defined
outside the scope, you are going to use
inside the current "scope" (e.g. do={} block here).