mIRC Homepage
Posted By: firefox timer and variables - 10/09/10 10:12 AM
let's say that I have

%var1 test1
%var2 test2

and I run /timer 1 10 /alias %var1 %var2

everything works fine

but if before the 10 seconds have finished the values change to:

%var1 test3
%var2 test3

when the time ends and calls the alias it uses the new values - how do I make it use the values of the vars when the timer was actually started?
Posted By: Thels Re: timer and variables - 10/09/10 10:20 AM
Run this bit of code before reading my explanation:

Code:
alias1 {
  echo -a setting variables
  set %x test1
  set %y test2
  .timer 1 5 alias2
  .timer 1 10 alias3 %x %y
}

alias2 {
  echo -a changing variables
  set %x test3
  set %y test4
}

alias3 {
  echo -a var x: %x
  echo -a var y: %y
  echo -a para 1: $1
  echo -a para 2: $2
}


When you use %var1 and %var2 as parameters in the timer command, they will arrive in that alias as $1 and $2 respectively, representing the first and second parameters sent along (assuming for the moment neither variable contains any spaces).

However, I'm assuming that your new alias reads %var1 and %var2 again, which indeed have been updated. Use $1 and $2 to check the variables as they were when you initiated the timer.

EDIT: Also, if %x would be $null, then you'd get %y as $1, since that would become the first parameter.
Posted By: 5618 Re: timer and variables - 10/09/10 10:33 AM
Variables are evaluated at the moment the timer is triggered.

You can simply prevent pre-evaluation by using $(%var1,0) - $() is similar to $eval()

This way %var1 will literally be passed to the alias as $1 and only be evaluated once the alias is called.

Edit: You can achieve this same result in various ways of course. Another method is using $+(%,var1)
Posted By: DJ_Sol Re: timer and variables - 10/09/10 10:58 PM
I think Khaled made it so you can put a ! after the command symbol to evaluate it when it executed.

timer 1 1 alias %!var1 %!var2 $!alias(%!var1,%!var2)

Is this right?
Posted By: Thels Re: timer and variables - 10/09/10 11:16 PM
It doesn't work with variables, only with identifiers.

%!var1 is actually a variable on it's own, separate from %var1.

I'm also not sure they work with timers. They were meant for scid, IIRC.
Posted By: Horstl Re: timer and variables - 14/09/10 12:26 PM
The command part of a timer will be evaluated on execution. However, the timer command will be evaluated as issued as well. Some of the methods to prevent that "first" evaluation (at the moment the timer command is issued) for variables:
Code:
//set -u3 %x 1 | timer 1 1 echo -a evaluated at timer start: %x // evaluated at timer end: $(%x,0) or % $+ x or $+(%,x) | set -u3 %x 2


for identifiers:
Code:
//timer 1 1 echo -a evaluated at timer start: $time(ss) // evaluated at timer end: $($time(ss),0) or $!time(ss) or $ $+ time(ss) or $+($,time(ss))


In several situations the "double evaluation" of timers (and of some other commands) may not only result in unexpected values, but become a security issue. Here are two further methods (especially useful for unknown strings or lengthy commands).
Another method would be to put your parameters not in the command part of a timer but in it's name (then use e.g. token identifiers on $ctimer).

Finally - it's the most obvious sollution for your initial example - you may have the timer call "/alias" only and store your values/parameters in global variables, hash tables, or the like.
Posted By: Thels Re: timer and variables - 14/09/10 01:19 PM
From what I understood, he actually wants them to evaluate when the timer starts, which makes me thinks he refers to the variables again, instead of $1 and $2.
Posted By: Horstl Re: timer and variables - 14/09/10 02:24 PM
Too bustling to add to the other... reading it again, you're right wink
© mIRC Discussion Forums