Adventure.js Docs Downloads
Score: 0 Moves: 0
Tutorial explaining how to work with verb effect hooks in Adventurejs. tutorial, verb effect hooks

How to:Verb Effect Hooks

Verb effect hooks are identical to verb event hooks, with the exception that they are not tied to specific verbs. You might say that effects are the results of events. For instance, consider "take lantern" or "put spellbook in knapsack" or "throw knife at troll". Each one of these verbs, and many other verbs as well, will result in moving one asset out of a second asset and into a third asset (even considering that the second or third asset may be a room, the same logic applies). The lantern will be moved from the room to the player. The spellbook will be moved from the player to the knapsack. The knife will be moved from the player to the troll (or to the room, if it misses). In cases like these, Adventurejs will check for the existence of four different hooks, and call any code that it finds:

  • direct_object.onRemoveThisFromThat( indirect_object )
  • indirect_object.onRemoveThatFromThis( direct_object )
  • direct_object.onMoveThisToThat( indirect_object )
  • indirect_object.onMoveThatToThis( direct_object )

Authors can use any of these. Or none of them! But you don't have to use all of them (unless you really want to). An author who wants to make something special happen when an asset changes places can tie that code to any of the assets involved. The goal behind this is to offer as much flexibility as possible in letting authors choose how to organize asset code.

Here is a list of effect hooks.

  • onMoveThatToThis - when that asset is moved to this asset
  • onRemoveThisFromThat - when this asset is removed from that asset
  • onRemoveThatFromThis - when that asset is removed from this asset
  • onMoveThisToThat - when this asset is moved to that asset
  • onNestThatToThis - when that character is nested to this asset
  • onNestThisToThat - when this character is nested to that asset
  • onUnnestThatFromThis - when that character is unnested from this asset
  • onUnnestThisFromThat - when this character is unnested from that asset
  • onSubtractSubstanceFromThis - when a substance is subtracted from this asset
  • onAddSubstanceToThis - when a substance is added to this asset
  • onDestroyThis - when this asset is removed from the game
  • onChangeMoisture - when this asset is moistened
  • onChangeTemperature - when this asset is subjected to a change in temperature
  • onChangeGravity - when this asset is subjected to a change in gravity

Here's an example that assigns its event hook code to the indirect object, using onMoveThatToThis and onRemoveThatFromThis. (Note the order of That and This. Indirect objects will usually use some form of on[Foo]That[Preposition]This)

MyGame.createAsset({
  class: "NPC",
  name: "Elvis",
}),
MyGame.createAsset({
  class: "Room",
  name: "The Building",
  event_hooks: {
    onMoveThatToThis: 
    {
      "Elvis": function() 
      {
        MyGame.print("Elvis has entered The Building! ");
      }
    },
    onRemoveThatFromThis: 
    {
      "Elvis": function() 
      {
        MyGame.print("Elvis has left The Building! ");
      }
    },
  },
}),

Here is a reverse example, where the event hook code is assigned to the direct object using onMoveThisToThat and onRemoveThisFromThat. (Note the order of This and That. Direct objects will usually use some form of on[Foo]This[Preposition]That)

MyGame.createAsset({
  class: "NPC",
  name: "Elvis",
  event_hooks: {
    onMoveThisToThat: 
    {
      "The Building": function() 
      {
        MyGame.print(`Elvis announces his arrival by strumming the opening chords of "Blue Suede Shoes." `);
      }
    },
    onRemoveThisFromThat: 
    {
      "The Building": function() 
      {
        MyGame.print(`Elvis bids farewell by playing the closing chords of "Hound Dog." `);
      }
    },
  },
}),
MyGame.createAsset({
  class: "Room",
  name: "The Building",
}),
Documentation generated by JSDoc 3.6.11 on Mon Nov 20 2023 18:04:50 GMT-0800 (Pacific Standard Time)
Found a problem or error in the docs? Report it to docs@adventurejs.com.