Hi Michael. What a great question.
One effective ways to make a robot do more than one thing is to use a behaviour-based approach, and the sumo robot does this. In a (simplified) behaviour-based approach, you have a series of triggers, or conditions which might cause the robot to do something, and a series of behaviours, of things the robot might do. The code (or, to be more technical, a routine called the 'arbiter' [which means 'decision maker']) goes through each of the triggers in turn, starting from the least important and going up to the most important, and sees if the trigger is active [meaning the condition is occurring]. If it is, it determines which behaviour corresponds to that trigger, and prepares to run it. However, as it goes through triggers of higher priority, if one of them is active, their behaviour will get to run instead. At the end of looking at all the triggers, it only runs one behaviour.
Here are the triggers and behaviours for the sumo robot:
The 'the script' block allows us to store a (reference to a) routine in a variable, and then call it [run it/invoke it/do it] later. This is handy as it makes for clear, compact code.
Instead of remembering which behaviour to call [or routine to run], the sumo program could remember a name or string representing the routine to run, go through all the triggers and make its decision, and then go through every possible name that it may have remembered and run the corresponding function if it was the chosen one, like this:
This code may be clearer for a beginner, but it is longer and more likely to cause problems, for if you make a mistake typing the name of the behaviour (typing 'Wandr' instead of 'Wander', for example), it will not work right.
You asked if the order is important. It is. Start with the least important behaviour first. More important behaviours go later, and each gets a chance to overturn or change the decision. Only if no trigger of greater importance is active will a less important trigger dictate the next action.
[If you just coded it up to say, 'if foe ahead -> attack' and 'if sees edge -> retreat', then, if there was a moment that both conditions are true, they'd activate one after the other [or worse, at the same time! -- and no robot can not drive forwards and backwards at the same time!] and you'd not get the result that you want, which is to do only one task, whichever is most important.]
You also asked if you can have an 'interrupt'. No, not really. An interrupt stops whatever is going on to do something else, and Enchanting has no support for them. What you could do is make a high priority trigger, and run the corresponding behaviour whenever it is active. Note, however, that the sumo program uses what is called 'co-operative multi-tasking', which means that while a behaviour is running, no other behaviour has a chance to run. I'd typically recommend a behaviour do something really quick and simple, like set the motors to drive forward, because it can do that many times in a second and keep on checking on the conditions affecting the robot. If, however, you run, say, a retreat behaviour that tells the robot to drive backwards 10 cm and turn, while that is happening, no other behaviour has a chance to run (or interrupt it).