Pre-release
AdventureJS Docs Downloads
Score: 0 Moves: 0
Tutorial explaining how to override output in AdventureJS. tutorial, override output, override_failure_msg, override_success_msg, prependOutput, appendOutput, overrideOutput, on_failure, on_first_failure, on_success, on_first_success

Basic Scripting:Override Output

The methods presented here are offered as beginner's tools, in that you can use them simply by writing strings, without any custom code. In fact, any of these methods can accept a string or an array or a function, because they pass through getStringArrayFunction() on their way to the player. For now we're focused on the string methods, but we'll present examples of each type, just to introduce the concepts. You're welcome to use whichever methods make the most sense to you as they are all equally valid.

Override one verb for all assets
override_failure_msg, override_success_msg

Override one verb for one asset, via verb subscription
on_failure, on_first_failure, on_success, on_first_success, on_destroy

Override all verbs for one asset
print_this_for_all_verb_failures,

Override an entire turn's output
prependOutput, appendOutput, overrideOutput

Override one verb for all assets

override_failure_msg, override_success_msg

These two methods broadly override a verb's success or failure messages for all assets. They are the simplest and bluntest tool of all the available methods. Using these requires calling the game.modifyVerb() method to edit the verb itself.

In this first example we'll set the verb fly to return a snarky response on any success or failure. Here's an example of the default output.

> fly You can't fly while you're in the crystal cave.

Override one verb for all assets String

MyGame.modifyVerb({
  name: "fly",
  override_failure_msg: "Do you see wings? I don't see wings. ",
  override_success_msg: "Wheee! ",
}),

And the results:

> fly Do you see wings? I don't see wings.

Override one verb for all assets Array

In the next example, we'll set each override property of fly to an array. Arrays are recognized automatically by getStringArrayFunction() and can be set to return array items randomly or sequentially.

MyGame.modifyVerb({
  name: "fly",
  override_failure_msg: {
    randomize: true,
    array: [
      "No.",
      "You find that you can't.",
      "You bump your head on the ceiling.",
    ],
  },
  override_success_msg: {
    randomize: true,
    array: [
      "Wheee!",
      "You fly!",
      "Up, up, and away!",
    ],
  },
});

And the results:

> fly No. > fly You bump your head on the ceiling. > wear wax wings You strap on the wax wings. > fly Up, up, and away!

Override one verb for all assets Function

In this example, we set each override property of fly to a function. When a function is found it is automatically called by getStringArrayFunction(). If the function returns a string, that will override the default output. (In fact, the function isn't required to return a string; it can be used to run custom code instead. We'll cover variations on that in a later tutorial.)

MyGame.modifyVerb({
  name: "fly",
  override_failure_msg: function () {
    if(MyGame.getPlayer().$is("wearing", "wax wings")){
      return `Though you're wearing the wax wings, conditions don't permit it. `;
    } else {
      return `You might need a flying aid. `;
    }
  },
  override_success_msg: function () {
    if(MyGame.getPlayer().$is("wearing", "wax wings")){
      return `Be careful not to fly too close to the sun! `;
    }
  },
});

And the results:

> fly You can't fly while you're in the crystal cave. You might need a flying aid. > wear wax wings You strap on the wax wings. > fly You can't fly while you're in the crystal cave. Though you're wearing the wax wings, conditions don't permit it. > exit cave You exit the crystal cave. > fly You take flight. Be careful not to fly too close to the sun!

Override one verb for one asset

on_failure, on_first_failure, on_success, on_first_success, on_destroy

These methods are features of verb subscriptions. Objects use verb subscriptions to tell verbs how to act on them. Each verb subscription can be uniquely customized. These methods can be used to append text to a verb's output when the verb acts on the object that is subscribed to it.

Here's our basic setup and a default output.

MyGame.createAsset({
  class: "Player",
  name: "Hero",
)};
MyGame.createAsset({
  class: "Edible",
  name: "stinky cheese",
)};
> eat stinky cheese You can't eat the stinky cheese.

Override one verb for one asset on_first_failure

