inform7

How Does One "Consult" a Book on a Random Topic in Interactive Fiction?


I started with the Costa Rican Ornithology example, which works as expected. I had the idea, largely for my own gratification, of allowing the player to flip randomly through the book since I don't expect to release this, to sample a random topic.

I pieced this together from several other examples.

Understand "consult [a book]" as consulting vaguely.

Consulting vaguely is an action applying to one visible thing.

Carry out consulting vaguely:
    let B be the book;
    let L be the number of rows in the contents of B;
    let N be a random number from 1 to L;
    choose row N in the contents of B;
    say "[reply entry][paragraph break]".

I've separated out the steps for clarity (and to ease the parsing and error reporting), but basically, choose a random row in the table of contents, then print the result. I also tried choose a random row... but that doesn't change the results seen below.

Where contents (like in the example) represents the table of topics and results.

A book is a kind of thing.  A book has a table name called the contents.

Unfortunately, this code crashes the game when trying to "consult book," with the following run-time error.

*** Run-time problem P10: Since the your former self is not allowed the property “contents”, it is against the rules to try to use it.

Ah-ha, I said to myself, contents sounds like what might sit inside a container, so of course I can't access it. I can change it to something distinct like ToC, "table of contents," though the meaning is less important than it not colliding with another name.

Unfortunately, that also doesn't work, leading me to believe that a table name is not the same as a table. This makes sense, but I don't see any path to bridging from the table's name (if that's actually what I'm seeing) to the actual table. And I also don't know that I understand what my "former self" has to do with anything, but I don't know how critical that is in understanding the problem.


Solution

  • The main issue is that you're referring to the book that the player has used in the command as "the book" but that's not correct – it should be "the noun". As "a book is a kind of thing" there is no "the book", you have to be more specific which book you mean. In this case it's "the noun" which is what the player specified in their command.

    Carry out consulting vaguely:
        let B be the noun;
        let L be the number of rows in the contents of B;
        let N be a random number from 1 to L;
        choose row N in the contents of B;
        say "[reply entry][paragraph break]".
    

    or more compactly,

    Carry out consulting vaguely:
        choose a random row in the contents of the noun;
        say "[reply entry][paragraph break]".