Timers and Script Run Commands

Top  Previous  Next

There are cases when executed code requires delay before continuing to the next step (line of code). Either it's about moving to another page, delaying execution of current script or another script, time & script run commands serve as creators of pauses / delays.

Strange thing to do, indeed.

In reality we try to avoid delays and here we make artificial ones.

 

TimeCommands

 

You'll use commands like Pause and ScriptTimer to delay execution of scripts either for data processing purposes or waiting for some event like user input. There are two specific timer-related commands - with PageTimer you set delay before moving to some other project page, and with ExitTimer command MMB waits specified amount of time before it exits the project.

 

RunScript command won't make any delays - it will immediately run specified script.

Both ScriptTimer and RunScript don't interrupt running of current script when calling another one - ScriptTimer will immediately continue with code lines of it's current script, RunScript will wait for called script to finish and then continue with code lines (if any) below.

All timer-related commands use millisecond as a time unit. One second contains 1000 milliseconds, and you'll often use n x 1000 to manage delays in seconds.

 

Pause("ms")

Description

 

Delays further running of current script for specified amount of time.

Pause

Code Examples

 

**Pause script for 5 seconds (5000 ms) :

Pause("5000")

** Pause script for 12 seconds (12000 ms) using numerical variable:

delay=12000

Pause("delay")

Additional Info

 

This command is occasionally being used to make delay for user input or wait for some external task (like data processing, copying) to finish.

 

RunScript("ObjectLabel")

Description

 

Runs script specified as command parameter.

RunScript

Code Example

 

**Run script labeled "Calculate" :

RunScript("Calculate")

Additional Info

 

Running of other scripts saves you from re-writing code lines - it's recommended to put frequently used routines under separate script objects and run 'em whenever they're needed. Code lines below RunScript line will in most cases wait for called script to finish it's work, so put and retrieve processing results using string and numerical variables.

This command runs default (Mouse Up) script on objects with both Mouse Down and Mouse Up events (Buttons, Bitmaps, Rectangles, etc).

WARNING! Do not use RunScript command to call the same Script object/MouseUp event multiple times and from the same Script object! This will cause an infinite recursion, because RunScript wait for its end. After using RunScript in that type of code you can see "Recursion in Script reached 50 levels" error message. You can use ScriptTimer instead, which doesn't wait for end.

 

RunScriptCode("parameter1","parameter2")

Description

 

RunScriptCode runs external scripts form string or string variable. It means that you can write any script you want in plain txt file, load entire file to string variable (using for example LoadText function) and run it over RunScriptCode. It's easy as that ;)

 

Available parameters:

paremeter1 string or string variable holding the script source code

paremeter2 0=quiet parsing, 1=loud parsing (1 = runtime errors are displayed)

Code Example

 

** Here is how to load and run an external file with script

LoadText("ExtScript$","<Embedded>\pagestart.txt")

RunScriptCode("ExtScript$","")

 

** Here is how to define a script code directly in other script

script$='For i=1 To 1000' + CHR(13) + CHR(10)

script$=script$ + 'm=m+1' + CHR(13) + CHR(10)

script$=script$ + 'Next i'

RunScriptCode("script$","")

 

 

Additional Info

 

Running of other scripts saves you from re-writing code lines - it's recommended to put frequently used routines in external or even embedded files and run them whenever they're needed. Code lines below RunScriptCode line will wait for called script to finish it's work.

WARNING! Do not use RunScriptCommand command to call the same Script multiple times and from the same Script! This will cause an infinite recursion, because RunScriptCode wait for its end. After using RunScriptCode in that type of code you can see "Recursion in Script reached 50 levels" error message. You can use ScriptTimer instead, which doesn't wait for end.

 

ScriptTimer("ObjectLabel","ms")

Description

 

Waits for specified amount of time (number or numerical variable as second parameter) and then starts execution of another script, specified as first command parameter:

ScriptTimer("ScriptLabel","TimeDelay")

To give you extra power, MMB introduces 1000 independent timers inside ScriptTimer command called Timer1..Timer1000. Using this principle, you can call ScriptTimer command 1000 times, using different timers and make 1000 independent threads - every thread runs it's own script and won't care much about others.

 

NOTE: In a previous versions, there were only three timers - TimerA, TimerB and TimerC. These timers are still active and you can see them in old projects and scripts. But we do not recommend to use them in new projects!

 

Using multiple timers starts by writing their labels...

Timer1, Timer2,..., Timer1000

...together with equal sign as a script label prefix:

