New turtles (asynchronous constructor)

(anewturtles n)
anewturtles n [instructions]
(anewturtles n [instructions] expr1 expr2 ...)
anewturtles n function)
(anewturtles n function expr1 expr2 ...)

Creates n new turtles simultaneously. A list of instructions or a function passed as an argument is used as constructor of each new turtle. The constructors are executed in parallel. The turtle which called anewturtles instruction is not waiting for the constructors completion. The constructor's arguments can be passed as optional expressions (all new turtles use the same argument values).

Example 1:

"t := (anewturtles 3)
print who
print children
foreach "ti :t [(print who @ :ti parent @ :ti )]

Output:

first
[t1 t2 t3]
t1 first
t2 first
t3 first

Example 2:

"t := anewturtles 3 [
  to getx op :x end
  "x := random 10
  print who
]
;print :x @ :t,3 ;error: variable :x may not be ready yet
print getx @ :t,3

Output:

t2
t1
t3
2

Example 3:

to model :dt
  repeat 3 [(print who repcountwait :dt]
  "x := :dt ;global variable (public)
end

"t := (anewturtles 2 $model 10)
print :t

Output:
Instruction print :t is executed before constructors of new turtles are completed.

model1 1
[model1 model2]
model2 1
model1 2
model2 2
model1 3
model2 3

See also:

Turtle - object

Table of Content