RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Timers

Eagle

Wanderer
Timers

Ok guys Eagle seems to be a little confused on how timers exactly work in the scripts, someone care to enlighten me a little bit and maybe a short example?
 

krrios

Administrator
Timers:

Script your own Timer class by having it subclass the base "Timer" class:

[code:1]public class MyTimer : Timer
{
}[/code:1]

Add a constructor passing in delay [, duration [, max exec count]]:

[code:1]public class MyTimer : Timer
{
// After 2.5 seconds, the timer will be ticked every 1.0 second. After 5 ticks, it will stop
public MyTimer() : base( TimeSpan.FromSeconds( 2.5 ), TimeSpan.FromSeconds( 1.0 ), 5 )
{
}
}[/code:1]

Override the protected OnTick event, and put whatever code you need in there:

[code:1]public class MyTimer : Timer
{
// After 2.5 seconds, the timer will be ticked every 1.0 second. After 5 ticks, it will stop
public MyTimer() : base( TimeSpan.FromSeconds( 2.5 ), TimeSpan.FromSeconds( 1.0 ), 5 )
{
}

protected override void OnTick()
{
// Whatever goes here
}
}[/code:1]

Now the timer class is scripted, all you need to do is start it.

[code:1]
Timer timer = new MyTimer();
timer.Start();
[/code:1]
 

Ceday

Page
is it possible to have 2 different timers in one class if you want to do different processes for each?
 

Ceday

Page
I'll give one more chance to it :)

As far as I understand, there is not any way to do it, but I want to be sure :)

Actually, some kind of index or label for Timers would be great.
In the OnTick event we can decide what we do with the help of label.
switch( label )
case x:
case y:

..
..

is it too late for a this kind of change? :)
 

David

Moderate
you can do something along these lines...
[code:1]public class MyTimer : Timer
{
private int myVariable; // declare variable here so both methods have access

public MyTimer( int Parameter ) : base( TimeSpan.FromSeconds( 2.5 ), TimeSpan.FromSeconds( 1.0 ), 5 )
{
myVariable = Parameter; // assign Parameter to myVariable so OnTick knows what it is
}

protected override void OnTick()
{
switch ( myVariable )
case 1:
// Whatever goes here
break;
}
}[/code:1]
 

Ceday

Page
oke, actually I wasnt careful enough.
I thought the process of overriding is done in the main thread, not in the Inherited timer class..

then, we dont need any index or something.

but Thanks anyway.
 
Top