Pre-release
Adventure.js Docs Downloads
Score: 0 Moves: 0
Tutorial explaining how to work with verb actions in Adventurejs. tutorial, verb actions

Start Scripting:Verb Actions

Verb actions provide a hook for authors to inject custom code into the doTry and doSuccess phases of a verb. Verb actions can be used to augment or override a verb's built-in logic. Action calls are made from within built-in blocks of code. Usually, actions are placed as close as possible to the top of a block, so that an author can override the entire block if they choose. Sometimes, actions may fall a little lower in the block; for example, if the verb needs to resolve some issue involving the order of words in the input before moving on to processing the nouns. This is just a mild warning to note that verb actions may not be perfectly consistent from verb to verb.

Here's an example of how you might hook an asset into a verb action. In this example, onTryThrowThis, onTryThrowThisAtThat, onThrowThis and onThrowThisAtThat are all actions of throw. You can find each verb's actions listed on its doc page.

MyGame.createAsset({
  class: "NPC",
  name: "cave troll",
  place: {in: "cave"},
});

MyGame.createAsset({
  class: "Weapon",
  name: "throwing star",
  dov: { 
    throw: { 
      // this lets a player throw the star at anything
      with_anything: true, 
    }, 
  }, 
  descriptions: {
    look: "It's a little star of black metal with sharpened points. ",

    // descriptions.throw is one way to print a string when the asset is thrown
    // If you also use verb actions as shown below, 
    // you may find the output competes with itself.
    // You can choose which option works the best for you.
    throw: "It bounces across the floor with a series of metallic clangs. ",
  },
  article: "the",
  place: { in: "weapons rack" },
  onTryThrowThis: function(params)
  {
    // this results if the player tries to throw the star
    // it will print independently of any other output the verb prints
    let msg = "You limber up your arm for a good overhand throw. ";
    MyGame.print(msg);
  },
  onTryThrowThisAtThat: 
  { 
    "cave troll": function(params)
    {
      // this results only if the player tries to throw the star at the troll
      let msg = "You size up the troll and ready your arm for a strong throw. ";
      MyGame.print(msg);
    }
  },
  onThrowThis: function(params)
  {
    // this results if the player succeeds in throwing the star
    let msg = "You feel a painful twinge in your arm after releasing the star. ";
    MyGame.print(msg);
  },
  onThrowThisAtThat: 
  { 
    "cave troll": function(params)
    {
      // this results only if the player succeeds in throwing the star at the troll
      let msg = "The star thuds to a stop in the troll's chest. The troll looks annoyed. ";
      MyGame.print(msg);
    }
  }
});
Phase Hooks
Verb Phases
Verb Actions

Verb actions exist on the asset.
They're expected to return text, but they can also execute code.
If text is return it will be appended to output.
Returning TRUE or no value (aka UNDEFINED) lets the verb continue
Returning NULL ends operations for this asset. If multiple actions are queued, operations will continue.
Returning FALSE ends operations for all queued actions as well as this one.