Script timer tutorial

From StealthBot Wiki Backup
Jump to: navigation, search

A timer is an object that fires an object event every so often. You can specify how many seconds or milliseconds you want between each fire, the "interval".

Creating

Creating the timer is relatively easy and managing your timers are just as easy. First, we need to create a timer in the Event_Load so the timer object will get created when the bot opens and remain open until you close the program:

Sub Event_Load()
    '// Create the LongTimer object, a type of timer that operates in seconds, with the name myTimer
    CreateObj "LongTimer", "myTimer"
    '// Enable it
    myTimer.Enabled = True
    '// Set the interval to 5 seconds
    myTimer.Interval = 5
End Sub

Timer event

An event is a subroutine with a specific name and amount of parameters. The timer subroutine doesn't have any parameters.

All that's left for the timer is to put the stuff we want it to do inside the timer subroutine. Note that when we're writing the timer subroutine, we don't use the word "event", but rather the timer's name, which we passed to CreateObj().

Sub myTimer_Timer()
    '// Display our text inside the bot so only we can see it and no one else.
    '   We can use the AddChat function followed by a colour we want, then
    '   changing the colour mid-way through the text.
    AddChat vbGreen, "This green ", vbMagenta, "and purple text ", vbGreen, "are appearing every 5 seconds."
End Sub

This timer will call AddChat() every five seconds.

Template:Usersource

See also