Gamefic

Adventure Games and Interactive Fiction

Dynamic Entities and Subplots

by Gamefic on November 27, 2016

A while back I quietly introduced an improvement to dynamic entities. It's always been possible to generate entities at runtime, but for a long time they weren't compatible with snapshots. In other words, any entity created at runtime would be lost in an undo or restore. Gamefic 1.0 was the first release to handle dynamic entities properly in snapshots. The latest version extends this capability with a new feature called subplots.

Creating an entity at runtime works with the usual make method. Here's a crude example that lets a player make infinite coins:

respond :forge do |actor|
  make Item, name: 'coin', description: 'A gold coin.', parent: actor
  actor.tell "You forged a gold coin."
end

A subplot allows you to generate entities that will automatically be destroyed at its conclusion. In this next version of the forge action, we start a subplot that gives the player a portable coin mint.

respond :forge do |actor|
  # The branch method creates a generic subplot and attaches it to the main plot.
  branch { |subplot|
    subplot.make Item, name: 'portable coin mint', description: 'Use this to make a coin. You can only use it once.', parent: actor
  }.introduce actor
  actor.tell "You're in a forging subplot."
end

The code uses subplot.make to create the mint so it will be marked as belonging to the subplot. Any entity created by a subplot includes the Subplot::Element module and has a #subplot method to identify its creator.

We can add a command to use the mint with another respond:

respond :use, Use.reachable(Item, Subplot::Element) do |actor, item|
  if item.name == 'portable coin mint'
    make Item, name: 'coin', description: 'A gold coin.', parent: actor
    actor.tell "You forged a gold coin."
    item.subplot.conclude
  else
    # Look for another appropriate action
    actor.proceed
  end
end

Note the call to item.subplot.conclude. This method destroys all the entities that belong to the subplot, i.e., the portable coin mint. The coin itself was made using the normal make method, which means it will persist after the subplot ends.

Subplots are still experimental and liable to change in a future release. Please feel free to provide feedback in the comment section or at the GitHub repo.