Pre-release
AdventureJS Docs Downloads
Score: 0 Moves: 0
Tutorial explaining how to work with verb reactions in AdventureJS. tutorial, verb reactions, doMoveThatToThis, doRemoveThisFromThat, doRemoveThatFromThis, doMoveThisToThat, doNestThatToThis, doNestThisToThat, doUnnestThatFromThis, doUnnestThisFromThat, doSubtractSubstanceFromThis, doAddSubstanceToThis, doDestroyThis, doChangeMoisture, doChangeTemperature, doChangeGravity

Basic Verb Use:Reaction Hooks

Verb reactions are identical to verb actions, 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.doRemoveThisFromThat( indirect_object )
  • indirect_object.doRemoveThatFromThis( direct_object )
  • direct_object.doMoveThisToThat( indirect_object )
  • indirect_object.doMoveThatToThis( 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 available verb reactions.

  • doActivate - when a tangible asset is turned on
  • doAddSubstanceToThis - when a substance is added to this asset
  • doChangeGravity - when this asset is subjected to a change in gravity
  • doChangeMoisture - when this asset is moistened
  • doChangeTemperature - when this asset is subjected to a change in temperature
  • doDeactivate - when a tangible asset is turned off
  • doDestroyThis - when this asset is removed from the game
  • doIngestThat - when that asset is ingested by this character
  • doIngestThis - when this asset is ingested by a character
  • doMoveThatToThis - when that asset is moved to this asset
  • doMoveThisToThat - when this asset is moved to that asset
  • doNestThatToThis - when that character is nested to this asset
  • doNestThisToThat - when this character is nested to that asset
  • doRemoveThatFromThis - when that asset is removed from this asset
  • doRemoveThisFromThat - when this asset is removed from that asset
  • doSubtractSubstanceFromThis - when a substance is subtracted from this asset
  • doTieThatToThis - when that rope is tied to this asset
  • doTieThisToThat - when this rope is tied to that asset
  • doUnnestThatFromThis - when that character is unnested from this asset
  • doUnnestThisFromThat - when this character is unnested from that asset

Here's an example that hooks into the verb reaction of to the indirect object, using doMoveThatToThis and doRemoveThatFromThis. (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",
  doMoveThatToThis: 
  {
    "Elvis": function() 
    {
      MyGame.print("Elvis has entered The Building! ");
    }
  },
  doRemoveThatFromThis: 
  {
    "Elvis": function() 
    {
      MyGame.print("Elvis has left The Building! ");
    }
  },
}),

Here is a reverse example that hooks into the verb reaction of the direct object using doMoveThisToThat and doRemoveThisFromThat. (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",
  doMoveThisToThat: 
  {
    "The Building": function() 
    {
      MyGame.print(`Elvis announces his arrival by strumming the opening chords of "Blue Suede Shoes." `);
    }
  },
  doRemoveThisFromThat: 
  {
    "The Building": function() 
    {
      MyGame.print(`Elvis bids farewell by playing the closing chords of "Hound Dog." `);
    }
  },
}),
MyGame.createAsset({
  class: "Room",
  name: "The Building",
}),

Verb reactions are optional methods that authors can use to add custom code. There are numerous effect that may be overridden. In this example, we set the doMoveThatToThis and doRemoveThatFromThis verb reactions in order to print a custom message whenever the player character enters or leaves this particular room.

MyGame.createAsset({
    class: "Room",
    name: "My Room",

    doMoveThatToThis: 
    {
      "My Hero": function() 
      {
        MyGame.print("My Hero has come to My Room! ");
      }
    },
    doRemoveThatFromThis: 
    {
      "My Hero": function() 
      {
        MyGame.print("My Hero has left My Room. ");
      }
    },
    
  },
  


To learn more about verb reactions... TODO

Consider the verb drop. Inputting "drop lantern" will result in two events – removing the lantern from the player and moving it to the room – which causes these four verb reactions:

lantern.doRemoveThisFromThat(player)
player.doRemoveThatFromThis(lantern)
lantern.doMoveThisToThat(room)
room.doMoveThatToThis(lantern)

There are four reactions because each asset in the interaction is checked for custom code pertaining to the other. This may seem redundant, but it's done in a deliberate effort to provide flexibility to authors. If you prefer to attach custom code to the lantern, you can. If you prefer to attach custom code to the player, you can. You're welcome to organize your code in whichever way serves you best.

Expand for example

In this example, imagine that an author would like the game to print a custom message whenever Elvis enters or leaves the building, regardless of what verb is used. Authors can hook into any of the doRemoveThisFromThat, doRemoveThatFromThis, doMoveThisToThat, or doMoveThatToThis verb reactions. Below is a generic example of how to code a verb reaction.

MyGame.createAsset({
class: "Player",
name: "Elvis",
}),
MyGame.createAsset({
class: "Room",
name: "The Building",
doMoveThatToThis: 
{
  "Elvis": function() 
  {
    MyGame.print("Elvis has entered The Building! ");
  }
},
doRemoveThatFromThis: 
{
  "Elvis": function() 
  {
    MyGame.print("Elvis has left The Building! ");
  }
},
}),
Expand for example

In this example, imagine that an author would like the game to print a custom message whenever Elvis enters or leaves the building, regardless of what verb is used. Authors can hook into any of the do_remove_this_from_that, do_remove_that_from_this, do_move_this_to_that, or do_move_that_to_this verb reactions. Below is a generic example of how to code a verb reaction.

MyGame.createAsset({
class: "Player",
name: "Elvis",
}),
MyGame.createAsset({
class: "Room",
name: "The Building",
do_move_that_to_this: 
{
  "Elvis": `Elvis has entered The Building! `;
},
do_remove_that_from_this: 
{
  "Elvis": `Elvis has left The Building! `;
},
})

When a custom function is called, the calling function passes in a parameter object with asset1 (which may be null), asset2 (which may be null, and params (which may be an empty object)