mIRC Homepage

Unexpected timer set var behaviour

Posted By: Moros

Unexpected timer set var behaviour - 04/10/20 10:45 PM

I've been troubleshooting a script error for quite some time and the problem seems to stem from the way some variables are reevaluated when set by a timer.

Example 1:
//set -s %test.var $eval($file(mirc.exe).version,0) | set -s %new.var %test.var

Expected return:
$file(mirc.exe).version
$file(mirc.exe).version

Example 2:
//set -s %test.var $eval($file(mirc.exe).version,0) | timer 1 1 set -s %new.var %test.var

Unexpected return:
$file(mirc.exe).version
7.63.0.0

timer 1 1 set -s %new.var $eval(%test.var,0) gives me the desired result, but I don't recall ever seeing this mentioned in the documentation.
Posted By: Khaled

Re: Unexpected timer set var behaviour - 05/10/20 09:05 PM

That is indeed the way /timers work. If you want to pass the non-evaluated form of the variable or identifier, you need to use $eval(), [], $+, $!, or various combinations of these.
Posted By: Sat

Re: Unexpected timer set var behaviour - 06/10/20 01:06 PM

Originally Posted by Moros
timer 1 1 set -s %new.var $eval(%test.var,0) gives me the desired result

Here you are preventing evaluation when you start the timer, which indeed works, but keep in mind that whatever you pass in then still has to be valid at the time that the timer triggers. For example, if you unset %test.var before the timer triggers, you will end up with nothing. That may or may not be what you want; often, it is not. When passing in something like a local variable or $1-, preventing evaluation when starting the timer will not work: when the timer actually triggers, the context in which that variable or identifier was valid will be gone.

Alternatively, you can prevent evaluation when the timer triggers. Usually that is a better approach, and also easier: mIRC has a built-in identifier for that, namely $unsafe. For example:

Code
//set -s %test.var $eval($file(mirc.exe).version,0) | timer 1 1 set -s %new.var $unsafe(%test.var)
Posted By: Moros

Re: Unexpected timer set var behaviour - 07/10/20 03:31 AM

Thank you for that clear explanation!
© 2020 mIRC Discussion Forums