9

Very new to Inform7 and it's style. I have looked through the provided docs and some internet browsing has yielded nothing for me... this is a simplistic version of what i'm looking for. I want to write something like this:

breakroom is a room. "A run of the mill breakroom."

soda pop is a kind of thing. "A refreshing soda pop."

soda machine is in the breakroom.  dispense button is on the soda machine.

instead of pushing dispense button:
    say "A soda can dispenses".
    create a soda pop (called pop) in the breakroom.

"create a soda pop (called pop) in the breakroom." is not a valid command obviously, but I hope it conveys what I want to do. I don't know how to instantiate objects at runtime. Can this be done reasonably? Any help would be appreciated. I am aware that there's not a big following here for Inform but I figure i'd give it a shot.

deepee1
  • 11,640
  • 4
  • 28
  • 43

2 Answers2

8

Inform doesn't handle dynamic objects very well, but they're often not the best approach anyway. Section 10.3. Dispensers and Supplies of Small Objects in the manual may be helpful.

I think the best model for this is a physical one: create a limited supply of cans in the machine. For example:

Breakroom is a room. "A run of the mill breakroom."

A soda pop is a kind of thing.  The description is "A refreshing soda pop."

The soda machine is in the breakroom.  It is fixed in place and transparent.
The description is "Just an average soda machine, with a large dispense
button."

There are three soda pops in the soda machine.

The dispense button is a part of the soda machine.

Instead of pushing the dispense button:
        if a soda pop (called the can) is in the soda machine:
                move the can to the breakroom;
                say "A soda can dispenses.";
        otherwise:
                say "The machine is empty, so nothing happens.".

Test me with "look / x machine / push button / look / push button /
push button / push button / look".

(make the machine opaque rather than transparent if you prefer!). In the above, I've also tweaked the description of a soda pop -- if you just say "Blah" rather than The description is "Blah" after an object definition, you set the initial description (printed as part of the room description) rather than the "examine" description, which I don't think is what you want here -- and I've made the button a "part" of the machine, rather than a separate object.

The result:

Welcome
An Interactive Fiction
Release 1 / Serial number 110324 / Inform 7 build 6G60 (I6/v6.32 lib 6/12N) SD

Breakroom
A run of the mill breakroom.

You can see a soda machine (in which are three soda pops) here.

>test me
(Testing.)

>[1] look
Breakroom
A run of the mill breakroom.

You can see a soda machine (in which are three soda pops) here.

>[2] x machine
Just an average soda machine, with a large dispense button.

In the soda machine are three soda pops.

>[3] push button
A soda can dispenses.

>[4] look
Breakroom
A run of the mill breakroom.

You can see a soda pop and a soda machine (in which are two soda pops) here.

>[5] push button
A soda can dispenses.

>[6] push button
A soda can dispenses.

>[7] push button
The machine is empty, so nothing happens.

>[8] look
Breakroom
A run of the mill breakroom.

You can see three soda pops and a soda machine (empty) here.

>
Matthew Slattery
  • 41,297
  • 6
  • 92
  • 117
  • Thanks for the help here. I became aware of the example you mentioned just this morning. This is a good help. One followup on this though, if I said that 2000 pops were in the machine would that use up a huge amount of memory or does it store a stack of identical objects in a minimal amount of space. (i.e. if 1 pop is 100 bytes, then is 2000 pops 100x2000 bytes?) – deepee1 Mar 25 '11 at 05:22
  • Never-mind on this one. Using the provided link there was a sentence describing that each object did in fact eat up memory. – deepee1 Mar 25 '11 at 06:13
6

I wrote an extension to do this sort of thing: https://github.com/i7/extensions/blob/master/Jesse%20McGrew/Dynamic%20Objects.i7x

To use it, you'd have to create a prototype object (say, "the original soda pop"), then use the expression a new object cloned from the original soda pop to instantiate the new object. This is more memory-efficient than creating a large static pool of objects, but it doesn't work on the Z-machine (only Glulx) and has some caveats if your objects are complicated.

Also, think seriously about whether you really need dynamic object creation. It may be easier and less confusing for players if you just come up with a sensible reason to reject the action, like "You can't bring yourself to spend the money when you haven't even finished the last soda you bought." Having a couple thousand soda cans lying around is likely to make the game slower without adding much benefit.

TheBeardyMan
  • 779
  • 9
  • 25
Jesse McGrew
  • 1,819
  • 17
  • 28
  • Makes sense. Here is 1 specific example of what I was planning to do. I have a bush that makes berries every 6 hours. These berries can be consumed or stockpiled by the player later on. Although I can easily find a practical limit to the number of berries a player could carry at once, it just seemed I would be writing code to resurrect these berries from the void after being eaten or discarded and that code just didn't seem very elegant vs instantiating and destroying the berries on creation and eating. Anyways, thanks for the extension link, I'll check it out. – deepee1 Apr 19 '11 at 15:04
  • The extension doesn't let you destroy objects, only create new ones (since I7 lacks the introspection features needed to avoid dangling pointers). So you'd still need to write some sort of recycling code. – Jesse McGrew Apr 25 '11 at 22:25