I6: Options for desired but missing class-related functionality?

The DM4 p. 66 lists five built-in functions for classes that are defined with a number of instances, as in the example on the preceding page:

Class Pebble(50)...

The Pebble.remaining() method will return the number of “not yet created” pebbles. Is there any comparable method along the lines of Pebble.created() or Pebble.instantiated() that would return how many pebbles have already been created? If not, would it be possible to create this using advanced features like Z-machine machine language and/or direct inspection of Z-machine memory?

Also, is there any way to access the nth instance of a class of this type? Again, if it’s not possible to do this easily, would it be possible using advanced features?

Constant MAX_PEBBLES = 20;
Class Pebble(MAX_PEBBLES)…

[ PebblesCreated ;
    return MAX_PEBBLES - Pebble.remaining();
Global pebble_base;

[ PebbleNth n ;
    if(pebble_base == 0){
        if(PebblesCreated() ~= 0){
            "Runtime error: this hack is fragile and requires you to call ~PebbleNth~ at least once before creating any pebbles."
        pebble_base = Pebble.create();
    return pebble_base + n;

The better way to accomplish this, though, would be to make changes to the compiler. That way you could add created() and nth(n) methods that work properly without requiring these sorts of hacks.

I don’t think compiler changes like this are a good idea. The resulting game code wouldn’t be better or more efficient; it would just be easier to invoke. But, on the down side, the behavior would then be hardwired into the language. If you didn’t like it, you’d have to implement it in your own code, which is what you’re doing already.

If you want the Nth of the class, it’s probably best to create an array of object references. Setting this up at the start of play is easy. But it costs memory, so not every game will want to do that.

1 Like


Thank you for the example workarounds; they are interesting, if not perfect.


I appreciate your argument about not adding one-off functionality to the compiler, and your suggestion of populating an array at the start of execution. I was trying to think up a way to add the “missing” methods to the Pebble class directly, but I’m not sure that this is even possible. I thought that for retriving the maximum members maybe something along the lines of:

Class   Ext_Class
    with    max_members
                [ x n ;
                    print "Calculating max members for ", (name) self, "...^";
                    objectloop(x ofclass self)
                    return n;

Class   Pebble
    class   Ext_Class...

might make it possible to call Pebble.max_members(), but it doesn’t seem to do what I had hoped. (The max_members method is inherited by individual pebble instances, not the Pebble class. Calling self.Pebble::max_members() from a pebble instance produces an RTE that the method doesn’t exist for class Pebble.)

Is there any way to create a “sibling” method to standard methods like X.remaining(), X.create() and X.destroy(), or is every method attached to a class hard-coded by the compiler?

They’re hard-coded. If you want to customize the class system, write functions.