Pre-release
Adventure.js Docs Downloads
Score: 0 Moves: 0
Tutorial explaining how to handle scoring with Adventurejs. tutorial, score, scoring

Get Started:Set Scores

Score events can be defined and called by the author using the Scorecard object. Event names and score amounts are arbitrary and can be set to any values you prefer, and you can have as many or as few as you like.

MyGame.scorecard.set({
  score_events: {
    "examine cobblestones": 1,
    "examine pitted cobblestone": 1,
    "open pitted cobblestone": 5,
    "take iron key": 1,
    "unlock door": 5,
    "enter central chamber": 1,
    "open chest": 1,
    "take statuette": 10,
  }
});

Once score events are set, they can be called like this.

MyGame.scorecard.completeEvent('take statuette');

Here's where it gets a little more involved. Since score events are arbitrary, not automatic, we have to place them at logical points inside the code we've already written. We set eight score events for this example game. We won't show them all in detail but we'll work through several of them here.

"take statuette"

Here's the code block for the jade statuette we established in Create Some Treasure. Previously we subscribed it to the verb take by using the shortcut dov:{take:true}. Now we're going to expand on that verb subscription. See the highlighted section below.

MyGame.createAsset({
  class: "Thing",
  name: "jade statuette",
  synonyms: ["statue","dragon"],
  adjectives: ["green"],
  dov: { 
    drop: true, 
    take: {
      on_first_success: function(){
        MyGame.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" },
});

"examine cobblestones"

As we did for the statuette and its take subscription, we'll have to flesh out the cobblestones object's examine subscription. Because look is a distinct verb from examine, we'll set look.on_first_success to forward examine.on_first_success. Score events are only counted once, so it's ok if they are placed in such a way that may be called multiple times.

MyGame.createAsset({
  class: "Collection",
  name: "cobblestones",
  place: { in: "Dungeon Antechamber" },
  collection: "pitted cobblestone, weathered cobblestone, cracked cobblestone",
  synonyms: [ ],
  is: { listed_in_room: false, known: true },
  dov: { 
    examine: {
      on_first_success: function(){
        MyGame.scorecard.completeEvent('examine cobblestones');
      },
    },
    look: {
      on_first_success: function(){
        return this.dov.examine.on_first_success();
      },
    },
  },
  description: "The entire room seems to be laid in cobblestones. Not just the floor, but also the walls and ceiling. The stones around the frame of the wood planked door draw your eye. Three in particular stand out: a cracked cobblestone, a weathered cobblestone, and a pitted cobblestone. ",
});

"examine pitted cobblestone" and "open pitted cobblestone"

These two score events are both associated with verb subscriptions on the same pitted cobblestone. We'll need to revise both verb subscriptions.

MyGame.createAsset({
  class: "Thing",
  name: "pitted cobblestone",
  aspects: { in: { with_assets: [ "pitted iron key" ], }, },
  dov: { 
    close: { with_nothing: true }, 
    open: { 
      with_nothing: true, 
      on_first_success: function(){
        MyGame.scorecard.completeEvent('open pitted cobblestone');
      },
    }, 
    examine: {
      on_first_success: function(){
        MyGame.scorecard.completeEvent('examine pitted cobblestone');
      },
    },
    look: {
      on_first_success: function(){
        return this.dov.examine.on_first_success();
      },
    },
  },
  is: { closed: true, known: true, listed_in_room: false },
  description: "The surface of this cobblestone seems especially pitted. The stone appears to be seamed, suggesting that it must be hinged. Currently it is $( pitted cobblestone is| open or| closed ). ",
  article: "a",
  adjectives: [ ],
  place: { in: "Dungeon Antechamber" },
});

"enter central chamber"

This last block is a little different because we want to call the score event when the player enters a room, rather than in response to a particular verb. This means that we need to use a verb reaction instead of a verb action. This is a new thing that we haven't touched on yet, which will be covered in Start Scripting. In short, verb reactions are happen in response to verb actions. For example, any time a verb causes one thing to move into another thing, doMoveThisToThat is called.

MyGame01.createAsset({
  class: "Player",
  name: "Mighty Hero",
  is: { active: true, },
  doMoveThisToThat: 
  {
    "Central Chamber": function() 
    {
      MyGame.scorecard.completeEvent('enter central chamber');
    }
  },
});

NEXT: Debugging