Skip to main content

How a script runs

Scripts · Events that start scripts · Control flow

In brief​

A run starts at the block's script with a clean state and executes its commands one after another until it stops; conditions test flags, blocks, the party, characters and items. A command that sends an event can start another block's script inside the running one. A script keeps nothing of its own: everything it changes is ordinary game state, kept with the level or saved with the game.

A run started inside another. Script A runs on its block. When it moves the party, the move sends the leave event to the old block, whose script B runs, then the enter event to the new block, whose script C runs; both run in the middle of the moving instruction. Script A then continues: the party-move command saved and restores its state. Other commands that send an event start nested runs without that protection.
Moving the party runs two other scripts before the moving script goes on.

A run​

A new run starts at the block's script offset with a clean state: the run variable is zero, no character is selected, and the block and the event are recorded. Commands then execute one after another until the script ends; jumps, calls and returns are described under control flow.1

A command that sends an event can start another block's script inside the running one. Only the party-move command protects the running script's state from that nested run; see scripts started by scripts. The return stack is not part of the clean state, and a nested run can return into the script that started it; see the return stack across runs.

Conditions​

SCRIPT_JUMP_UNLESS is the conditional branch of the script language. It evaluates an expression built from ScriptExpressionToken tokens — flags, block contents, party and character properties, the current item, dice rolls and constants, combined with comparisons and logical operators. If the result is nonzero the script continues; otherwise it jumps elsewhere. Many scripts of the original levels start with such a test, and several are often chained.1

Two tokens read the state of the run itself, which is how a script knows why it was started and what the player answered:

  • SCRIPT_EXPR_EVENT_MASK gives the event that started the run.
  • SCRIPT_EXPR_RUN_VARIABLE gives the run variable, set by dialogue choices, some special events and the flag commands.

Some tokens that search for a character also select it, so a condition can choose which character a later command affects or names in a message.

What the conditions test​

The counts come from the 1958 conditions reachable in the 16 original levels.

  • Walls. The most frequent test reads a block's wall references: SCRIPT_EXPR_BLOCK_BYTE (522 times) with offset 0, 1, 2 or 3, the face turned north, east, south or west, and SCRIPT_EXPR_BLOCK_FIRST_BYTE (74 times) for the north face. They tell which wall a face shows now, and so whether a lever, a button or a door has been changed by the wall commands. No other offset is used.
  • Characters. The race search (19 times) looks for an elf 7 times and a dwarf 9 times, and once each for a human, a half-elf and a halfling; it is usually combined with a level flag, so that a character of that race remarks on something once. The class search (97 times) looks for a thief 67 times, a mage 14, a fighter 8, a cleric 7 and a paladin once. Both consider only conscious characters and select the one found. The portrait search and SCRIPT_EXPR_NO_ELIGIBLE_CHARACTER, true when every character in the party is invisible, are not used.
  • Items carried. The item-holder form of SCRIPT_EXPR_PARTY_BLOCK_OR_ITEM_HOLDERS counts the characters in the party, dead or not, who carry an item of a given type and value anywhere in their inventory. It is used 7 times: level 11 sends the party to one of two places depending on whether nobody carries an ITEM_TYPE_SPELLBOOK or an ITEM_TYPE_HOLY_SYMBOL; level 13 chooses its dialogue by whether as many characters carry an ITEM_TYPE_MEDUSA_SHIELD as there are active characters, which a dead character's shield can satisfy in place of an active one's; levels 2 and 15 check that exactly one character carries a given ITEM_TYPE_QUEST_ITEM.
  • Items on the floor. SCRIPT_EXPR_BLOCK_ITEM_TYPE_COUNT (124 times) mostly asks whether any item lies in a block (101 times, flag 0, so an item flying through does not count). The 23 tests with flag 1 also count flying items, for example three ITEM_TYPE_GEM in a block, or an ITEM_TYPE_STICKY_PAPER that stops the items a level 15 script moves along a row. SCRIPT_EXPR_ITEM_IN_BLOCK is not used.2

Scripts and the game's state​

The table summarizes the commands of the walls and doors, movement, monsters and items and characters, flags and messages pages, and what persists.

A script keeps nothing of its own beyond the run: everything it changes is ordinary game state, kept or rebuilt like the rest. The table gives, for each kind of command, what it changes and what becomes of the change.3

CommandsState they changeWhen the party leaves the levelIn a saved game
Walls, doorsWall references of the current level's blocksKept in the level's recordSaved
DoorsA door in motionFinished at onceFinished before saving
Moving and turning the party, SCRIPT_SET_DIRECTIONThe party's block and facingSet by the level changeSaved
Changing levelThe current level and sublevel—Saved
Placing, moving or provoking monstersThe current level's monster slotsKept in the level's recordSaved
Item commands, moving items, removing quest itemsThe shared item table, which holds the items of every levelKeptSaved
Launching and turning flying objectsObjects in flightEnded: thrown items fall, the rest vanishEnded before saving
Character commands, special events and sequencesThe characters: hit points, experience, levels, life, party membershipKeptSaved
Flags and restingLevel flags, global flags, the rest refusalKeptSaved
Conditions, dialogue, flag commandsThe run variable and the selected characterReset at the start of every runNot saved
Messages, dialogue, sound, waits and redrawsThe screen and the sound only——

Some state is out of a script's reach:

  • Bindings. Which blocks have a script, the script each one starts and the events it accepts are written only by the level loader, from the INF cell bindings, and read again at every load. No command changes them. The same holds for the level's timed block scripts and step-triggered script.
  • Other levels. Only the current level is in play: the monsters, timers and blocks of other levels wait in their records and receive no event. A script reaches another level only by moving items there, which appear when that level is next loaded, and by changing level itself.

When scripts run. A script runs inside the step of the game loop that sent its event, and nothing else of the loop happens until it ends; see what the order means. During a wait the shared counter keeps running unless time is paused, so timed updates that come due run once, at the next timed-update step after the script. Wall changes in view mark the view, which the game redraws once in the redraw step of the same pass, unless the script redraws it first. A level change stops the run, and a command that sends an event can start another script inside the running one.

Sources​

Locations refer to the game executable reference; comparative sources are listed in the technical references. Statements about the original levels come from decoding the scripts of the 16 reference INF files listed in the file inventory.

Footnotes​

  1. Block event dispatcher and the expression evaluator. Comparative source: oeob_eval_v2 (REF-SCUMM-003). ↩ ↩2

  2. The condition evaluator, count eligible characters, search the inventory and search or count a block's items. ↩

  3. The block event dispatcher, which resets the run variable, the selected character, the event block and the event mask, none of them in the saved party variables; the cell-binding loader, the only writer of a block's script reference found; the item lists rebuilt at each load; the block-list check and the game loop. ↩