Tutorial Details:
Difficulty Level:
Intermediate
Topics Covered: Use of
the try / retry commands in Mindscript.
Assumed Knowledge:
The Basics, My First Program, Tasks, Sub-Routines
etc.
Written By: BILL LANE
BACK
As discussed in Tasks, Sub-Routines etc Spybots are capable
of running more than one sub-routine/ task at a time. This
can lead to problems when more than one task wants to use
the same resource. For example, two tasks try to run the same
motor in opposite directions at the same time. Fortunately,
Mindscript provides a structure to help us deal with this
situation. This is the try
command. Let's have a look at an example:
sub winGame(reps){
try{
Action(62,1, moveDance, reps ,200)
wait 400
stop tasks
}retry on fail
}
In this example we want the Spybot to do a little dance when
we call this sub. To ensure there are no conflicts the command
block has been wrapped around with the try
command. As you can see try
starts and finishes with it's own curly braces and is followed
in this case with retry on fail.
If another sub already has access to the motors then sub winGame
will fail to run. It will then try to run this code block
again. There are three other options: restart
( returns to the start of the sub or task and runs from there)
; abort (continue with
the next command after the try); stop
(go to the end of the sub or task).
It is possible to tell the program which subs have a greater
priority with regards accessing resources. You do this by
using the priority keyword at the top of the sub-routine or
task. The priority can be from 1 (highest priority) to 8 (lowest
priority). Here's winGame with a priority setting:
sub winGame(reps){
priority 1
try{
Action(62,1, moveDance, reps ,200)
wait 400
stop tasks
}retry on fail
}
|