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