35c8f06d65d22f293533e2522ee00cbf2261b7cd
[ibg.git] / chapters / 16.rst
1 ===================
2 Debugging your game
3 ===================
4
5 .. only:: html
6
7   .. image:: /images/picN.png
8      :align: left
9
10 .. raw:: latex
11
12    \dropcap{n}
13
14 obody understands the phrase *errare humanum est* quite in the same way 
15 as a programmer does. Computers are highly efficient machines capable of 
16 wondrous calculations, but they lack imagination and insist that every 
17 single item thrown at them must be presented according to certain rules 
18 previously defined. You can't negotiate with a computer; you either bow 
19 in submission or bite the dust.
20
21 Inform behaves no differently. If you make a typing or syntax mistake, 
22 the compiler will send you back to revise your work. "It was just a 
23 lousy comma!" you cry in disgust. The compiler remains silent. It has 
24 nothing to gain by argument, because it’s always right. So you go and 
25 change the lousy comma. No harm done except perhaps to your pride.
26
27 Errors that are found during compilation may be tedious to correct, but 
28 are usually easy to find; after all, the compiler tries politely to 
29 point out what and where the mistake was. Trouble begins after you've 
30 managed to satisfy all of the compiler's complaints. You are rewarded by 
31 a clean screen, devoid of a list of errors, and you are offered -- a 
32 gift!
33
34 A new file has appeared in your folder. A story file. Yes, *the* game. 
35 You quickly open your favourite interpreter and begin to play -- only to 
36 discover the dark side of errors, the bugs. Bugs come in all shapes, 
37 colours and sizes: big, small, stupid, absurd, minor, disturbing, 
38 nerve-wracking and catastrophic. They are often unpredictable: they 
39 regale our eyes with surprising, unexpected behaviour. They defy logic: 
40 I can TAKE the key, and the game even says "Taken", but the key remains 
41 in the same place and won't appear in my inventory. Or: opening the door 
42 while wearing the fur coat causes a programming error and a cryptic 
43 message "tried to find the attribute of nothing". And many, many others.
44
45 When designing a game you try to take into consideration the states that 
46 your objects will find themselves in, but any medium-sized game has such 
47 a number of objects and actions that it's almost impossible to think of 
48 all the possible variations, permutations and possibilities.
49
50 Debugging consists in finding run-time errors, and then correcting them. 
51 Pretty easy, you might think, but no. Detection of such errors is not 
52 straightforward, since they tend to manifest themselves only under 
53 precise circumstances. Then you have to investigate your code to find 
54 out what is causing them. And then, if you discover the offending lines, 
55 you must make the appropriate changes. (There is also the case when you 
56 can't find the mistake. Don't worry, it's there somewhere. Persistence 
57 always pays off in the end.)
58
59 To help you out in this daunting task, Inform has a stock of special 
60 actions: the debugging verbs. They become available at run-time when the 
61 source file is compiled in **Debug mode** (``-D switch``). When you are 
62 ready to release your game, you’ll have to recompile, switching off 
63 Debug to avoid allowing the players to benefit from the debugging verbs. 
64 We'll cover briefly a few of these actions, and tell you what they do.
65
66
67 Command lists
68 =============
69
70 The only way to test a game is to play it. As you make progress writing 
71 code, the game grows complicated, and it becomes really tiresome to 
72 repeat all the commands every time you play. Not unusually, when you fix 
73 the behaviour of some object, you are also affecting the behaviour of 
74 other objects or actions, so it's a good idea to test everything now and 
75 then; you have to make sure that your recent changes and fixes didn't 
76 spoil something that previously worked fine.
77
78 The RECORDING command (RECORDING ON and RECORDING OFF) saves the 
79 commands that you type as you play into a text file (you'll probably be 
80 prompted for a file name). When you add a new section to the game, you 
81 can play to that point, type RECORDING ON to capture (in another file) 
82 the commands which exercise that section, and then later use your editor 
83 to append those new commands to the existing list.
84
85 The REPLAY command runs the text file created by RECORDING, playing all 
86 the stored commands in one go. This way you can very quickly check 
87 whether everything is working as it should.
88
89 You can open the file of commands with any text editor program and 
90 modify the contents as need arises: for instance, if you want to delete 
91 some commands no longer necessary because of a change to the game, or if 
92 you forgot to test some particular object and you need to add new 
93 commands.
94
95 This technique (the use of recorded lists of commands) is, and we can't 
96 emphasise it too strongly, one of the most useful testing features for a 
97 game designer.
98
99
100 Spill them guts
101 ===============
102
103 Some debugging verbs offer information about the current state of things.
104
105 TREE
106
107   This action lists all the objects in the game and how they contain 
108   each other. You can discover the possessions of just one object by 
109   typing TREE *object*. All the objects that you have defined in the 
110   source file are turned into numbers by Inform when it compiles the 
111   story file; this command also lists those internal
112   :samp:`{obj_id}` numbers.
113
114 SHOWOBJ *object*
115
116   Displays information about the *object*, the attributes it currently 
117   has and the value of its properties. The *object* can be anywhere, 
118   not necessarily in scope. For instance, in "Heidi":
119
120   .. code-block:: transcript
121
122     >SHOWOBJ NEST
123     Object "bird's nest" (29) in "yourself"
124       has container moved open workflag
125       with name 'bird's' 'nest' 'twigs' 'moss',
126            description "The nest is carefully woven of twigs and moss." (19230),
127
128 SHOWVERB *verb*
129
130   Displays the grammar of the *verb*, just like a standard ``Verb``
131   definition. This comes in handy when you have tampered with ``Extend`` 
132   and are not sure about the final results of your machinations. An 
133   example from "William Tell":
134
135   .. code-block:: transcript
136
137     >SHOWVERB GIVE
138     Verb 'feed' 'give' 'offer' 'pay'
139         * held 'to' creature -> Give
140         * creature held -> Give reverse
141         * 'over' held 'to' creature -> Give
142         * 'homage' 'to' noun -> Salute
143
144   The first lines reproduce the verb definition as it's written in the 
145   library. The last line, however, is the direct consequence of our 
146   tailored ``Extend``:
147
148   .. code-block:: inform6
149
150     Extend 'give'
151         * 'homage' 'to' noun        -> Salute;
152
153 SCOPE
154
155   Lists all of the objects currently in scope (in general terms, visible 
156   to the player character). More powerfully, you can type SCOPE *object* 
157   to discover which objects are in scope for the named *object*. This 
158   feature becomes useful when you have NPCs capable of tampering with 
159   their surroundings.
160
161
162 What on earth is going on?
163 ==========================
164
165 There comes the time when some actions don't produce the desired effects 
166 and you don't know why. The following debugging verbs offer information 
167 about what the interpreter is up to, which might enable you to identify 
168 the moment when things started to go awry.
169
170 ACTIONS (or ACTIONS ON ) and ACTIONS OFF
171
172   Gives information about all the actions going on. Some actions get 
173   redirected to others, and this becomes at times a source of mischief 
174   and mystery; here you get a clue what's happening. For example, take 
175   this transcript from "William Tell":
176
177   .. code-block:: transcript
178
179     Further along the street
180     People are still pushing and shoving their way from the southern gate towards
181     the town square, just a little further north. You recognise the owner of a fruit
182     and vegetable stall.
183
184     Helga pauses from sorting potatoes to give you a cheery wave.
185
186     >SEARCH STALL
187     [ Action Search with noun 35 (fruit and vegetable stall) ]
188     [ Action Examine with noun 35 (fruit and vegetable stall) (from < > statement) ]
189     It's really only a small table, with a big heap of potatoes, some carrots and
190     turnips, and a few apples.
191     ...
192
193 CHANGES (or CHANGES ON ) and CHANGES OFF
194
195   Tracks object movements, and changes to properties and attributes:
196
197   .. code-block:: transcript
198
199     Middle of the square
200     There is less of a crush in the middle of the square; most people prefer to
201     keep as far away as possible from the pole which towers here, topped with that
202     absurd ceremonial hat. A group of soldiers stands nearby, watching everyone who
203     passes.
204
205     >GO NORTH
206     [Setting Middle of the square.warnings_count to 1]
207     A soldier bars your way.
208
209     "Oi, you, lofty; forgot yer manners, didn't you? How's about a nice salute for
210     the vogt's hat?"
211
212     >AGAIN
213     [Setting Middle of the square.warnings_count to 2]
214
215     "I know you, Tell, yer a troublemaker, ain't you? Well, we don't want no bovver
216     here, so just be a good boy and salute the friggin' hat. Do it now: I ain't
217     gonna ask you again..."
218
219     >SALUTE HAT
220     [Setting hat on a pole.has_been_saluted to 1]
221     You salute the hat on the pole.
222
223     "Why, thank you, sir," sneers the soldier.
224
225     >GO SOUTH
226     [Setting Middle of the square.warnings_count to 0]
227     [Setting hat on a pole.has_been_saluted to 0]
228     [Moving yourself to South side of the square]
229     ...
230
231 TIMERS (or TIMERS ON ) and TIMERS OFF
232
233   This verb shows you the state of all active timers and daemons at the 
234   end of each turn. We haven't mentioned timers -- similar to daemons -- 
235   in this guide; you might perhaps use one to explode a bomb ten turns 
236   after lighting its fuse.
237
238 TRACE (or TRACE ON ), TRACE *number* and TRACE OFF
239
240   If you turn on this powerful verb, you'll be able to follow the 
241   activity of the **parser** -- that part of the library which tries to 
242   make sense of what the player types -- and this will indeed be a 
243   wonderful moment of gratitude that someone else took the trouble of 
244   writing it. Since the parser does so many things, you can decide the 
245   level of detail about the displayed information with the *number* 
246   parameter, which can go from 1 (minimum info) to 5 (maximum info). By 
247   default, TRACE ON and TRACE with no number sets level 1. Trace level 
248   1 shows the grammar line that the parser is thinking about, while 
249   level 2 shows each individual token on each grammar line that it 
250   tries. The information displayed with higher levels may become quite 
251   hacky, and you are advised to use this feature only if nothing else 
252   helps.
253
254
255 Super-powers
256 ============
257
258 GONEAR *object*
259
260   This action lets you teleport to the room where the *object* is. This 
261   is useful when, for example, certain parts of the map are closed 
262   until the player character solves some puzzle, or if the game map is 
263   divided in different areas. If the room you want to visit has no 
264   objects, you can use...
265
266 GOTO *number*
267
268   Teleports you to the room with that internal *number*. Since rooms 
269   usually have no name, you'll have to discover the internal number of 
270   the room object (with the command TREE, for instance).
271
272 PURLOIN *object*
273
274   PURLOIN works exactly as TAKE , with the nice addition that it doesn't 
275   matter where the object is: in another room, inside a locked 
276   container, in the claws of the bloodthirsty dragon. More dangerously, 
277   it doesn't matter if the object is takeable, so you may purloin 
278   ``static`` or ``scenery`` objects. PURLOIN is useful in a variety of 
279   situations, basically when you want to test a particular feature of 
280   the game that requires the player character to have some objects 
281   handy. Instead of tediously collecting them, you may simply PURLOIN 
282   them. Be careful: it's unwise to PURLOIN objects not meant to be 
283   taken, as the game's behaviour may become unpredictable.
284
285 ABSTRACT *object* TO *object*
286
287   This verb enables you to move the first *object* to the second 
288   *object*. As with PURLOIN , both objects can be anywhere in the game. 
289   Bear in mind that the second object should logically be a 
290   ``container``, a ``supporter`` , or something ``animate``.
291
292
293 Infix: the harlot's perogative
294 ==============================
295
296 The basic debugging verbs are fairly versatile, easy to use, and don't 
297 consume a lot of memory. Occasionally though, you'll meet a bug which 
298 you simply can't catch using regular techniques, and that’s when you 
299 might want to investigate the Infix debugger. You'll need to compile 
300 using the ``-X`` switch, and you'll then be able to monitor and modify 
301 almost all of your game’s data and objects. For instance, you can use 
302 ";" to inspect -- and change -- a variable:
303
304 .. code-block:: transcript
305
306   Inside Benny's cafe
307   Benny's offers the FINEST selection of pastries and sandwiches. Customers clog
308   the counter, where Benny himself manages to serve, cook and charge without
309   missing a step. At the north side of the cafe you can see a red door connecting
310   with the toilet.
311
312   >; deadflag
313   ; == 0
314
315   >; deadflag = 4
316   ; == 4
317
318       *** You have been SHAMEFULLY defeated ***
319
320   In that game you scored 0 out of a possible 2, in 2 turns.
321
322 It's often quite maddening to realise that some variable is still 
323 ``false`` because the Chalk puzzle didn't work properly, and that you 
324 can't test the Cheese puzzle until the variable becomes ``true``. Rather 
325 than quit, fix the Chalk, recompile, play back to the current position 
326 and only *then* tackle the Cheese, how much easier to just change the 
327 variable in mid-stream, and carry right on.
328
329 You can use ``;WATCH`` to monitor an object; you'll see it receive 
330 messages and you'll be told when its property and attribute values 
331 change:
332
333 .. code-block:: transcript
334
335   >;WATCH MID_SQUARE
336   ; Watching object "Middle of the square" (43).
337
338   >NORTH
339   [Moving yourself to Middle of the square]
340   [Moving local people to Middle of the square]
341   [Moving Gessler's soldiers to Middle of the square]
342   [Moving your son to Middle of the square]
343
344   Middle of the square
345   There is less of a crush in the middle of the square; most people prefer to
346   keep as far away as possible from the pole which towers here, topped with that
347   absurd ceremonial hat. A group of soldiers stands nearby, watching everyone who
348   passes.
349   [Giving Middle of the square visited]
350
351   >NORTH
352   [ "Middle of the square".before() ]
353   [ mid_square.before() ]
354   [Setting Middle of the square.warnings_count to 1]
355   A soldier bars your way.
356
357   "Oi, you, lofty; forgot yer manners, didn't you? How's about a nice salute for
358   the vogt's hat?"
359
360   >NORTH
361   [ "Middle of the square".before() ]
362   [ mid_square.before() ]
363   [Setting Middle of the square.warnings_count to 2]
364
365   "I know you, Tell, yer a troublemaker, ain't you? Well, we don't want no bovver
366   here, so just be a good boy and salute the friggin' hat. Do it now: I ain't
367   gonna ask you again..."
368
369   >NORTH
370   [ "Middle of the square".before() ]
371   [ mid_square.before() ]
372   [Setting Middle of the square.warnings_count to 3]
373
374   "OK, Herr Tell, now you're in real trouble.
375   ...
376
377 .. todo:: "Herr" above is italicized.  Was that a mistake in the original text?
378
379 Infix is quite complex -- there are more commands than those we have 
380 shown you -- so while it's good to have available, it's not really a 
381 tool for novices. If you do use it, be careful: you get a lot of runtime 
382 power, and may easily screw up the state of the game. Remember, however, 
383 that the changes affect only the current story file while it’s running; 
384 to make permanent amendments, you still need to edit the source file.
385
386 You won't need it often, but Infix can sometimes provide quick answers 
387 to tricky problems.
388
389
390 No matter what
391 ==============
392
393 Your game will still have some undetected bugs despite all your efforts 
394 to clean it up. This is normal, even for experienced designers; don't 
395 feel discouraged or demoralised. You might find it reassuring to know 
396 that our own example games in this guide -- which certainly don't 
397 qualify as "complex programming" -- were far from perfect at the First 
398 Edition. We blush at the following report from an extremely diligent 
399 play-tester:
400
401   I found these things when playing “Captain Fate”:
402
403   * player is able to wear clothes over the costume,
404
405   * player can change into costume in the dark unlocked bathroom without
406     being interrupted,
407
408   * player can drop clothes in the dark unlocked bathroom. Try REMOVE
409     CLOTHES. X SELF. REMOVE COSTUME. INV -- X SELF says that you
410     are wearing the costume, but the inventory does not reflect this.
411
412 The Second Edition fixed those problems, and quite a few more besides. 
413 "That's it;" we thought, "after all this time, our example games are 
414 sure to be squeaky clean." In our dreams... Another diligent play-tester 
415 then wrote:
416
417   While reading I took notes of some mistakes and inconsistencies:
418
419   * BENNY, GIVE KEY TO CUSTOMERS and BENNY, GIVE KEY will
420     make Benny give the key to the player. The same goes for coffee.
421
422   * Benny will force the player back into the cafe even when the key is
423     dropped in the café, or put on the counter (in Benny's plain sight!).
424
425 Of course, the code we've offered you in this edition takes care of 
426 those embarrassing issues, but it might very well happen that a few more 
427 undetected absurdities pop up from now on.
428
429 The final stage of debugging must happen elsewhere, at the hands of some 
430 wilful, headstrong and determined beta-testers; these are the people 
431 who, if you’re lucky, will methodically tear your game to shreds and 
432 make extensive reports of things that don't work reliably, things that 
433 don't work as smoothly as they might, things that ought to work but 
434 don't, things that never even crossed your mind (like, uh, dropping the 
435 costume in the dark). Once you think your game is finished -- in that it 
436 does all that you think it should, and you've run out of ideas on how 
437 else to test it -- look for a few beta-testers; three or four is good. 
438 The IF community offers some beta-testing resources, or you can always 
439 ask in RAIF for kind souls willing to have a go at your game. Remember 
440 the golden rules:
441
442   * **Expect no mercy**. Although it hurts, a merciless approach is what 
443     you need at this time; much better to discover your errors and 
444     oversights now, before you release the game more widely. And don't 
445     forget to acknowledge your testers' assistance somewhere within the 
446     game.
447
448   * **Never say never**.  If your testers suggest that the game should 
449     respond better to an attempted action, don't automatically respond 
450     with "No one's going to try that!" They already have, and will again 
451     -- be grateful for your testers' devious minds and twisted psyches. 
452     Although a normal player won't try all of those oddball things, 
453     every player is bound to try at least one, and their enjoyment will 
454     be greater, the reality enhanced, if the game "understands".
455
456   * **Ask for more**. Don't treat your testers simply as validators of 
457     your programming skills, but rather as reviewers of your 
458     storytelling abilities. Encourage them to comment on how well the 
459     pieces fit together, and to make suggestions -- small or radical -- 
460     for improvement; don't necessarily reject good ideas just because 
461     implementing them "will take too long". For example: "the scene in 
462     the Tower of London doesn't somehow seem to belong in an Arabian 
463     Nights game", or "having to solve three puzzles in a row just to 
464     discover the plate of sheep's eyes is a little over the top", or 
465     "this five-room trek across the desert really is a bit dull; 
466     perhaps you could add a quicksand or something to liven it up?", or 
467     "the character of the eunuch in the harem seems to be lacking in 
468     something". That is, view the testers collectively not as simple 
469     spell-checkers, but rather as collaborative editors on your latest 
470     novel.