Pre-release
Adventure.js Docs Downloads
Score: 0 Moves: 0
Tutorial explaining how to create exits in Adventurejs. tutorial, Create an Exit

Get Started:Create an Exit

Exits are created like any other asset, using createAsset().

MyGame.createAsset({
  class: "Exit",
  direction: "east",
  place: { in:  "Central Chamber" },
  destination: "East Chamber",
})

HOWEVER. There's a shortcut for creating exits. If you're following from How to Create a Room, you'll recall that we have two rooms with no connections between them. Here are those rooms again, with some exit shortcuts.

// game constructor block...
// player constructor block...
MyGame.createAsset({
  class: "Room",
  name: "Dungeon Antechamber",
  description: "You're in a dingy dungeon antechamber. Every surface seems to be made of pitted cobblestones. ",
  exits: 
  {
    north: "Central Chamber",
    south: "You feel the lure of the tiny patch of sky that's still visible through the entrance... but you don't want to leave without the treasure! ",
  },
});
MyGame.createAsset({
  class: "Room",
  name: "Central Chamber",
  description: "You're in the central chamber of the dungeon, a round room with a high stone ceiling. Paths in several directions are blocked by fallen stones. ",
  exits:
  {
    south: "Dungeon Antechamber",
  }
});

Let's take a look at the exits object we added to the Dungeon Antechamber. We have a couple of things to point out here. First, let's look at this line:

north: "Central Chamber",

Here we're saying that dungeon_antechamber.exits.north leads to the Central Chamber. When the game launches, the Dungeon Antechamber's initialization method can get enough information from this context to create this exit.

#TMI About dungeon_antechamber

You might have noticed where we referred to dungeon_antechamber.exits.north, though we know that the name of the room is "Dungeon Antechamber". That's because Adventurejs serializes all asset names into IDs for internal use, making them lower case and replacing spaces with underscores. You can refer to assets by name in any methods that are designed for authors, but if you end up digging deeper, you may bumpb into methods that use IDs, so it's useful to be aware how IDs differ from names.

Now let's look at the next line. Hey, that's not a room name!

south: "You feel the lure of the tiny patch of sky that's still visible through the entrance... but you don't want to leave without the treasure! ",

Using the shortcut method, you can create denied exits. Which is to say that if a player tries to go that way, the game will print this text instead of taking them anywhere.

Before we put it all together, we're going to update the descriptions of the two rooms. When a player enters a room, the game tells them what directions are available to travel in, but it doesn't describe the exits. We want to include exit descriptions as part of the room description.

// game constructor block...
// player constructor block...
MyGame.createAsset({
  class: "Room",
  name: "Dungeon Antechamber",
  description: "You're in a dingy dungeon antechamber. Every surface seems to be made of pitted cobblestones. The path descends into darkness to the north. The air here is dank and oppressive, leavened only by a slight draft of fresh air at your back. ",
  exits: 
  {
    north: "Central Chamber",
    south: "You feel the lure of the tiny patch of sky that's still visible through the entrance... but you don't want to leave without the treasure! ",
  },
});
MyGame.createAsset({
  class: "Room",
  name: "Central Chamber",
  description: "You're in the central chamber of the dungeon, a round room with a high stone ceiling. Paths in several directions are blocked by fallen stones. The path to the south is a touch brighter and emits a hint of sweeter air. ",
  exits:
  {
    south: "Dungeon Antechamber",
  }
});

Ok, let's put it all together. Get in there and try entering "go north" or "go south".

// game constructor block...
// player constructor block...
MyGame.createAsset({
  class: "Room",
  name: "Dungeon Antechamber",
  description: "You're in a dingy dungeon antechamber. Every surface seems to be made of pitted cobblestones. The path descends into darkness to the north. The air here is dank and oppressive, leavened only by a slight draft of fresh air at your back. ",
  exits: 
  {
    north: "Central Chamber",
    south: "You feel the lure of the tiny patch of sky that's still visible through the entrance... but you don't want to leave without the treasure! ",
  },
});
MyGame.createAsset({
  class: "Room",
  name: "Central Chamber",
  description: "You're in the central chamber of the dungeon, a round room with a high stone ceiling. Paths in several directions are blocked by fallen stones. The path to the south is a touch brighter and emits a hint of sweeter air. ",
  exits:
  {
    south: "Dungeon Antechamber",
  }
});

Before we move on, we have a couple of things to point out about exits.

First, exits are singular, meaning that they only exist in one room. In order to connect two rooms to each other, each one has to have an exit connected to the other. One advantage of this is that it lets you make one-way exits, or exits that go to different places each time they're used.

Second, exits are only connectors, they have no "physical" presence, meaning no doors. Exits only exist to move characters between places. Think of them like big floating wormholes. Of course, exits can be linked to doors, and we'll cover that in the next tutorial.

NEXT: Create a Door