Use the new ENQUEUE command to add actions to the Action Queue rather than executing them immediately. For example:
for i 1 10 {
ENQUEUE {
SAY $fmt["A count %s",@i] } };
for i 1 10 {
ENQUEUE {
SAY $fmt["B count %s",@i] } }
Putting the above code into a button and clicking on it will result in the bot's chat output resembling this:
A: 1
B: 1
A: 2
B: 2
A: 3
B: 3
A: 4
B: 4
A: 5
B: 5
A: 6
B: 6
A: 7
B: 7
A: 8
B: 8
A: 9
B: 9
A: 10
B: 10
Without the ENQUEUE command, the output would instead be:
A: 1
A: 2
A: 3
A: 4
A: 5
A: 6
A: 7
A: 8
A: 9
A: 10
B: 1
B: 2
B: 3
B: 4
B: 5
B: 6
B: 7
B: 8
B: 9
B: 10
The purpose of ENQUEUE is to allow multiple action sequences to be executed in parallel (as if they are executing at the same time). To achieve the effect of "parallel processing", there are multiple Action Queues, each of which can contain a sequence of actions to be executed in parallel. That is, Magsbot takes items from each queue, one item from each queue at a time, to be processed. Therefore, in order for any two actions to be executed in parallel, they must be in different queues. If two or more actions are in the same queue, then they will simply execute sequentially without any particular benefit.
ENQUEUE has an optional argument that allows the user to specify which queue an action or action sequence should be put into. For example, this command would put the specified action into the queue with index 2:
ENQUEUE 2 { SAY "Woo hoo!" }
If there is no queues with the specified index (which is likely since queues are created and destroyed as needed) then the action will be put into a newly-created queue. If you want to assure that a new queue is created, then you would use index -1.
Within an ENQUEUE command, the variable @queue contains the index of the queue containing the specified action. Update: as of version 2.2, when called outside of an ENQUEUE clause, the @queue function will contain the index of the queue used by the most recently called ENQUEUE. Do NOT depend on an assignment made within an ENQUEUE clause to pass the queue index out of the clause, since the assignment won't take place until the clause (ENQUEUE'd action) executes.
But in most cases, you would not specify an index. When the index is ommitted, then the specified action or actions will be placed into a new queue uniquely associated with the current Local Variable List. This assures that when ENQUEUE is used in Behavior Table actions, the created queue will be unique to that table row, and not with any other table actions that may also have used ENQUEUE. In other words, ENQUEUE'd actions in the Behavior Table will execute in parallel.
As mentioned above, the local variable list is also placed in the action queue when an ENQUEUE command is used. The current list of pertinent AW Attributes is placed in the queue with the associated action as well. Therefore, when a queue item executes, the @atr and $atr functions will return values that were current at the time the ENQUEUE command occurred, not at the time the action is processed. (Note that only attributes that apply to the Event that triggered the action are placed in the queue; @atr and $atr will return the current values for any other AW Attributes.)
Action sequences (such as "MOVETO 21000 -775000; AVTYPE 1; GESTURE 12") are split into individual actions when placed in the queue. Note however that actions such as FOR, WHILE, IF and so on, cannot be split up. Therefore when (for example) a FOR action that has been enqueued executes, the entire loop will execute before another queue item is fetched. For speed, then, you would avoid placing FOR and similar actions within an ENQUEUE. Instead, put the ENQUEUE inside the FOR loop, which will cause multiple actions to be put into the queue! (Be careful about memory though...a 10000 iteration loop with an ENQUEUE inside it will cause 10000 queue items to be created!)
New in 2.1.3:
The QSTATE 0 command allows you to suspend execution of the queue so that it can filled with actions using ENQUEUE without the execution of those action beginning until the QSTATE 1 command is used to restart queue execution.