Defining Our First Room
So far, the only really significant thing we have done to the source file is to indicate that the room in which the game will start will be called outsideCottage. Our next job is to define this room. As a first attempt, add the following to the end of your source file:
outsideCottage: OutdoorRoom
roomName = 'In front of a cottage'
desc = "You stand just outside a cottage; the forest stretches east. "
;
Be careful to copy this code exactly, including the punctuation, not least the semicolon at the end (by itself on the last line). Also, be careful to note that 'In front of cottage' is enclosed in single quotation marks, and "You stand outside a cottage; the forest stretches east. " in double quotation marks. This distinction is important and must be followed (the significance of the distinction will be explained in more detail on p. 50 below).
If you compile and run the game it should now run, although the game is pretty basic. Since there's only one room in the game you can't actually move anywhere, and there are no objects to manipulate or even examine. About the most interesting thing you can do with the game right now is to quit straight away!
Before making things more interesting, let's take a look at the definition of the one room we have defined so far. The first line outsideCottage: OutdoorRoom consists of the object name followed (after the colon) to the superclass to which it belongs. The object name is simply the name by which this object will be referred in our code; we could have called it room101 or auntieMyrtle, but it is obviously better to choose something that makes reasonable sense. Note that we have followed the TADS 3 convention of starting an object name with a lowercase letter, while using a capital letter at the start of any subsequent words in the name.
OutdoorRoom is the name of the class to which we want this game object to belong. By default an OutdoorRoom has ground and sky, but no walls, which is what we want here. Try running the game again and type examine ground, x sky and x wall. Now change OutdoorRoom to Room, and compile and run the game again and type the same commands. Finally change Room back to OutdoorRoom. Again note the naming convention, since OutdoorRoom names not an object but a class it starts with a capital letter.
The next two lines define the properties of our OutdoorRoom object:
|
roomName = 'In front of a cottage'
|
desc = "You stand just outside a cottage; the forest stretches east. "
|
|
Finally, on the last line, is a semicolon by itself; this simply ends the definition of this object. An alternative is to enclose the property list in curly braces thus:
|
{
roomName = 'In front of a cottage'
desc = "You stand just outside a cottage; the forest stretches east. "
}
Either form is possible and which you use is largely up to you. There are situations (as we shall see later) in which you have to use braces; in other situations (as we shall again see) the use of the semicolon can make for more compact code.
Note that the semicolon is also used to terminate TADS 3 program statements. This can be a source of confusion because the property definitions look rather like assignment statements, so it can be very easy to slip into writing:
|
roomName = 'In front of a cottage';
desc = "You stand just outside a cottage; the forest stretches east. ";
;
If you try to compile this, you'll get an error, because the compiler will now think the definition of outsideCottage ends with the name property and won't know what to do with the desc property. Since this is such a common source of potential confusion it's worth remembering the following golden rule straight away:
A property definition is not a programming statement. Do not end it with a semicolon.
We have laboured the definition of outsideCottage at some length, since the principles involved are common to a great deal of TADS 3 programming, most of which consists in defining objects.
If you read the previous chapter, then all this should be reasonably familiar from the "goldskull" example. Now we come to our first major innovation: although the definition of outsideCottage seems simple enough, it can in fact be made a good deal simpler through a feature of the language called 'templates'. A template simply defines a shorthand way of defining the most common properties an object is likely to have. Since every room will have a name and a description the TADS 3 library defines a template that looks like this:
|
|
|
"You stand just outside a cottage; the forest stretches east. "
;
|
|
"You stand just outside a cottage; the forest stretches east. "
'In front of a cottage'
;
|
|
|
Getting Started in TADS 3
[Main]
[Previous] [Next]