Class definition
Class is a set of variables and functions which describe properties and behaviour of a turtle (object) in POOL.
It is a convenient way to organize a program which runs multiple turtles.
POOL uses the syntax of a function definition to define also a class. Body of the class-function contains code
which is executed when new turtle is created. This code includes definitions of member functions and variables.
Function which is used to define a class can be considered as a constructor of new turtle. Note:
this function should not output any value.
Most of multi-turtle programming tasks will need a way of communication between turtles in order to exchange
values of variables or mutually call member functions. POOL uses operator @
for this purpose.
All member functions in POOL are public and can be called mutually by turtle-objects. Variables are:
- public, if they are defined as global; public variables can be read with @
operator;
- protected, if they are defined as local; protected variables are not available for other turtles;
- static, if they are defined as shared; single instance of a shared variable is available for all turtles.
Example 1:
to model :p
make "l 0
to fn :x
make "l :x
output :x^:p
end
end
make "t (list (newt $model 2) (newt $model 3) (newt $model 5))
print :t
foreach "z :t [(print who @ :z (fn repcount) @ :z)]
foreach "z :t [print :l @ :z]
Output:
[model1 model2 model3]
model1 1
model2 8
model3 243
1
2
3
Example 2:
to model :h0 :c
to step
rt :a fd :v
"a := :a + rnorm 2
if abs :a > 30 ["a := 30 * sign :a]
"v := :v + rnorm 0.1
if :v < 0 ["v := 0]
end
make "v 3
let "a 0
seth :h0
setpc :c
end
wrap
"n := 5
repeat :n [
"h := 360 * repcount / :n
"c := 1000 * repcount / :n
"t := (newt $model :h :c)
]
ht
"ts := children
let "tick timer [
foreach "t :ts [step @ :t]
if :iter % 100 = 0 [
let "s 0
foreach "t :ts ["s := :s + :v @ :t]
print :s / count :ts
]
] 10
Output:
(program is drawing in the graphics window)
3.60541860738304
3.24327855650685
3.35303447692073
2.92485334993107
3.17479251202894
3.43988973971224
...
See also:
Function definition and call
Remote variables and results of remote calls.
Veriables, data access
Turtle - object
#, turtle - get the turtle
Function as a value
$, func - get the function
Table of Content