~ubuntu-branches/ubuntu/vivid/inform/vivid

« back to all changes in this revision

Viewing changes to inform-6.31.1/manual/s5.html

  • Committer: Bazaar Package Importer
  • Author(s): Jan Christoph Nordholz
  • Date: 2008-05-26 22:09:44 UTC
  • mfrom: (2.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080526220944-ba7phz0d1k4vo7wx
Tags: 6.31.1+dfsg-1
* Remove a considerable number of files from the package
  due to unacceptable licensing terms.
* Repair library symlinks.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 
2
<html>
 
3
<head>
 
4
<title>DM4 &#167;5: Introducing messages and classes</title>
 
5
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
 
6
<link rel="stylesheet" type="text/css" href="dm4.css">
 
7
</head>
 
8
<body>
 
9
<p class="navbar">
 
10
 <a href="index.html">home</a> /
 
11
 <a href="contents.html">contents</a> /
 
12
 <a href="ch2.html" title="Chapter II: Introduction to Designing">chapter II</a> /
 
13
 <a href="s4.html" title="&#167;4: &#8216;Ruins&#8217; begun">prev</a> /
 
14
 <a href="s6.html" title="&#167;6: Actions and reactions">next</a> /
 
15
 <a href="dm4index.html">index</a>
 
16
</p>
 
17
<div class="page">
 
18
<a id="p92" name="p92"></a>
 
19
<h2>&#167;5 &nbsp; Introducing messages and classes</h2>
 
20
 
 
21
<blockquote>
 
22
On a round ball<br>
 
23
A workman that hath copies by, can lay<br>
 
24
An Europe, Afrique and an Asia,<br>
 
25
And quickly make that, which was nothing, All.<br>
 
26
&#8212; John Donne (1571?&#8211;1631), <i>Valediction: Of Weeping</i>
 
27
</blockquote>
 
28
 
 
29
<p class="normal"><span class="atleft"><img src="dm4-092_1.jpg" alt=""></span>
 
30
Though <a href="s4.html">&#167;4</a> was a little vague 
 
31
in saying &#8220;the library asks the mushroom if it would like to react&#8221;, 
 
32
something basic was happening in Inform terms: the object 
 
33
<code>InformLibrary</code> was sending the message <code>before</code> to the 
 
34
object <code>mushroom</code>. Much more on how actions take place in 
 
35
<a href="s6.html">&#167;6</a>, but here is roughly what the library does 
 
36
if the player types &#8220;eat mushroom&#8221;:</p>
 
37
 
 
38
<p class="lynxonly"></p>
 
39
<pre class="code">
 
40
if (mushroom.before() == false) {
 
41
    remove mushroom;
 
42
    if (mushroom.after() == false)
 
43
        print &quot;You eat the mushroom. Not bad.^&quot;;
 
44
}
 
45
</pre>
 
46
 
 
47
<p class="normal">The library sends the message <code>before</code> to ask the 
 
48
<code>mushroom</code> if it minds being eaten; then, if not, it consumes 
 
49
the mushroom; then it sends the message <code>after</code> to ask if 
 
50
the mushroom wishes to react in some way; then, if not, it prints 
 
51
the usual eating-something text. In response to the messages <code>before</code>
 
52
and <code>after</code>, the mushroom is expected to reply either <code>true</code>, 
 
53
meaning &#8220;I'll take over from here&#8221;, or <code>false</code>, 
 
54
meaning &#8220;carry on&#8221;.</p>
 
55
 
 
56
<p class="indent">Most of the other properties in 
 
57
<a href="s4.html">&#167;4</a> are also receiving messages.
 
58
For example, the message</p>
 
59
 
 
60
<p class="lynxonly"></p>
 
61
<pre class="code">mushroom.description();</pre>
 
62
 
 
63
<p class="normal">is sent when the player tries to examine the 
 
64
mushroom: if the reply is <code>false</code> then the library prints 
 
65
&#8220;You see nothing special about the speckled mushroom.&#8221;
 
66
Now the mushroom was set up with</p>
 
67
 
 
68
<p class="lynxonly"></p>
 
69
<pre class="code">
 
70
description
 
71
    &quot;The mushroom is capped with blotches, and you aren't at all
 
72
    sure it's not a toadstool.&quot;,
 
73
</pre>
 
74
 
 
75
<a id="p93" name="p93"></a>
 
76
<p class="normal">which doesn't look like a rule for receiving a message, 
 
77
but it is one all the same: it means &#8220;print this text out, print 
 
78
a new-line and reply <code>true</code>&#8221;. A more complicated 
 
79
rule could have been given instead, as in the following elaboration
 
80
of the stone-cut steps in &#8216;Ruins&#8217;:</p>
 
81
 
 
82
<p class="lynxonly"></p>
 
83
<pre class="code">
 
84
description [;
 
85
    print &quot;The cracked and worn steps descend into a dim chamber.
 
86
           Yours might &quot;;
 
87
    if (Square_Chamber hasnt visited)
 
88
        print &quot;be the first feet to tread&quot;;
 
89
    else print &quot;have been the first feet to have trodden&quot;;
 
90
    &quot; them for five hundred years. On the top step is inscribed
 
91
    the glyph Q1.&quot;;
 
92
],
 
93
</pre>
 
94
 
 
95
<p class="normal"><code>visited</code> is an attribute which is currently held 
 
96
only by rooms which the player has been to. The glyphs will come into 
 
97
the game later on.</p>
 
98
 
 
99
<p class="indent">The library can send out about 40 different kinds 
 
100
of message, <code>before</code>and <code>description</code> being 
 
101
two of these. The more interesting an object is, the more ingeniously 
 
102
it will respond to these messages. An object which ignores all incoming 
 
103
messages will be lifeless and inert in play, like a small stone.</p>
 
104
 
 
105
<p class="aside"><span class="warning">&#9650;</span>
 
106
Some properties are just properties, and don't receive messages. 
 
107
Nobody ever sends a <code>name</code> message, for instance: 
 
108
the <code>name</code> property is just what it seems to be, a list
 
109
of words.</p>
 
110
 
 
111
<p class="dotbreak">� � � � �</p>
 
112
 
 
113
<p class="normal">So the library is sending out messages to your 
 
114
objects all the time during play. Your objects can also send each 
 
115
other messages, including &#8220;new&#8221; ones that the library 
 
116
would never send. It's sometimes convenient to use these to trigger
 
117
off happenings in the game. For example, one way to provide hints 
 
118
in &#8216;Ruins&#8217; might be to include a macaw which squawks 
 
119
from time to time, for a variety of reasons:</p>
 
120
 
 
121
<p class="lynxonly"></p>
 
122
<pre class="code">
 
123
Object -&gt; macaw &quot;red-tailed macaw&quot;
 
124
  with name 'red' 'tailed' 'red-tailed' 'macaw' 'bird',
 
125
       initial &quot;A red-tailed macaw eyes you from an upper branch.&quot;,
 
126
       description &quot;Beautiful plumage.&quot;,
 
127
       before [;
 
128
           Take: &quot;The macaw flutters effortlessly out of reach.&quot;;
 
129
       ],
 
130
       squawk [ utterance;
 
131
           if (self in location)<a id="p94" name="p94"></a>
 
132
               print &quot;The macaw squawks, ~&quot;, (string) utterance,
 
133
                     &quot;! &quot;, (string) utterance, &quot;!~^^&quot;;
 
134
       ],
 
135
  has  animate;
 
136
</pre>
 
137
 
 
138
<p class="normal">(For the final version of &#8216;Ruins&#8217; the 
 
139
designer thought better of the macaw and removed it, but it still 
 
140
makes a good example.) We might then, for instance, change the <code>after</code> 
 
141
rule for dropping the mushroom to read:</p>
 
142
 
 
143
<p class="lynxonly"></p>
 
144
<pre class="code">
 
145
Drop: macaw.squawk(&quot;Drop the mushroom&quot;);
 
146
      &quot;The mushroom drops to the ground, battered slightly.&quot;;
 
147
</pre>
 
148
 
 
149
<p class="normal">so that the maddening creature would squawk &#8220;Drop 
 
150
the mushroom! Drop the mushroom!&#8221; each time this was done. At 
 
151
present it would be an error to send a <code>squawk</code> message to any object 
 
152
other than the macaw, since only the macaw has been given a rule 
 
153
telling it what to do if it receives one.</p>
 
154
 
 
155
<p class="dotbreak">� � � � �</p>
 
156
 
 
157
<p class="normal">In most games there are groups of objects with 
 
158
certain rules in common, which it would be tiresome to have to write 
 
159
out many times. For making such a group, a class definition is 
 
160
simpler and more elegant. These closely resemble object definitions, 
 
161
but since they define prototypes rather than actual things, they 
 
162
have no initial location. (An individual tree may be somewhere, 
 
163
but the concept of being a tree has no particular place.) So the 
 
164
&#8216;header&#8217; part of the definition is simpler.</p>
 
165
 
 
166
<p class="indent">For example, the scoring system in &#8216;Ruins&#8217; 
 
167
works as follows: the player, an archaeologist of the old school, gets 
 
168
a certain number of points for each &#8216;treasure&#8217; (i.e., 
 
169
cultural artifact) he can filch and put away into his packing case. 
 
