BBC BASIC 2.31 for the Tatung Einstein, showing DRIFT, the text adventure the course builds, in the middle of a game
← Back to Courses
module
22

Looking

Introduction

CRYO BAY and a list of exits is a map, not a place. An adventure lives in its descriptions: a line or two for each room saying what is there. They are longer than one line of the screen - and left to itself, the Einstein breaks a long line wherever it reaches column 40, in the middle of a word. This section gives the rooms their words, and prints them properly.

Descriptions

One more DATA line per room, from line 3100, and one more array, D$(), read in PROCSETUP. The descriptions are in quotes because they have commas in them. A program line can be much longer than one row of the screen; it just wraps as you type it.

Breaking Between Words

PROCWRAP(T$) prints T$ a screen line at a time, breaking at spaces:

  • If what is left fits - fewer than 40 characters - print it and stop. Fewer than 40, not 40, because a line of exactly 40 leaves an empty row after it (S19).
  • Otherwise, find the last space in the first 40 characters, print everything before it, and carry on with everything after it.

Finding that space is a small loop of its own, and it is in a function, FNBREAK, for a reason that is worth knowing: BBC BASIC cannot leave a procedure or function from inside a FOR or REPEAT loop. An ENDPROC or = inside the loop gives No PROC or No FN. So FNBREAK lets its loop finish, and only then hands back the answer. S%=1 means no space was found at all, and the line is simply cut at 39.

The Code

LOAD "ADV2". Add:

1030 DIM D$(4):RESTORE 3100:FOR R%=1 TO 4:READ D$(R%):NEXT
1120 PROCWRAP(D$(R%))
2000 DEF PROCWRAP(T$)
2010 LOCAL S%
2020 REPEAT
2030 S%=FNBREAK(T$)
2040 PRINT LEFT$(T$,S%-1)
2050 T$=MID$(T$,S%+1)
2060 UNTIL T$=""
2070 ENDPROC
2080 DEF FNBREAK(T$)
2082 LOCAL S%
2084 IF LEN(T$)<40 THEN =LEN(T$)+1
2086 S%=41
2088 REPEAT S%=S%-1:UNTIL MID$(T$,S%,1)=" " OR S%=1
2090 IF S%=1 THEN =40
2092 =S%
3100 DATA "YOU WAKE IN THE CRYO BAY. YOUR SLEEP POD STANDS OPEN. THE OTHER PODS ARE EMPTY AND DARK."
3110 DATA "A CORRIDOR RUNS THROUGH THE SHIP. TO THE NORTH IS THE BRIDGE DOOR, AND BESIDE IT A WALL LOCKER."
3120 DATA "THE ENGINE ROOM. THE REACTOR IS COLD, AND THE SOCKET FOR ITS POWER CELL IS EMPTY."
3130 DATA "THE BRIDGE. STARS WHEEL SLOWLY PAST THE WINDOW. THE NAVIGATION CONSOLE IS LIT, WAITING FOR A CODE."

Starting from

LOAD "ADV2" from S21. When it works, SAVE "ADV3".

What you should see

CRYO BAY
YOU WAKE IN THE CRYO BAY. YOUR SLEEP
POD STANDS OPEN. THE OTHER PODS ARE
EMPTY AND DARK.
EXITS: EAST
WHAT NOW? E
CORRIDOR
A CORRIDOR RUNS THROUGH THE SHIP. TO
THE NORTH IS THE BRIDGE DOOR, AND
BESIDE IT A WALL LOCKER.
EXITS: NORTH EAST WEST

Every line breaks between words.

Descriptions wrapped between words

Change One Thing

  • Change lines 2084 and 2086 to use 20 and 21 instead of 40 and 41. What does the cryo bay look like now?
  • Change line 3100 to DATA "YOU WAKE UP. IT IS VERY COLD." and run it.
  • After LOAD "ADV3", type at the >: PROCWRAP("THIS IS A VERY LONG MESSAGE THAT WILL NOT FIT ON ONE LINE OF THE SCREEN, SO PROCWRAP BREAKS IT BETWEEN WORDS.") A procedure can be used straight from the >.

Exercises

22.1 Give the bridge a new description of your own, at least three screen lines long, and check it breaks cleanly.

22.2 Give PROCWRAP a narrower column - 30 characters - and try it on the cryo bay. Which two lines must change, and to what?

Worked solutions are in Appendix II.

When It Goes Wrong

SymptomCause
No PROC at line 2030 (or No FN)An ENDPROC or = inside a loop. Let the loop finish first.
Words break in the middleThe text is being printed with PRINT, not PROCWRAP.
A description is cut short at a commaIts DATA item is not in quotes.
Out of DATA at line 1030A description line is missing.

Summary

Each room's description is a quoted DATA item, read into D$(). PROCWRAP prints text a screen line at a time, breaking at the last space that fits. A loop cannot be left with ENDPROC or =, so FNBREAK answers after its loop. Save it as ADV3.

Next

S23, Things - objects, and where each one is.

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.