The Asking Stack

In NetLogo we ask turtles and patches to execute blocks of commands:

ask turtle 5 [move eat mate]

ask patch 3 2 [inspect-neighbors update-state]

ask patches [inspect-neighbors update-state]

In the third example we ask the set of all patches to execute a block of commands. In this case each patch will execute the block.

NetLogo runs in a single thread of control. That means that at any given moment only one agent is executing a block of commands. We call this agent the active agent/turtle/patch.

Of course one of the commands an agent executes might be the ask command. For example:

ask turtle 5 [draw-square ask turtle 3 [draw-circle] draw-hexagon]

Turtle 5 draws a square, then asks turtle 3 to draw a circle, then turtle 5 draws a hexagon.

While turtle 3 is drawing a circle, it is the active turtle. Turtle 5 is blocked, waiting for turtle 3 to finish. When turtle 3 is finished, it becomes inactive; turtle 5 is unblocked and once again becomes the active turtle.

Of course turtle 5 could have been asked to execute the block by turtle 0:

ask turtle 0
[
   ask turtle 5
   [
      draw-square
      ask turtle 3
      [
         draw-circle
      ]
   draw-hexagon
   ]
   draw-triangle
]

In this case while turtle 3 draws a circle, turtle 5 is blocked, waiting to draw a hexagon and turtle 0 is blocked waiting to draw a triangle.

At any given point in time we can imagine there is a stack of agents. The agent on top of the stack is active, the others are blocked. Let's call this the asking stack. Agents not on the asking stack are inactive.

In our example, turtle 3 stands on the back of turtle 5. Turtle 5 stands on the back of turtle 0. On whose back does turtle 0 stand? Who is at the bottom of the asking stack?

 

Self vs. Myself

Assume a global variable called winner has been defined:

globals [winner] ; the turtle with the highest score

Here is how turtle 0 might declare himself to be the winner:

ask turtle 0 [set winner self]

To rub it in, turtle 0 might ask the defeated turtle 1 to declare him the winner:

ask turtle 0 [ask turtle 1 [set winner myself]]

Inside a block or procedure self always refers to the active agent while myself refers to the blocked agent just below the active agent on the asking stack.