Pre-release
Adventure.js Docs Downloads
Score: 0 Moves: 0
Tutorial explaining how to add some treasure in Adventurejs. tutorial, add some treasure

Get Started:Add Some Treasure

Our puzzle is in place. All that's missing now is a reward for the player. We'll start with a chest that can hold some treasure. We won't bother to set a key for this chest because the process is identical to the way we set a key for the door.

MyGame.createAsset({
  class: "Chest",
  name: "brass chest",
  place: { in: "Central Chamber" },
  description: `It's a small but sturdy looking chest bound in brass$( brass chest is| open then| and lined with yolk colored satin ). `,
  adjectives: [ "treasure", "holy" ],
  is: { 
    closed: true, 
    locked: false,
  },
  dov: { 
    open: { 
      with_nothing: true, 
      on_first_success: function(){ MyGame01.overrideOutput(`The chest's interior is lined with a rich golden yolk colored satin that fairly glows in the dim dungeon. Nestled comfortably inside the yolky satin bed is a small jade statuette. `); }, }, 
    close: { with_nothing: true }, 
  },
});
#TMI How to lock the chest

We're not locking the chest for this demo, but ok, you really want to see how we'd do it. It's the same as the key for the door - we just need to specify what the key unlocks. And since we set the chest to is: { locked: false } we'll want to revise that to is: { locked: true }.

MyGame.createAsset({
  class: "Key",
  name: "brass key",
  iov: { unlock: { with_assets: ['brass chest'], }, },
  description: "It's a small brass key that looks like it's been gnawed on. ",
  article: "a",
  adjectives: [ ],
  place: { in: "Central Chamber" },
});
#TMI About on_first_success: function()

You might have noticed that we did something new here.

on_first_success: function(){ MyGame01.overrideOutput(`The chest's interior is lined with a rich golden yolk colored satin that fairly glows in the dim dungeon. Nestled comfortably inside the yolky satin bed is a small jade statuette. `); }, }, 

We did this to override the game's description of the chest the first time it's opened, and there are actually several things to note here.

  • The default typeof dov.verb.on_first_success is a string, and if a string is found it will be appended to the output of a turn the first time a verb is successfully applied to an asset. This is documented at greater length in Customize Output.
  • However, you can see that, in this example, we've set on_first_success to a function rather than a string. That's because this property passes through String|Array|Function which is a method that can handle several variable types.
  • Our custom function is calling another function, MyGame01.overrideOutput(). As you might imagine, that is a way to override the output of the turn. There are similar methods to prepend or append text to the default output. These are also documented in Customize Output.

And here's some treasure to put in the chest. We don't have any particular Treasure class so we'll just make an instance of Thing and give it take and drop verb subscriptions.

MyGame.createAsset({
  class: "Thing",
  name: "jade statuette",
  synonyms: ["statue","dragon"],
  adjectives: ["green"],
  dov: { 
    drop: true, 
    take: {
      on_first_success: function(){
        MyGame01.scorecard.completeEvent('take statuette');
      },
    },
  },
  description: `It's a breathtakingly detailed statuette of a dragon in miniature. The polished jade has a translucent effect that fairly glows, drawing your gaze into its depths. It takes effort to drag your eyes away from it. `,
  place: { in: "brass chest" },
});
// game constructor block...
// player constructor block...
// dungeon antechamber constructor block...
// north exit constructor block...
// wood planked door constructor block...
// central chamber constructor block...
// south exit constructor block...
// iron bound door constructor block...
// beaten iron key constructor block...
// pitted cobblestone constructor block...
// weathered cobblestone constructor block...
// cracked cobblestone constructor block...
// cobblestones constructor block...
// brass chest constructor block...
// jade statuette constructor block...

We're almost done here. Next, let's add some scoring and a win event.

NEXT: Set Scores