【问题标题】:Need help Creating new objects in inform7需要帮助在 inform7 中创建新对象
【发布时间】:2011-03-22 03:10:23
【问题描述】:

Inform7 非常新,它的风格。我浏览了提供的文档,一些互联网浏览对我没有任何帮助......这是我正在寻找的简单版本。我想写这样的东西:

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.

“在休息室制作汽水(称为汽水)。”显然不是一个有效的命令,但我希望它传达了我想要做的事情。我不知道如何在运行时实例化对象。这可以合理地完成吗?任何帮助,将不胜感激。我知道 Inform 的追随者并不多,但我想我会试一试。

【问题讨论】:

    标签: object creation inform7


    【解决方案1】:

    Inform 不能很好地处理动态对象,但它们通常不是最好的方法。手册中的10.3. Dispensers and Supplies of Small Objects 部分可能会有所帮助。

    我认为最好的模型是实体模型:在机器中制造有限供应的罐头。例如:

    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".
    

    (如果您愿意,请将机器设为opaque 而不是transparent!)。在上面,我还调整了汽水的描述——如果你只是在对象定义后说"Blah"而不是The description is "Blah",那么你设置了初始描述(作为房间描述的一部分打印)而不是“检查”描述,我认为这不是您想要的——而且我已将按钮设为机器的“一部分”,而不是单独的对象。

    结果:

    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.
    
    >
    

    【讨论】:

    • 感谢您的帮助。我意识到你今天早上提到的例子。这是一个很好的帮助。但是,如果我说机器中有 2000 个 pop 会占用大量内存,或者它会在最小的空间中存储一堆相同的对象,那么对此进行后续处理。 (即如果 1 个 pop 是 100 个字节,那么 2000 个 pop 是 100x2000 个字节吗?)
    • 没关系。使用提供的链接,有一个句子描述了每个对象实际上确实消耗了内存。
    【解决方案2】:

    我写了一个扩展来做这种事情:https://github.com/i7/extensions/blob/master/Jesse%20McGrew/Dynamic%20Objects.i7x

    要使用它,您必须创建一个原型对象(例如,“原始汽水”),然后使用表达式 a new object cloned from the original soda pop 来实例化新对象。这比创建大型静态对象池更节省内存,但它不适用于 Z 机器(仅限 Glulx),并且如果您的对象很复杂,则需要注意一些事项。

    另外,请认真考虑您是否真的需要动态对象创建。如果你只是想出一个合理的理由来拒绝这个动作,比如“你连买的最后一瓶汽水都没喝完,你就不能让自己花钱”,这对玩家来说可能会更容易,也不会让人困惑。放置几千个汽水罐可能会使游戏变慢而不会增加太多好处。

    【讨论】:

    • 有道理。这是我打算做的1个具体示例。我有一棵灌木,每 6 小时就结一次浆果。这些浆果可以稍后被玩家食用或储存。尽管我可以很容易地找到玩家一次可以携带的浆果数量的实际限制,但似乎我会编写代码以在被吃掉或丢弃后从虚空中复活这些浆果,而该代码看起来并不优雅vs 在创建和进食时实例化和销毁浆果。无论如何,谢谢你的扩展链接,我去看看。
    • 该扩展不允许您销毁对象,只能创建新对象(因为 I7 缺乏避免悬空指针所需的自省功能)。所以你仍然需要编写某种回收代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多