Difference between revisions of "Object Oriented Programming in Script/Sending Messages"
(Created page with "Official SCI Documentation<br /> <div align="center"> Chapter: 1 | Object Oriented Programming i...") |
|||
Line 18: | Line 18: | ||
| | ||
− | + | ==<br /> Sending Messages == | |
+ | |||
+ | The syntax for sending a message to an object is identical to that for a procedure call. The object name (or an expression which evaluates to an object ID) is followed by the message selector and any parameters, all enclosed in parentheses. There are three different kinds of messages which can be sent to objects: | ||
+ | |||
+ | ===<br /> Setting a property: === | ||
+ | <blockquote>A property of an object can be set by sending a message whose message selector is the name of the property followed by a colon (':') followed by the new value of the property: | ||
+ | |||
+ | <blockquote> | ||
+ | <div class="CodeBlockHeader">Code:</div> | ||
+ | <syntaxhighlight lang="sci"> | ||
+ | (ego x:23) or (ego x: 23) | ||
+ | </syntaxhighlight> | ||
+ | </blockquote> | ||
+ | |||
+ | sets the x property of ego to 23. | ||
+ | </blockquote> | ||
+ | |||
+ | ===<br /> Requesting the value of a property: === | ||
+ | <blockquote>The value of a property can be obtained by sending a message with no parameters whose message selector is the name of the property followed by a question mark ('?'): | ||
+ | |||
+ | <blockquote> | ||
+ | <div class="CodeBlockHeader">Code:</div> | ||
+ | <syntaxhighlight lang="sci"> | ||
+ | (ego x?) | ||
+ | </syntaxhighlight> | ||
+ | </blockquote> | ||
+ | |||
+ | will return the value of the x property of ego. | ||
+ | </blockquote> | ||
+ | |||
+ | ===<br /> Invoking a method: === | ||
+ | <blockquote> | ||
+ | A method of an object can be invoked by sending a message with any number of parameters whose message selector is the name of the method followed by a colon (':'): | ||
+ | </blockquote> | ||
+ | |||
+ | <blockquote> | ||
+ | <div class="CodeBlockHeader">Code:</div> | ||
+ | <syntaxhighlight lang="sci"> | ||
+ | (ego moveTo: x y) or (ego moveTo:x y) | ||
+ | </syntaxhighlight> | ||
+ | </blockquote> | ||
+ | |||
+ | tells ego to move to coordinates x and y by invoking the moveTo method of ego. | ||
+ | |||
+ | |||
+ | Any number of messages can be sent in one fell swoop: | ||
+ | |||
+ | <blockquote> | ||
+ | <div class="CodeBlockHeader">Code:</div> | ||
+ | <syntaxhighlight lang="sci"> | ||
+ | (ego | ||
+ | x:50 | ||
+ | y:50 | ||
+ | setMotion: MoveTo 100 100 | ||
+ | setCycle: Reverse self | ||
+ | ) | ||
+ | </syntaxhighlight> | ||
+ | </blockquote> | ||
+ | |||
+ | will position ego at coordinates (50, 50), start him moving to coordinates (100, 100), and set him to cycle in reverse cel order. When multiple messages are sent to an object, the messager in the kernel sends them one at a time in left to right order. In multiple message sends, all parameters are evaluated before the messages are sent. | ||
| |
Revision as of 20:38, 5 December 2015
Sending Messages
The syntax for sending a message to an object is identical to that for a procedure call. The object name (or an expression which evaluates to an object ID) is followed by the message selector and any parameters, all enclosed in parentheses. There are three different kinds of messages which can be sent to objects:
Setting a property:
A property of an object can be set by sending a message whose message selector is the name of the property followed by a colon (':') followed by the new value of the property:
Code:(ego x:23) or (ego x: 23)sets the x property of ego to 23.
Requesting the value of a property:
The value of a property can be obtained by sending a message with no parameters whose message selector is the name of the property followed by a question mark ('?'):
Code:(ego x?)will return the value of the x property of ego.
Invoking a method:
A method of an object can be invoked by sending a message with any number of parameters whose message selector is the name of the method followed by a colon (':'):
Code:(ego moveTo: x y) or (ego moveTo:x y)
tells ego to move to coordinates x and y by invoking the moveTo method of ego.
Any number of messages can be sent in one fell swoop:
Code:(ego x:50 y:50 setMotion: MoveTo 100 100 setCycle: Reverse self )
will position ego at coordinates (50, 50), start him moving to coordinates (100, 100), and set him to cycle in reverse cel order. When multiple messages are sent to an object, the messager in the kernel sends them one at a time in left to right order. In multiple message sends, all parameters are evaluated before the messages are sent.
- Notes
< Previous: Objects Next: An Extended Example >