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