Interaction between agents
Use ask to evaluate a block of statements in the context of one agent or a collection of agents. The receiver is the agent named by self; the agent that started the ask is myself.
Ask an agent or population​
species observer {
agent target;
reflex communicate when: target != nil {
ask target {
write "Sender: " + myself.name;
write "Receiver: " + self.name;
}
}
}
The target can also be a list of agents or a species population. Use the as: facet when the target's species cannot be inferred from its expression. The ask statement reference describes its facets and scope.
Find nearby agents​
The spatial operator at_distance selects agents within a specified distance from the current agent. Use it with ask to interact with nearby agents. Other spatial queries, including closest_to, are listed in the spatial operator reference.

model connect_the_neighbors
global {
geometry shape <- square(100);
float movement_range <- 0.2;
float connection_range <- 10.0;
int number_of_agents <- 100;
init {
create point_agent number: number_of_agents;
}
}
species point_agent {
reflex move {
location <- location + {
rnd(-movement_range, movement_range),
rnd(-movement_range, movement_range)
};
}
aspect default {
draw circle(1) color: #yellow border: #black;
ask point_agent at_distance(connection_range) {
if (self != myself) {
draw polyline([myself.location, self.location]) color: #black;
}
}
}
}
experiment connect_the_neighbors mode: gui {
output {
display network {
species point_agent aspect: default;
}
}
}