For LaTeX: Start each chapter and appendix on a new page.
[ibg.git] / chapters / 04.rst
1 .. raw:: latex
2
3    \newpage
4
5 ======================
6  Reviewing the basics
7 ======================
8
9 .. epigraph::
10
11    | *G was a gamester, who had but ill-luck;*
12    | *H was a hunter, and hunted a buck.*
13
14 .. only:: html
15
16   .. image:: /images/picG.png
17      :align: left
18
19 .. raw:: latex
20
21     \dropcap{g}
22
23 oing through the design of our first game in the previous chapter has
24 introduced all sorts of Inform concepts, often without giving you much
25 detail about what's been happening.  So let's review some of what we've
26 learnt so far, in a slightly more organised fashion.  We'll talk about
27 "Constants and variables" on page 49, "Object definitions" on page 50,
28 "Object relationships -- the object tree" on page 52, "Things in quotes" on
29 page 55, and "Routines and statements" on page 56.
30
31 Constants and variables
32 =======================
33
34 Superficially similar, constants and variables are actually very different
35 beasts.
36
37 .. rubric:: Constants
38
39 A **constant** is a name to which a value is given once and once only; you
40 can't later use that name to stand for a different value.  Think of it as a
41 stone tablet on which you carve a number: a carving can't be undone, so
42 that you see the same number every time you look at the stone.
43
44 So far, we've seen a ``Constant`` being set up with its value as a string
45 of characters::
46
47     Constant Story "Heidi";
48
49 and as a number::
50
51     Constant MAX_CARRIED 1;
52
53 Those two examples represent the most common ways in which constants are
54 used in Inform.
55
56 .. rubric:: Variables
57
58 A **variable** is a name to which a value is given, but that value can be
59 changed to a different one at any time.  Think of it as a blackboard on
60 which you mark a number in chalk: whenever you need to, just wipe the board
61 and write up a new number.
62
63 We haven't set up any variables of our own yet, though we've used a couple
64 which the library created like this::
65
66     Global location;
67     Global deadflag;
68
69 The value of a **global variable** created in this way is initially 0, but
70 you can change it at any time.  For example, we used the statement::
71
72      location = before_cottage;
73
74 to reset the value of the location variable to the ``before_cottage``
75 object, and we wrote::
76
77      if (nest in branch) deadflag = 2;
78
79 to reset the value of the ``deadflag`` variable to 2.
80
81 Later, we'll talk about the **local variable** (see "Routines" on page 179)
82 and about using object properties as variables (see "Objects" on page 177).
83
84 Object definitions
85 ==================
86
87 The most important information you should have gleaned from the previous
88 chapter is that your entire game is defined as a series of objects.  Each
89 room is an object, each item that the player sees and touches is an object;
90 indeed the player herself is also an object (one that's automatically
91 defined by the library).
92
93 The general model of an **object** definition looks like this::
94
95         Object      obj_id   "external_name"   parent_obj_id
96            with     property    value ,
97                     property    value ,
98                     ...
99                     property    value ,
100            has      attribute    attribute   ... attribute
101            ;
102
103 The definition starts with the word ``Object`` and ends with a semicolon;
104 in between are three major blocks of information:
105
106 * immediately after the word ``Object`` is the header information;
107 * the word ``with`` introduces the object's **properties**;
108 * the word ``has`` introduces the object's **attributes**.
109
110 .. rubric:: Object headers
111
112 An object header comprises up to three items, all optional:
113
114 * An internal ``obj_id`` by which other objects refer to this object.  It's
115   a single word (though it can contain digits and underscores) of up to
116   thirty-two characters, and it must be unique within the game.  You can
117   omit the ``obj_id`` if this object isn't referred to by any other
118   objects.
119
120   For example: ``bird``, ``tree``, ``top_of_tree``.
121
122 * An ``external_name``, in double quotes, which is what the interpreter
123   uses when referring to the object.  It can be one or more words, and need
124   not be unique (for instance, you might have several ``"Somewhere in the
125   desert"`` rooms).  Although not mandatory, it's best to give *every*
126   object an ``external_name``.  For example: ``"baby bird"``, ``"tall
127   sycamore tree"``, ``"At the top of the tree"``.
128
129 * The internal ``obj_id`` of another object which is the initial location
130   of this object (its "parent" -- see the next section) at the start of the
131   game.  This is omitted from objects which have no initial parent; it's
132   *always* omitted from a room.
133
134   For example: the definition of the ``bird`` starts like this, specifying
135   that at the start of the game, it can be found in the ``forest`` room
136   (though later the player character will pick it up and move it around)::
137
138       Object   bird "baby bird" forest
139       ...
140
141   The ``tree`` starts like this; the only real difference is that, because
142   the player character can't move a ``scenery`` object, it's always going
143   to be in the ``clearing``::
144
145       Object   tree "tall sycamore tree" clearing
146       ...
147
148   .. note::
149
150      There's an alternative method for defining an object's initial
151      location, using "arrows" rather than the parent's internal ``obj_id``.
152      For example, the definition of the bird could have started like this::
153
154          Object   -> bird "baby bird"
155          ...
156
157      We don't use the arrows method in this guide, though we do describe
158      how it works in "Setting up the object tree" on page 185.
159
160 .. rubric:: Object properties
161
162 An object's property definitions are introduced by the ``with`` keyword.
163 An object can have any number of properties, and they can be defined in any
164 order.  Each definition has two parts: a name, and a value; there's a space
165 between the two parts, and a comma at the end.
166
167 Think of each property as a variable which is specifically associated with
168 that object.  The variable's initial setting is the supplied value; if
169 necessary, it can be reset to other values during play (though in fact most
170 property values don't change in this way).
171
172 Here are examples of the properties that we've come across so far::
173
174     description "The nest is carefully woven of twigs and moss.",
175     e_to forest,
176     name 'baby' 'bird' 'nestling',
177     each_turn [; if (nest in branch) deadflag = 2; ],
178
179 By happy coincidence, those examples also demonstrate most of the different
180 types of value which can be assigned to a property.  The value associated
181 with the ``description`` property in this particular example is a string of
182 characters in double quotes; the value associated with this ``e_to``
183 property is the internal identity of an object; the ``name`` property is a
184 bit unusual -- its value is a list of dictionary words, each in single
185 quotes; the ``each_turn`` property has a value which is an **embedded
186 routine** (see "Embedded routines" on page 58).  The only other type of
187 value which is commonly found is a simple number; for example::
188
189      capacity 10,
190
191 In all, the library defines around forty-eight standard properties -- like
192 ``name`` and ``each_turn`` -- which you can associate with your objects;
193 there's a complete list in "Object properties" on page 266.  And in
194 "William Tell: in his prime" on page 91 we show you how to invent your own
195 property variables.
196
197 .. rubric:: Object attributes
198
199 An object's attribute list is introduced by the ``has`` keyword.  An object
200 can have any number of attributes, and they can be listed in any order,
201 with a space between each.
202
203 As with properties, you can think of each attribute as a variable which is
204 specifically associated with that object.  However, an attribute is a much
205 more limited form of variable, since it can have only two possible states:
206 present, and absent (also known as set/clear, on/off, or true/false;
207 incidentally, a two-state variable like this is often called a **flag**).
208 Initially, an attribute is either present (if you mention its name in the
209 list) or absent (otherwise); if necessary, its state can change during play
210 (and this is relatively common).  We often say that a certain object
211 currently *has* a certain attribute, or that conversely it *hasn't* got it.
212
213 The attributes that we've come across so far are::
214
215      container light open scenery static supporter
216
217 Each of those answers a question: Is this object a container?  Does it
218 provide light?  and so on.  If the attribute is present then the answer is
219 Yes; if the attribute isn't present, the answer is No.
220
221 The library defines around thirty standard attributes, listed in "Object
222 attributes" on page 269.  Although you *can* devise additional attributes
223 -- see "Common properties and attributes" on page 185 -- in practice you
224 seldom need to.
225
226 Object relationships -- the object tree
227 =======================================
228
229 Not only is your game composed entirely of objects, but also Inform takes
230 great care to keep track of the relationships between those objects.  By
231 "relationship" we don't mean that Walter is Wilhelm's son, while Helga and
232 Wilhelm are just good friends; it's a much more comprehensive exercise in
233 recording exactly where each object is located, relative to the other
234 objects in the game.
235
236 Despite what we just said, Inform relationships *are* managed in terms of
237 **parent** and **child** objects, though in a much broader sense than
238 Wilhelm and Walter.  When the player character is in a particular room --
239 for example the forest -- we can say that:
240
241 * the forest object is *the* parent of the player object, or alternatively
242 * the player object is *a* child of the forest object.
243
244 Also, if the player is carrying an object -- for example the nest -- we say
245 that:
246
247 * the player object is *the* parent of the nest object, or that
248 * the nest object is *a* child of the player object.
249
250 Note the emphasis there: an object has exactly *one* parent (or no parent
251 at all), but can have *any number* of child objects (including none).
252
253 For an example of an object having more than one child, think about the way
254 we defined the nest and tree objects::
255
256     Object   nest "bird's nest" clearing
257     ...
258
259     Object   tree "tall sycamore tree" clearing
260     ...
261
262 We used the third of the header items to say that the clearing was the
263 parent of the nest, and also that the clearing was the parent of the tree;
264 that is, both nest and tree are child objects of the clearing.
265
266 .. note::
267
268    A "room" isn't anything magical; it's just an object which *never* has a
269    parent, and which *may* from time to time have the player object as a
270    child.
271
272 When we defined the bird, we placed it in the forest, like so::
273
274     Object   bird "baby bird" forest
275     ...
276
277 We didn't place any other objects in that room, so at the start of the game
278 the forest was the parent of the bird (and the bird was the only child of
279 the forest).  But what happens when the player character, initially in the
280 ``before_cottage`` room, goes EAST to the forest?  Answer: the player's
281 parent is now the forest, and the forest has two children -- the bird *and*
282 the player.  This is a key principle of the way Inform manages its objects:
283 the parent--child relationships between objects change continuously, often
284 dramatically, as the game progresses.
285
286 Another example of this: suppose the player character picks up the bird.
287 This causes another change in the relationships.  The bird is now a child
288 of the player (and *not* of the forest), and the player is both a parent
289 (of the bird) and a child (of the forest).
290
291 In this diagram, we show how the object relationships change during the
292 course of the game.  The straight lines represent parent--child
293 relationships, with the parent object at the top of the line, and the child
294 object at the bottom.
295
296 .. list-table::
297    :widths: 1 3 5
298
299    * - 1.
300      - At the start of the game:
301      - .. image:: /images/heidiobj1.*
302
303    * - 2.
304      - The player types: ``GO EAST``
305      - .. image:: /images/heidiobj2.*
306
307    * - 3.
308      - The player types: ``TAKE THE BIRD``
309      - .. image:: /images/heidiobj3.*
310
311    * - 4.
312      - The player types: ``GO NORTHEAST``
313      - .. image:: /images/heidiobj4.*
314
315    * - 5.
316      - The player types: ``PUT BIRD IN NEST``
317      - .. image:: /images/heidiobj5.*
318
319    * - 6.
320      - The player types: ``TAKE NEST``
321      - .. image:: /images/heidiobj6.*
322
323    * - 7.
324      - The player types: ``UP``
325      - .. image:: /images/heidiobj7.*
326
327    * - 8.
328      - The player types: ``PUT NEST ON BRANCH``
329      - .. image:: /images/heidiobj8.*
330
331 In this short example, we've taken a lot of time and space to spell out
332 exactly how the objects relationship patterns -- generally known as the
333 **object tree** -- appear at each stage.  Normally you wouldn't bother with
334 this much detail (a) because the interpreter does most of the work for you,
335 and (b) because in a real game there are usually too many objects for you
336 to keep track of.  What's important is that you understand the basic
337 principles: at any moment in time an object either has no parent (which
338 probably means either that it's a room, or that it's floating in hyperspace
339 and not currently part of the game) or exactly one parent -- the object
340 that it's "in" or "on" or "a part of".  However, there's no restriction on
341 the number of children that an object can have.
342
343 There's a practical use for these relationships, covered in detail further
344 on.  As a designer, you can refer to the current parent or children of any
345 given object with the ``parent``, ``child`` and ``children`` routines, and
346 this is one feature that you will be using frequently.  There are also
347 other routines associated with the object tree, to help you keep track of
348 the objects or move them around.  We'll see them one by one in the next
349 chapters.  For a quick summary, see "Objects" on page 177.
350
351 Things in quotes
352 ================
353
354 Inform makes careful distinction between double and single quotes.
355
356 .. rubric:: Double quotes
357
358 Double quotes "..." surround a **string** -- a letter, a word, a paragraph,
359 or almost any number of characters -- which you want the interpreter to
360 display while the game is being played.  You can use the tilde ``~`` to
361 represent a double quote inside the string, and the circumflex ``^`` to
362 represent a newline (line break) character.  Upper-case and lower-case
363 letters are treated as different.
364
365 A long string can be split over several lines; Inform transforms each line
366 break (and any spaces around it) into a single space (extra spaces not at a
367 line break are preserved, though).  These two strings are equivalent::
368
369     "This is a      string of characters."
370
371     "This
372       is
373             a    string
374                        of characters."
375
376 When the interpreter displays a long character string -- for example, while
377 describing a feature-packed room -- it employs automatic word-wrapping to
378 fit the text to the player's screen.  This is where you might insert ``^``
379 characters to force line breaks to appear, thus presenting the text as a
380 series of paragraphs.  So far, we've seen strings used as the value of a
381 ``Constant``::
382
383     Constant Headline
384           "^A simple Inform example
385            ^by Roger Firth and Sonja Kesserich.^";
386
387 which could equally have been defined thus::
388
389     Constant Headline
390           "^A simple Inform example^by Roger Firth and Sonja Kesserich.^";
391
392 and as the value of an object description property::
393
394     description "Too young to fly, the nestling tweets helplessly.",
395
396 Later, you'll find that they're also very common in ``print`` statements.
397
398 .. rubric:: Single quotes
399
400 Single quotes '...' surround a **dictionary word**.  This has to be a
401 single word -- no spaces -- and generally contains only letters (and
402 occasionally numbers and hyphens), though you can use ``^`` to represent an
403 apostrophe inside the word.  Upper-case and lower-case letters are treated
404 as identical; also, the interpreter normally looks only at the first nine
405 characters of each word that the player types.
406
407 When the player types a command, the interpreter divides what was typed
408 into individual words, which it then looks up in the dictionary.  If it
409 finds all the words, and they seem to represent a sensible course of
410 action, that's what happens next.
411
412 So far, we've seen dictionary words used as the values of an object
413 ``name`` property::
414
415      name 'bird^s' 'nest' 'twigs' 'moss',
416
417 and indeed that's just about the only place where they commonly occur.
418 You'll save yourself a lot of confusion by remembering the distinction:
419 Double quotes for Output, Single quotes for Input (DOSI).
420
421 Routines and statements
422 =======================
423
424 A routine is a collection of statements, which are performed (or we often
425 say "are executed") at run-time by the interpreter.  There are two types of
426 routine, and about two dozen types of statement (there's a complete list in
427 "Statements" on page 174; see also "Inform language" on page 257).
428
429 .. rubric:: Statements
430
431 A **statement** is an instruction telling the interpreter to perform a
432 particular task -- to "do something" -- while the game is being played.  A
433 real game usually has lots and lots of statements, but so far we've
434 encountered only a few.  We saw::
435
436      location = before_cottage;
437
438 which is an example of an **assignment** statement, so-called because the
439 equals sign ``=`` assigns a new value (the internal ID of our
440 ``before_cottage`` room) to a variable (the global variable ``location``
441 which is part of the library).  Later we saw::
442
443      if (nest in branch) deadflag = 2;
444
445 which is actually *two* statements: an assignment, preceded by an ``if``
446 statement::
447
448      if (nest in branch) ...
449
450 The ``if`` statement tests a particular condition; if the condition is
451 true, the interpreter executes whatever statement comes next; if it isn't
452 true, the interpreter ignores the next statement.  In this example, the
453 interpreter is testing whether the ``nest`` object is "in" or "on" (which
454 we now know means "is a child of") the ``branch`` object.  For most of the
455 game, that condition is not true, and so the interpreter ignores the
456 following statement.  Eventually, when the condition becomes true, the
457 interpreter executes that statement: it performs an assignment::
458
459     deadflag = 2;
460
461 which changes the value of the library variable ``deadflag`` from its
462 current value to 2.  Incidentally, if statements are often written on two
463 lines, with the "controlled" statement indented.  This makes it easier to
464 read, but doesn't change the way that it works::
465
466     if (nest in branch)
467         deadflag = 2;
468
469 The thing that's being controlled by the ``if`` statement doesn't have to
470 be an assignment; it can be any kind of statement.  In fact, you can have
471 lots of statements, not just one, controlled by an ``if`` statement.  We'll
472 talk about these other possibilities later.  For now, just remember that
473 the only place where you'll find statements are within standalone routines
474 and embedded routines.
475
476 .. rubric:: Standalone routines
477
478 A **standalone routine** is a series of statements, collected together and
479 given a name.  When the routine is "called" -- by its given name -- those
480 statements are executed.  Here's the one that we've defined::
481
482     [ Initialise; location = before_cottage; ];
483
484 Because it's such a tiny routine, we placed it all on a single line.  Let's
485 rewrite it to use several lines (as with the ``if`` statement, this improves
486 the readability, but doesn't affect how it works)::
487
488     [ Initialise;
489         location = before_cottage;
490     ];
491
492 The ``[ Initialise;`` is the start of the routine, and defines the name by
493 which it can be "called".  The ``];`` is the end of the routine.  In
494 between are the statements -- sometimes known as the body of the routine --
495 which are executed when the routine is called.  And how is that done?  By a
496 statement like this::
497
498     Initialise();
499
500 That single statement, the routine's name followed by opening and closing
501 parentheses, is all that it takes to call a routine.  When it comes across
502 a line like this, the interpreter executes the statements -- in this
503 example there's only one, but there may be ten, twenty, even a hundred of
504 them -- in the body of the routine.  Having done that, the interpreter
505 resumes what it was doing, on the line following the ``Initialise();``
506 call.
507
508 .. note::
509
510    You may have noticed that, although we've defined a routine named
511    ``Initialise``, we've never actually called it.  Don't worry -- the
512    routine is called, by the Inform library, right at the start of a game.
513
514 .. rubric:: Embedded routines
515
516 An **embedded routine** is much like a standalone routine, though it
517 doesn't have a name and doesn't end in a semicolon.  This is the one that
518 we defined::
519
520      [; if (nest in branch) deadflag = 2; ]
521
522 except that we didn't write it in isolation like that: instead, we defined
523 it to be the value of an object property::
524
525      each_turn [; if (nest in branch) deadflag = 2; ],
526
527 which would have worked just the same if we'd written it like this::
528
529      each_turn [;
530          if (nest in branch)
531              deadflag = 2;
532      ],
533
534 All embedded routines are defined in this manner: as the value of an object
535 property.  That's where they're embedded -- inside an object.  The
536 introductory characters ``[;`` maybe look a little odd, but it's really
537 only the same syntax as for a standalone routine, only without a name
538 between the ``[`` and ``;``.
539
540 For calling an embedded routine, thus causing the statements it contains to
541 be executed, the method that we described for a standalone routine won't
542 work.  An embedded routine has no name, and needs none; it's
543 *automatically* called by the library at appropriate moments, which are
544 determined by the role of the property for which it is the value.  In our
545 example, that's at the end of every turn in which the player character is
546 in the same room as the branch.  Later, we'll see other examples of
547 embedded routines, each designed to perform a task which is appropriate for
548 the property whose value it is; we'll also see that it is possible to call
549 an embedded routine yourself, using an ``obj_id.property()`` syntax -- in
550 this example, we could call the routine by writing ``branch.each_turn()``.
551 There's more about these topics in "Routines and arguments" on page 67, "A
552 diversion: working with routines" on page 104 and in "Routines" on
553 page 179.
554
555 That ends our review of the ground covered in our first game.  We'll have
556 more to say about most of this later, but we're trying not to overload you
557 with facts at this early stage.  What we'd like you to do is to look back
558 at the source of the game, and ensure that you can recognise all the
559 elements which this chapter has described.  Then, we'll move on to fix a
560 few of the game's more important defects.