I7 Code Snippets

The following is a collection of useful snippets of Inform 7 code: none of them big enough to warrant extensions, but often handy all the same.

Basic Phrases

The following are often useful, and so simple they're worth dropping into any project:
  
 To decide if (n - a number) is between (a - a number) and (b - a number):
  if n is at least a and n is at most b, yes;
  no.

To print a list of alternatives, such as "the hat or the coat". (Emily Short's listing extension allows this and much more, but this is probably quicker to get working.)
  
To say the or-separated list of (the collection - a description):
	let N be the number of members of the collection;
	repeat with item running through the collection:
		say "[the item]";
		decrease N by 1;
		if N is 1, say " or ";
		if N > 1, say ", "; 

Scripted Sequences

A standard I6 trick was an every_turn routine with a counter that went up, well, every turn. This could step the player through a script. Implementing something similar using I7 scenes was impossible until scene-properties were introduced a few builds ago. Now you can do something neatly as follows.
  
A scene has a rulebook called the scene-script.
The null-rulebook is a rulebook.
The scene-script of a scene is usually the null-rulebook.

A scene has a number called the beat.
The playback beat is a number that varies.
A scene can be counting or uncounting. A scene is usually uncounting.

Every turn (this is the play scene scripts rule): 
	repeat with i running through happening scenes:
		if i is counting, increase the beat of i by one;
		now the playback beat is the beat of i;
		if the scene-script of i are not the null-rulebook:
			consider the scene-script of i.

This assigns a rulebook to every scene which is run at the every turn stage (and which can be stopped mid-flow to prevent extra rules, without disrupting the normal every turn flow). Additionally, scenes declaring as "counting" have a beat which will increase automatically once a turn. This can be examined / changed using the "beat of the scene". For convenience or, when playing a scene's rulebook we can examine the value of the "playback beat". Here's a quick example:
  
Meeting Jake is a counting scene. 
Jakes-Script is a rulebook.
The scene-script of Meeting Jake is the Jakes-Script rulebook.
Meeting Jake begins when the location is the Pool Hall.

Jakes-Script rule when Pool Game Question is not spoken and the playback beat is between 3 and 5:
	say "'So?' Jake demands. 'You want to play a game?'" instead.


Text Variations by Index

The following code implements a segmented say substitution that prints out one of a set of alternatives depending on the value of a number. The main use for this is in implementing "semi-sticky random": for example, you could write a one-room maze (like those in Hunter, In Darkness) by supplying a counter variable to the substitution and varying the counter as the player "moves" from room to room. This would ensure that each "room" has a consistent description, while still having them programmatically generated.
  

To say by (N - a number) -- beginning say_by_index:
(-
	switch(({N} % ( {-segment-count} ))) { 0:

-).

To say or  -- continuing say_by_index:
(-
	{-segment-count}:
-).

To say end -- ending say_by_index:
	(- {-close-brace} -).


    
The following short example demonstrates the substitution in use.

The Study is a room.

The study has a number called the index. 

Every turn:
	increase the index of the study by a random number between 1 and 3;
	say "[index of the study] - [by the index of the study]A[or]B[or]C[end].";



Home - Inform 7 website