170
Treasures clearly have rules in common, and the following class 
 
171
defines them:</p>
 
172
 
 
173
<p class="lynxonly"></p>
 
174
<pre class="code">
 
175
Class Treasure
 
176
  with cultural_value 5, photographed_in_situ false,
 
177
       before [;
 
178
           Take, Remove:
 
179
               if (self in packing_case)
 
180
                   &quot;Unpacking such a priceless artifact had best wait
 
181
                   until the Carnegie Institution can do it.&quot;;
 
182
               if (self.photographed_in_situ == false)
 
183
                   &quot;This is the 1930s, not the bad old days. Taking an
 
184
                   artifact without recording its context is simply
 
185
                   looting.&quot;;
 
186
           Photograph:<a id="p95" name="p95"></a>
 
187
               if (self has moved)
 
188
                   &quot;What, and fake the archaeological record?&quot;;
 
189
               if (self.photographed_in_situ) 
 
190
                   &quot;Not again.&quot;;
 
191
       ],
 
192
       after [;
 
193
           Insert:
 
194
               if (second == packing_case)
 
195
               {   score = score + self.cultural_value;
 
196
                   &quot;Safely packed away.&quot;;
 
197
               }
 
198
           Photograph: self.photographed_in_situ = true;
 
199
       ];
 