Only called the first time the verb fails to be applied to this asset, in case you want a variation between the first attempt and subsequent attempts.

MyGame.createAsset({
  class: "Edible",
  name: "stinky cheese",
  dov: {
    eat: {
      on_first_failure: `It's far too strong. You're going to need a good strong stout to pair it with.`,
    },
  },
)};

Override one verb for one asset on_failure

Called each time the verb fails to be applied to this asset.

MyGame.createAsset({
  class: "Edible",
  name: "stinky cheese",
  dov: {
    eat: {
      on_failure: `Not without a good strong stout at hand. `,
    },
  },
)};

Override one verb for one asset on_first_success

Only called the first time the verb is successfully applied to this asset, in case you want a variation between the first use and subsequent uses.

MyGame.createAsset({
  class: "Edible",
  name: "stinky cheese",
  dov: {
    eat: {
      on_first_success: `Then, you sip the strong stout. The two bold flavors complement each other brilliantly.`,
    },
  },
)};

Override one verb for one asset on_success

Called each time the verb is successfully applied to this asset.

MyGame.createAsset({
  class: "Edible",
  name: "stinky cheese",
  dov: {
    eat: {
      on_success: `It goes down well with the strong stout.`,
    },
  },
)};

Override one verb for one asset on_destroy

If asset.dov.verb.then_destroy is true, the asset will be destroyed after the verb is applied, and if asset.dov.verb.on_destroy is set to any value, that value will be printed.

MyGame.createAsset({
  class: "Edible",
  name: "stinky cheese",
  dov: {
    eat: {
      then_destroy: true, 
      on_destroy: `You greedily gulp down the entire block of cheese.`,
    },
  },
)};

Override one verb for one asset Results

In the prior examples we showed one method at a time to help distiguish them, but because they all operate on the same object, we would define them all together.

MyGame.createAsset({
  class: "Edible",
  name: "stinky cheese",
  dov: {
    eat: {
      on_first_failure: `It's far too strong. You're going to need a good strong stout to pair it with.`,
      on_failure: `Not without a good strong stout at hand. `,
      on_first_success: `Then, you sip the strong stout. The two bold flavors complement each other brilliantly.`,
      on_success: `It goes down well with the strong stout.`,
      then_destroy: true,
      on_destroy: `You greedily gulp down the entire block of cheese.`,
    },
  },
)};

Finally, here are all four verb subscription properties in action.

> eat stinky cheese You can't eat the stinky cheese. It's far too strong. You're going to need a good strong stout to pair it with. > eat stinky cheese You can't eat the stinky cheese. Not without a good strong stout at hand. > take stout You take the strong stout. > eat stinky cheese You eat a bite of the stinky cheese. Then, you sip the strong stout. The two bold flavors complement each other brilliantly. > eat stinky cheese You eat a bite of the stinky cheese. It goes down well with the strong stout. > eat stinky cheese You eat a bite of the stinky cheese. It goes down well with the strong stout. You greedily gulp down the entire block of cheese.

And a last word... the default behavior of these methods is to append text. But, just like the override methods above, they can be configured to append, prepend, override, or run custom code.

Override all verbs for one asset

print_this_for_all_verb_failures

This methods broadly overrides all verb failure messages for one specified asset. This is a broad tool for one asset. In the provided example, we assume an imaginary object that only responds to a limited set of verbs, where all other verbs will return the common failure message. In this first example, we'll set print_this_for_all_verb_failures to return a string.

Override all verbs for one asset String

MyGame.createAsset({
  class: "Scenery",
  name: "imaginary object",
  place: { in: "Play Room" },
  list_in_room: false,
  print_this_for_all_verb_failures: `While {we} can think about the imaginary object, 
  or ask or tell about it, {we} can't {verb} it. `,
}),

And the results:

> eat imaginary object While you can think about the imaginary object, or ask or tell about it, you can't eat it.

Override all verbs for one asset Array

In the next example, we'll set the imaginary object's override property to an array. Arrays are recognized automatically by getStringArrayFunction() and can be set to return array items randomly or sequentially.

