Turbo Pascal Text Adventure Primer cover: BEACON, the adventure the primer builds in Turbo Pascal 2.0, mid-game on the Tatung Einstein
← Back to Modules
module
11

Text In The Program

Introduction

Part one was about words coming in. Part two is about words going out, and an adventure has far more going out: BEACON's twelve rooms alone are 1,517 characters of description, and the whole game has about 2,600. Where do they live? The obvious place is the program itself.

Two Ways In The Program

As string constants. Each message is a Writeln with its text in quotes, and a case picks one:

case N of
  1: Writeln('You are on the beach.');
  2: Writeln('A small cove.');
  ...

As a typed constant array. The messages are values in a table, and the program prints Msg[N]:

const
  Msg: array[1..3] of Str25 =
    ('You are on the beach.',
     'A small cove.',
     'A wooden jetty.');

What They Cost

When R compiles, Turbo reports Code:, the bytes the compiled program takes. That is the measuring stick.

A string constant costs one byte of code for each character. Make one message fourteen characters longer, and Code: rises by fourteen. BEACON's twelve room descriptions written as Writelns take 2,015 bytes of code; the same program with every message empty takes 498. The difference, 1,517, is the descriptions to the byte.

A typed constant array costs its declared length for every entry, however short the message in it. Three messages in a string[25] array take 177 bytes of code. Declare them string[255] to be safe, and the same three messages take 865. So the array's length has to be chosen to fit the longest message, and every shorter one wastes the difference.

And the array has a hard limit. Turbo reads a line of a program only up to its 126th character, and a string constant cannot carry on to the next line, so a constant can hold little more than a hundred characters. A typed constant cannot be joined from pieces with + either. BEACON's first room description is 209 characters, and cannot go in a typed constant array at all. As Writelns it can, in pieces:

Write('You are on a strip of shingle ...');
Writeln('... and a store shed stands to the east.');

Paid For Twice

There is a cost that does not show in Code:. Compiling in memory, as R does, Turbo holds your program's text in memory as well as the compiled code. Every message is in memory twice: once in quotes in the source, once in the code. A game whose prose was all in the program would run out of room long before it ran out of story.

The Code

program InCode;
var
  N: Integer;

procedure Say(N: Integer);
begin
  case N of
    1: Writeln('You are on the beach.');
    2: Writeln('A small cove.');
    3: Writeln('A wooden jetty.');
  end;
end;

begin
  for N := 1 to 3 do
    Say(N);
end.

Starting from

A new work file INCODE.

What you should see

You are on the beach.
A small cove.
A wooden jetty.

and, while it compiled, Code: 203 bytes. Type the typed constant version above as INARRAY (with type Str25 = string[25]; and a for loop printing Msg[N]) and it shows Code: 177 bytes.

Change One Thing

  • Make message 2 'A small cove, full of weed.'. By how much does Code: change?
  • Change the loop to for N := 1 to 4 do. What does Say(4) print, and why is there no error?
  • In INARRAY, change Str25 = string[25] to string[15]. What happens to the first message, and to Code:?

Exercises

11.1 Add a fourth message, 'A cramped shed.', to INARRAY.

11.2 Change INARRAY's strings to string[255]. What is Code: now? Work out where the extra bytes went.

11.3 Give Say an else, so that a message number it does not know prints No message and the number, and try it with numbers up to 5.

Worked solutions are in Appendix II.

When It Goes Wrong

SymptomCause
Error 55: String constant exceeds lineA message too long for one line of the program. Split it into several Writes.
Error 3: ',' expected in a typed constantYou tried to join two strings with + there. It is not allowed.
A message is cut shortThe array's strings are shorter than the message.
Long messages break in the middle of wordsThe screen is 40 characters wide and Writeln does not care about words. S12 fixes it.

Summary

Text in the program costs a byte of code a character, and as much again in memory for the program's text while you work on it. A typed constant array costs its declared length for every message, and cannot hold a message much over a hundred characters. Both are fine for a few short messages. For a whole game's prose, the text has to move out of the program.

Next

S12, A File Of Messages: the text moves to the disc.

Get the Newsletter

New guides, disk images and community finds, roughly once a quarter. No spam, we promise, this isn't Tatung's marketing department.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Newsletter

Subscribe to our newsletter and stay updated.

This form is protected by reCAPTCHA, and the Google Privacy Policy and Terms of Service apply.