I6: Objects from a class already created at start of execution?

Although DM4 section 3.11 “Creating and deleting objects during play” seems to imply that objects belonging to a class with a specified number of instances aren’t created until the class’s .create() method is called, it appears that if using Inform 6.31 (10th Feb 2006), the maximum number of objects are already created at compile time. For example:

Class   Pebble(10)
    with    short_name "tiny pebble";

[ Main x ;

    print "Number of pebbles created: ", 10-Pebble.remaining();
    new_line;

    print "Objects in the world:";
    objectloop (x ofclass Object) ! note that all 10 possible instances of Pebble seem to exist with names Pebble_1, etc.
        print " ", (name) x;      ! oddly, also listed is a Pebble_11
    new_line;

];

produces output (using frotz):

Number of pebbles created: 0
Objects in the world: Pebble_1 Pebble_2 Pebble_3 Pebble_4 Pebble_5 Pebble_6 Pebble_7 Pebble_8 Pebble_9 Pebble_10 Pebble_11
[Hit any key to exit.]

Some questions:

  1. Is it correct to think that the various pebble instances already exist in some fashion at start of execution, as implied by the objectloop results? Note that the test (x ofclass Pebble) returns TRUE for Pebble_1, Pebble_2, etc. even before use of Pebble.create();

  2. What is Pebble_11 doing on that list of objects? Note that the 11th call to Pebble.create() returns nothing, as expected, so it doesn’t seem like the Pebble_11 object has any intended function. Is its creation a bug?

  3. There is a related explanatory note on p.65 “Inform does allow object creation during play, but it insists that the programmer must specify in advance the maximum resources which will ever be needed. (For example, the maximum number of beach stones which can ever be in play.) This is a nuisance, but means that the resulting story file will always work, or always fail, identically on every machine running it.” Is the implication that the maximum number of objects is created at compile time by design, and that all the class’s .create() function does is return the object number of the next “free” one?

This, exactly. Inform was designed to compile to the Z-machine, which doesn’t have any way to create or destroy objects at runtime. So it simulates “dynamic objects” by declaring a certain number of generic objects at compile-time, then initializing them and moving them into the world when they’re “created”.

No idea what Pebble_11 is doing, though. I suspect it might be an off-by-one error.

An extra “invisible” instance of the object is required as a class prototype. When you create() one, its properties get initialized by copying them over from the prototype.

1 Like

zarf, Thanks for this. It makes quite a bit about the workings of the subsystem more clear.

I don’t have all this stuff memorized, by the way. I looked at objects.c, found the make_class() function, and looked for where it was adding 1 to n. There was a comment. :)

2 Likes