Handlers
Handlers are message listeners within a script. An object has a script, which has handlers. The object must have access to a handler for a particular message in order to receive that message and act on it. For more on messages, see Messages.
An object’s behaviors are defined by the way it handles different messages that may be sent to it. When an object receives a message, it will respond and perform a scripted action, but only if it has a script with a handler for that particular message. All scripts consist of one or more handlers for the various messages that the object is interested in handling (including the initial handler of a script, which handles a message by the same name as the script). If an object receives a message for which it doesn’t have a matching handler, it ignores that message.
A SenseTalk script is made up of handlers. A handler is a part of a script that defines what the script will do when a particular message is sent to it. There are three primary types of handlers: command handlers (sometimes called on handlers), function handlers, and generic handlers (also known as to handlers). There are also two special types of handlers, getProp and setProp handlers, discussed in Properties.
Types of Handlers
Command, Function, and Generic Handlers
There are three primary types of message handlers: generic, command, and function handlers. Generic handlers begin with to handle
(or simply to
) and can handle both command and function messages. For simplicity, always use generic handlers unless there is a special need to handle command and function messages differently. Command handlers begin with the word on
and handle messages sent by commands. A command handler typically performs a series of actions. Function handlers, which begin with the word function
, handle function call messages, and return a value.
It should be noted that a command handler may also return a value, and a function handler may perform actions in addition to returning a value. The only real difference between the handler types is the kind of messages they will handle: a command handler will never be run as a result of a function message, and a function handler will never be called by a command message.
If a script contains both a generic handler (to
) and a specific handler (on
or function
) for the same message name, the specific handler always receives priority and receives the message. The to
handler still receives messages of the other type. If a script contains all three types of handlers for a particular message, the on
handler receives command messages by that name, the function
handler receives function messages, and the to
handler is never called.
The following is a very simple handler for a greetTheUser
message:
to greetTheUser
put "Welcome to SenseTalk. Happy scripting!"
end greetTheUser
To
, To Handle
Keyword
Behavior: The to
or to handle
keyword declares a generic handler that can receive both command and function messages. If the handler needs to take different actions depending on how it was called, the messageType
function can be used to find out whether the handler was called as a command or as a function.
When the script receives either a command message or a function message matching messageName, the statements of this handler are executed. The incoming parameter values passed to the handler is assigned to the corresponding parameter names (paramName1 , paramName2, paramNameN) declared following the messageName.
Syntax:
to {handle} messageName {{with | of | given} {a | an | the} paramName1 {, paramName2 ...} }
statements
end [messageName | handler | to {handle}]
Example:
to handle increaseSize given amount
if amount is empty then add 1 to my size else add amount to my size
end increaseSize