Event and signal handling functions
to onevent :arg1 :arg2 ...
instructions
end
to onsignalname :turtle :data
instructions
end
to onsignal :name :turtle :data
instructions
end
Events are actions which occur during the program execution in order difficult to predict a priori
when the code is written. They include actions on the user interface like mouse moves, cicks, keyboard hits and
actions of turtles like collisions or edge crossing. Events can launch default or user-written handling functions.
Signals are a special case of events. They are raised programmatically by turtles and timers. Signals are used
to synchronize parallel work of multiple turtles.
In order to implement a handling function for an event you should create the function with the name and arguments
corresponding to the event. Name of the function is made of prefix "on" and the event name.
Signal handling function should be named as "onsignal" followed by the signal name; arguments for the signal handling
function are: :turtle
(a turtle which has sent the signal) and :data
(a variable which
contains additional information added to the signal when it was raised). Function named "onsignal" without any
following name handles all signals which were not handled by dedicated "onsignalnazwa" functions;
:name
argument with the signal name is available in this case.
Handling functions are executed as separate tasks of turtles. Tasks are queued in turtle's task queues in the
order of raised events.
Note 1: Definition of a handling function is not required to use all arguments available for an event, however
it is required to preserve the order of arguments as documented in opisie zdarzenia.
Note 2: Event (signal) handling function is active for the turtle which contains definition. It is also visible
bot not active for child turtles. Instructions eventenable
,
eventdisable
can be used to activate and disactivate the handling
function after it was defined.
Example 1:
to onmouseclick :mousepos
print :mousepos
end
Output:
Position of the mouse is written to the text output after each click.
{-295 130}
{119 18}
{-14 -100}
{87 145}
Example 2:
to onsignaltick :turtle :data
(print :turtle :data)
end
"t := (timer "tick 300 50 4)
Output:
first {1 50.0078125 50.0078125}
first {2 301.0234375 351.03125}
first {3 302.015625 653.046875}
first {4 302.015625 955.0625}
Example 3:
to onsignal :name :turtle
(print "signal :name "from :turtle)
end
to onsignaltwo
print "dedicated_handler_two
end
signal "one
signal "two
signal "three
Output:
signal one from first
dedicated_handler_two
signal three from first
See also:
List of events in POOL
Function definition and the call to a function
Table of Content