How to use identifiers or if in a timer

From Scriptwiki
Revision as of 20:44, 2 September 2005 by Zyberdog (talk | contribs) (If-satements: typo)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

The evaluation moment is the moment at which identifiers are converted in to their respective values. E.g. $me will be replaced by your current nick at the evaluation moment.

Evaluation

What many people often fail to realize is that there are two evaluation moments when you use a timer. The first occurs when you start the timer and the second occurs when the timer executes the command.

//timer 2 2 echo -a $time

This command will give you the following output:

18:40:43
18:40:43

This is due to the first evaluation point being the moment the timer is started, so the command it executes becomes:

echo -a 18:40:43

Keeping this in mind we are able to change the above timer to do what we wanted it to do:

//timer 2 2 echo -a $ $+ time

We get the same result with

 //timer 2 2 echo -a $!time

These commands will give you the following output:

18:40:43
18:40:45

What happens is that when you launch the timer, it evaluates the echo command from "echo -a $ $+ time" to "echo -a $time", which is the command we wanted it to execute.

Aliases

Often you want the events executed by the timer to be a bit more sophisticated than an echo or similar single-line commands. In those cases you will need to use an alias instead. Example:

alias echoTime {
  echo -a Local Time: $time
  echo -a Local Date: $date
}
//timer 2 2 echoTime

Returns

Local Time: 10:31:11
Local Date: 25/08/2005

If-statements

Easiest way to use if-statement to timer is to just add it there.

//timer 2 1 if (a == b) { echo - ok }

Kinda stupid, eh? It's hard to try to figure out how to get any advantage from this.

Let's make our own random number generator.

alias custom_rand return $calc((16807 * $$1) % (2 ^ 31 - 1))
 
//timer 2 1 echo -a $!custom_rand( $!ctime)

Try it and see what values do you get. Then, how about adding more level to it? If you want to limit to values from 1 to N, we need t modify our alias little.

alias custom_rand return $calc((16807 * $$1) % (2 ^ 31 - 1) % $rand(1, $$2))

//timer 2 1 echo -a $!custom_rand( $!ctime, 450 )

Gives us values from 1 to N. How about that if-statement then? Notice how every identifier must be evaluated (!) and how alias can be given as parameter to another alias.

alias custom_rand return $calc((16807 * $$1) % (2 ^ 31 - 1) % $rand(1, $$2))
alias better_than if ($$1 > $$2) return $true | else return $false

//timer 2 1 echo -a $!better_than( $!custom_rand( $!ctime, 500 ), 125 )

Returns ex.

$true
$false

The idea? Depends of its usage. The idea was to give you an idea how to call alias with parameters and evaluate timers. Hopefully successfully.

Contributed by Wixbit