Roger Firth's IF pages | ![]() |
InFancy -- using Inform objects | ![]() |
![]() |
Unlike the single-line header, the body of an object usually occupies at least three or four lines; thirty or forty lines is common, and three or four hundred isn't unheard-of. But that's advanced stuff: our aspirations at this stage are much more modest -- understand the permitted syntax.
Just as we divided the overall object definition into a header and a body, it's useful to subdivide the body into segments. There are four possible segments; they're all optional, and often only one or two are needed. The segments can occur in any order; each is introduced by a keyword:
Object header_data class ... ! inheritance classes with ... ! public properties private ... ! private properties has ... ! attributes flags ; |
The term header_data represents the name/relationship material that we covered on the previous page; hopefully you now know what that all means. Next, we'll summarise the use of the four segments types.
The class keyword introduces a space-separated list of class names from which this object inherits parts of its definition.
class class_iname class_iname ... class_iname |
This won't make much sense until we've talked about class definitions, so we'll leave the details of the class segment until later. To be going on with, here's an example:
Object kitchen_table "table"
class Furniture
... ;
|
The with keyword introduces a comma-separated list of the object's (public) property variables.
with property_def, property_def, ... property_def |
Here, each property_def represents a variable which is associated with this object, and therefore consists of a prop_name and (optionally) an initial value. In fact, you can have:
That is, for each property_def in the with syntax definition, you can substitute any of:
prop_name or prop_name value or prop_name value value ... value |
Finally, each value in this definition can be anything that can be evaluated at compile-time:
number or 'dictionary_word' or "string" or [; statement; statement; ... statement; ] |
The next page has more to say about the use of properties, but here's a short example:
Object kitchen_table "table"
with name 'battered' 'pine' 'table',
description "The table is scuffed and stained with indeterminate substances.",
... ;
|
The private keyword introduces a comma-separated list of the object's internal (private) property variables.
private property_def, property_def, ... property_def |
The structure of the private segment is exactly the same as the with segment, and in fact the only difference between the two is that the property_def settings in a with segment are accessible from other objects, while those in a private segment can be accessed only by its own object. It turns out that this distinction is of minimal value in most games, and the majority of authors never feel the need to use private properties. Follow their lead, sez I.
The has keyword introduces a space-separated list of true/false (on/off, present/absent, set/unset) flags associated with the object.
has attribute_def attribute_def ... attribute_def |
Each attribute_def is either an attr_name (which sets that flag value to true), or ~attr_name (which sets the flag to false).
The next page has more to say about the use of attributes, but here's a short example:
Object kitchen_table "table"
has static supporter
... ;
|
We've mentioned the various elements making up an object definition; now's the time to summarise what we've said so far. The commonly-found and generally useful syntax of an object is something like:
Object set_of_arrows iname "xname" parent_iname class class_iname class_iname ... class_iname with prop_name value, prop_name value, ... prop_name value has attr_name attr_name ... attr_name; |
Notes
Phew! Spelling out the object syntax is a bit of a hard slog, but it's important to be confident with what's allowed. Next, we talk some more about properties and attributes.