mIRC Home    About    Download    Register    News    Help

Print Thread
#219327 13/03/10 03:01 PM
Joined: Mar 2010
Posts: 2
C
Cru Offline OP
Bowl of petunias
OP Offline
Bowl of petunias
C
Joined: Mar 2010
Posts: 2
Hi all,
I'm trying to make a very simple script, but have some troubles with a timer..

Firstly, here's what I did :
Code:
/stock {
  /set %stock $1
  /set %timeleft $2
  msg $chan Current stock : %stock - Time left : %timeleft
  .timer $calc($2 * 2) 1 set %timeleft $calc(%timeleft - 0.5)
}

So when I type, for example, /stock 115 5000, people on the chan are alerted of the new Stock and Time left.

Now, people can also check the Stock and Time left at any time by typing !stock. So I added in Remote :
Code:
on *:TEXT:!stock:*:{
  notice $nick Current stock : %stock - Time left : %timeleft
}



Now, my problem is that the time left is supposed to be reduced of 0.5 every second. But it seems to stop after the first change, so people will always see the same value (timeleft - 0.5) every time they type !stock.


1) What's wrong in my script ?
2) Any solution ? Thanks smile

~ Crupu

Joined: Jan 2003
Posts: 2,523
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
The problem is that $calc(%timeleft - 0.5) is evaluated inside the /stock alias and not at the time of /timer's firing. In other words, the /timer command only sees the value of $calc(%timeleft - 0.5) at the time /stock was executed - it does not see the string "$calc(%timeleft - 0.5)" (so that it can evaluate that string each time it fires).

There are several ways to solve this problem. In your case, the simplest might be to put an exclamation mark right after the $ in $calc, which prevents mirc from evaluating the identifier in /stock:

.timer $calc($2 * 2) 1 set %timeleft $!calc(%timeleft - 0.5)

However, if you have complex expressions in the part of the /timer, it becomes increasingly complicated to escape evaluations in script and the code becomes error-prone and hard to read. A cleaner alternative is to just create another alias that performs these commands and use that alias in /timer. In your case, this would look something like:
countdown { 
set %timeleft $calc(%timeleft - 0.5)
}

stock {
set %stock $1
set %timeleft $2
msg $chan Current stock : %stock - Time left : %timeleft
.timer $calc($2 * 2) 1 countdown
}

You might be wondering why we only had to escape $calc() (in the first solution) and not %timeleft. This is because mirc prevents evaluation of variables when they directly follow a /set (also /dec and /inc) command. With this in mind, a third solution to your problem is
stock {
set %stock $1
set %timeleft $2
msg $chan Current stock : %stock - Time left : %timeleft
.timer $calc($2 * 2) 1 dec %timeleft 0.5
}



/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
Joined: Mar 2010
Posts: 2
C
Cru Offline OP
Bowl of petunias
OP Offline
Bowl of petunias
C
Joined: Mar 2010
Posts: 2
Thanks, it works better smile

Another problem now is that the timer never stops.. It's supposed to stop when the timing reaches 0 (or almost), but it keeps doing -0.5 every second, to the infinite...? :|

Joined: Jan 2003
Posts: 2,523
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
Check for rogue timers by typing /timers, then stop them with /timer<name> off. It is a good idea to use a named timer in your /stock alias, so that a subsequent /stock call will simply reset the same timer instead of starting a new one that works concurrently.

Alternatively, you can use /set -z and get rid of the /timer altogether. The only issue is that -z decreases by 1 every second, not by 0.5 as you want. You can fix that by setting %timeleft to $calc($2 * 2) (instead of simply $2) and then using $calc(%timeleft / 2) when reporting its value with a /notice:
Code:
stock {
  set %stock $1
  set -z %timeleft $calc($2 * 2)
  msg $chan Current stock : %stock - Time left : $2
}
on *:TEXT:!stock:*:{
  notice $nick Current stock : %stock - Time left : $calc(%timeleft / 2)
}


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com

Link Copied to Clipboard