200
</pre>
 
201
 
 
202
<p class="normal">(The packing case won't be defined until 
 
203
<a href="s12.html">&#167;12</a>, which is about containers.) 
 
204
Note that <code>self</code> is a variable, which always means 
 
205
&#8220;whatever object I am&#8221;. If we used it in the definition 
 
206
of the mushroom it would mean the mushroom: used here, it means 
 
207
whatever treasure happens to be being dealt with. Explanations
 
208
about <code>Insert</code> and <code>Remove</code> will come later 
 
209
(in <a href="s12.html">&#167;12</a>). The action <code>Photograph</code> 
 
210
is not one of the standard actions built in to the library, and will 
 
211
be added to &#8216;Ruins&#8217; in the next section.</p>
 
212
 
 
213
<p class="indent">An object of the class <code>Treasure</code> automatically 
 
214
inherits the properties and attributes given in the class definition. 
 
215
Here for instance is an artifact which will eventually be found in 
 
216
the Stooped Corridor of &#8216;Ruins&#8217;:</p>
 
217
 
 
218
<p class="lynxonly"></p>
 
219
<pre class="code">
 
220
Treasure -&gt; statuette &quot;pygmy statuette&quot;
 
221
  with name 'snake' 'mayan' 'pygmy' 'spirit' 'precious' 'statuette',
 
222
       description
 
223
          &quot;A menacing, almost cartoon-like statuette of a pygmy spirit
 
224
           with a snake around its neck.&quot;,
 
225
       initial &quot;A precious Mayan statuette rests here!&quot;;
 
226
</pre>
 
227
 
 
228
<p class="normal">From <code>Treasure</code>, this statuette inherits 
 
229
a <code>cultural_value</code> score of 5 and the rules about taking 
 
230
and dropping treasures. If it had itself set <code>cultural_value</code>
 
231
to 15, say, then the value would be 15, because the object's actual 
 
232
definition always takes priority over anything the class might have 
 
233
specified. Another of the five &#8216;Ruins&#8217; treasures, which 
 
234
will be found in the Burial Shaft, has a subtlety in its definition:</p>
 
235
 
 
236
<p class="lynxonly"></p>
 
237
<pre class="code">
 
238
Treasure -&gt; honeycomb &quot;ancient honeycomb&quot;
 
239
  with article &quot;an&quot;,
 
240
       name 'ancient' 'old' 'honey' 'honeycomb',
 
241
       description &quot;Perhaps some kind of funerary votive offering.&quot;,
 
242
       initial
 
243
          &quot;An exquisitely preserved, ancient honeycomb rests here!&quot;,<a id="p96" name="p96"></a>
 
244
       after [;
 
245
           Eat: &quot;Perhaps the most expensive meal of your life.
 
246
               The honey tastes odd, perhaps because it was used to
 
247
               store the entrails of the Lord buried here, but still
 
248
               like honey.&quot;;
 
249
       ],
 
250
  has  edible;
 
251
</pre>
 
252
 
 
253
<p class="normal">The subtlety is that the honeycomb now has two 
 
254
<code>after</code> rules: a new one of its own, plus the existing 
 
255
one that all treasures have. Both apply, but the new one happens 
 
256
first.</p>
 
257
 
 
258
<p class="aside"><span class="warning">&#9650;&#9650;</span>
 
259
So comparing <code>cultural_value</code> and <code>after</code>, there 
 
260
seems to be an inconsistency. In the case of <code>cultural_value</code>, 
 
261
an object's own given value wiped out the value from the class, 
 
262
but in the case of <code>after</code>, the two values were joined 
 
263
up into a list. Why? The reason is that some of the library's properties 
 
264
are &#8220;additive&#8221;, so that their values accumulate into 
 
265
a list when class inheritance takes place. Three useful examples are
 
266
<code>before</code>, <code>after</code> and <code>name</code>.</p>
 
267
 
 
268
<p class="aside"><span class="warning">&#9650;&#9650;</span>
 
269
Non-library properties you invent (like <code>squawk</code> or 
 
270
<code>cultural_value</code>) will never be additive, unless you declare 
 
271
them so with a directive like</p>
 
272
 
 
273
<p class="lynxonly"></p>
 
274
<pre class="code">Property additive squawk;</pre>
 
275
 
 
276
<p class="aside">before <code>squawk</code> is otherwise mentioned. 
 
277
(Or you could imitate similar kinds of inheritance using the 
 
278
superclass operator.)</p>
 
279
 
 
280
<p class="aside"><span class="warning"><b>&#8226;</b>
 
281
<b>REFERENCES</b></span><br>
 
282
See &#8216;Balances&#8217; for an extensive use of message-sending. 
 
283
The game defines several complicated classes, among them the white 
 
284
cube, spell and scroll classes. &nbsp;
 
285
<span class="warning"><b>&#8226;</b></span>&#8216;Advent&#8217;
 
286
has a treasure-class similar to this one, and uses class definitions 
 
287
for the many similar maze and dead-end rooms, as well as the sides 
 
288
of the fissure. &nbsp;
 
289
<span class="warning"><b>&#8226;</b></span>&#8216;Toyshop&#8217;
 
290
contains one easy class (the wax candles) and one unusually hard one 
 
291
(the building blocks). &nbsp;
 
292
<span class="warning"><b>&#8226;</b></span>Class definitions can be worthwhile 
 
293
even when as few as two objects use them, as can be seen from the two 
 
294
kittens in &#8216;Alice Through the Looking-Glass&#8217;.</p>
 
295
 
 
296
</div>
 
297
<p class="navbar">
 
298
 <a href="index.html">home</a> /
 
299
 <a href="contents.html">contents</a> /
 
300
 <a href="ch2.html" title="Chapter II: Introduction to Designing">chapter II</a> /
 
301
 <a href="s4.html" title="&#167;4: &#8216;Ruins&#8217; begun">prev</a> /
 
302
 <a href="s6.html" title="&#167;6: Actions and reactions">next</a> /
 
303
 <a href="dm4index.html">index</a>
 
304
</p>
 
305
</body>
 
306
</html>
 
307