ScriptTimer("Timer1=ScriptLabel1","TimeDelay")

ScriptTimer("Timer2=ScriptLabel2","TimeDelay")

ScriptTimer("Timer3=ScriptLabel3","TimeDelay")

 

ScriptTimerThreads

 

Code Examples

 

** Run Script1 with delay of 8 seconds :

ScriptTimer("Script1","8000")

** Run MyScript with delay specified using numerical variable :

delay=5500

ScriptTimer("Script1","delay")

** Run ThreadScript with 5 second delay and using Timer1:

ScriptTimer("Timer1=ThreadScript","5000")

Additional Info

 

This command is occasionally being used to make delay for user input or wait for some external task (like data processing, copying) to finish.

ScriptTimer runs script only once, so you have to call ScriptTimer command again to repeat script running.

In addition to this, running of ScriptTimer before previously set timer delay has elapsed will reset previous setting and use the new one.

 

For example, if you run:

ScriptTimer("Script1","8000")

...and before elapsed 8 seconds you call ScriptTimer again:

ScriptTimer("Script2","12000")

...previous setting will be substituted with the new one, running Script2 after 12 seconds. This is also the case for individual timers Timers1, Timers2, etc...

ScriptTimer command runs default (Mouse Up) script on objects with both Mouse Down and Mouse Up events (Buttons, Bitmaps, Rectangles, etc).

All running scripts (including ScriptTimers) running from ordinary Page are terminated by jump to another Page. If you want to preserve run of a ScripTimer even if user jumps between pages, place the Script object on Master Page/Layer and then call it with Master Page:: or Master Layer:: prefix (as described here).

Then the script should appear like this:

ScriptTimer("Master Page::Script","100")

or this if you want to use multiple timers.

ScriptTimer("Timer1=Master Page::Script","100")

 

PageTimer("ms","PageLabel")

Description

 

Waits for specified amount of time (number or numerical variable as the first parameter) and then either moves to the next page (if second parameter is empty), page specified as the second parameter, or performs one of the following actions specified as the second command parameter:

THIS_PAGE - restarts current project page (object positions, background music) and runs it's startup script

 

THIS_SCRIPT - runs only startup script of current project page

 

IF_IDLE - moves to next page if there's no keyboard or mouse click event for specified amount of time

 

PageTimer

Code Examples

 

** Move to next page after 8 seconds:

PageTimer("8000","")

**Move to Page 7 after 20 seconds :

PageTimer("20000","Page 7")

**Run startup script of current page after 10 seconds :

PageTimer("10000","THIS_SCRIPT")

**Restart current page after 5 seconds :

PageTimer("5000","THIS_PAGE")

**Move to next page if no user interaction for 60 seconds :

PageTimer("60000","IF_IDLE")

Additional Info

 

Running of PageTimer before previously set timer delay has elapsed will reset previous setting and use the new one.

 

For example, if you run:

PageTimer("120000","")

...and before elapsed 120 seconds you call PageTimer again:

PageTimer("180000","")

...previous setting will be substituted with the new one and timer will move to next page after 180 seconds.

Using ExitTimer after PageTimer will cancel PageTimer event.

 

ExitTimer("ms")

Description

 

Waits for specified amount of time (number or numerical variable as command parameter) and then exits (closes) project. If keyboard or mouse click event occurs during delay, ExitTimer is canceled.

 

ExitTimer

Code Example

 

**Exit from project after 120 seconds of user inactivity:

PageTimer("120000")

Additional Info

 

Running of PageTimer before ExitTimer delay has elapsed will cancel ExitTimer event.

 

For example, if you run:

ExitTimer("120000","")

...and before elapsed 120 seconds you call PageTimer:

PageTimer("12000","")

...PageTimer will cancel ExitTimer event.

 

Refresh("FORCED")

Description

 

Redraws project's window and allows processing of other events in project (mouse & keyboard input, running of scripts, etc).

 

Refresh without FORCED parameter is just the standard refresh. It should be good enough in mos cases.

 

FORCED - Forced refresh of entire project window. This option is useful in cases, when the standard Refresh becomes insufficient (some graphically intensive operations). We would recommend to use this parameter only in case of problems with standard refresh.

Refresh

 

Code Example

 

** Perform standard refresh

Refresh("")

 

** Perform FORCED refresh. Use it carefully! It can slowdown your program.

Refresh("FORCED")

 

Additional Info

 

Calling this command is highly recommended in for..next loops, to allow processing of other threads (running routines also present in project) and avoid 'freezing' of application window.