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