New objects (asynchronous constructor)

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

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

Object in POOL is a simplified version of a turtle. Objects do not have graphics representation and do not handle events related to graphics (e.g. mouse clicks). Objects use less memory, they are created faster and also execute their tasks faster than turtles.

Example 1:

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

Output:

first
[o1 o2 o3]
o1 first
o2 first
o3 first

Example 2:

"t := anewobjects 3 [
  to getx op :x end
  "x := random 10
  print who
]

;print :x @ :t,3 ;error: variable :x may be not ready yet
print getx @ :t,3

Output:

o1
o2
o3
5

Example 3:

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

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

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

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

See also:

Turtle - object
Synchronization of multiple objects

Table of Content