MyGame.createAsset({
  class: "Scenery",
  name: "imaginary object",
  place: { in: "Play Room" },
  list_in_room: false,
  print_this_for_all_verb_failures: {
    randomize: true,
    array: [
      `While {we} can think about the imaginary object, 
      or ask or tell about it, {we} can't {verb} it. `,
      `{We} don't know of any way to {verb} the imaginary object,
      but {we} can think about or ask about or tell about it. `,
      `Don't be ridiculous, {we} can't {verb} the imaginary object. `,
    ],
  },
});

And the results:

> eat imaginary object You don't know of any way to eat the imaginary object, but you can think about or ask about or tell about it.

Override all verbs for one asset Function

In this example, we set print_this_for_all_verb_failures to a function. When a function is found it is automatically called by getStringArrayFunction(). If the function returns a string, that will override the default output. (In fact, the function isn't required to return a string; it can be used to run custom code instead. We'll cover variations on that in a later tutorial.)

MyGame.createAsset({
  class: "Scenery",
  name: "imaginary object",
  place: { in: "Play Room" },
  list_in_room: false,  
  print_this_for_all_verb_failures: function () {    
    if(MyGame.getPlayer().$is("wearing", "fairy crown")){
      return `{We} try to {verb} the imaginary object, but nothing comes of it. `;
    } else {
      return `{We} don't know of any way to {verb} the imaginary object. `;
    }
  },
});

And the results:

> eat imaginary object You don't know of any way to eat the imaginary object. > wear fairy crown You slide on the fairy crown. > eat imaginary object You try to eat the imaginary object, but nothing comes of it.

Override an entire turn's output

prependOutput, appendOutput, overrideOutput

These methods can be used to revise the output for the current turn. They're independent of any particular objects or verbs and can be called from any code block during the game loop. The following examples show the methods being called from doNestThisToThat and doUnnestThisFromThat, which are verb reaction hooks that you may not be familiar with yet. Don't worry! We'll cover those in later tutorials. We'll focus on the act of riding a bicycle that the player is not yet on (as opposed to riding a bicycle that the player is on already). Here's our basic setup and a default output.

MyGame.createAsset({
  class: "Player",
  name: "Hero",
)};
MyGame.createAsset({
  class: "Bicycle",
  name: "bicycle",
)};
> ride bike You mount the bicycle and take a seat.

Override an entire turn's output prependOutput

Prepend a string before the beginning of the turn's default output.

MyGame.createAsset({
  class: "Player",
  name: "Hero",
  doNestThisToThat: {
    bicycle: function () {
      MyGame.prependOutput("You wobble a bit, climbing on. ");
    },
  },
)};
> ride bike You wobble a bit, climbing on. You mount the bicycle and take a seat.

Override an entire turn's output appendOutput

Append a string to the end of the turn's default output.

MyGame.createAsset({
  class: "Player",
  name: "Hero",
  doUnnestThisFromThat: {
    bicycle: function () {
      MyGame.prependOutput("You nearly fall down in the process, but manage to make a clumsy recovery. ");
    },
  },
)};
> get off bike You get off of the bicycle. You nearly fall down in the process, but manage to make a clumsy recovery.

Override an entire turn's output overrideOutput

Override's the current turn's default output.

MyGame.createAsset({
  class: "Player",
  name: "Hero",
  doNestThisToThat: {
    bicycle: function () {
      MyGame.overrideOutput("You climb onto the pristine white banana seat of the classic 1964 Schwinn
        Stingray, and dream of the adventurous treks to come. ");
    },
  },
  doUnnestThisFromThat: {
    bicycle: function () {
      MyGame.overrideOutput("You climb, sadly, wistfully off the 1964 Schwinn Stingray, the classic bike
        of your dreams, with a vow to ride again soon. ");
    },
  },
)};
> ride bike You climb onto the pristine white banana seat of the classic 1964 Schwinn Stingray, and dream of the adventurous treks to come. > get off bike You climb, sadly, wistfully off the 1964 Schwinn Stingray, the classic bike of your dreams, with a vow to ride again soon.