Pre-release
Adventure.js Docs Downloads
Score: 0 Moves: 0
Tutorial explaining how to create a key in Adventurejs. tutorial, create a key

Get Started:Create a Key

We create a key with good old createAsset(). However, to talk about keys we need to introduce some new concepts. These are things you'll come to use extensively, but don't worry about absorbing everything right now. We'll cover this stuff again and again.

MyGame.createAsset({
  class: "Key",
  name: "beaten iron key",
  iov: { unlock: { with_assets: ['wood planked door'], }, },
  description: "It's a thick, heavy key made of beaten iron. ",
  article: "a",
  adjectives: [ ],
  place: { in: "Dungeon Antechamber" },
});

Check out this line.

iov: { unlock: { with_assets: ['wood planked door'], }, },

iov is short for "indirect object of verb", and this means the key can be used as an indirect object of the verb unlock. This is what we call a verb subscription. Recall that an asset is any object in the game world, whether it's a key or a lantern or a sword or a spell or an NPC. When an asset is created it's just an inert lump of properties that can't be interacted with. An asset must be subscribed to verbs in order for those verbs to act upon it.

Most built-in asset classes – including Key and also other things like Chest, Door, Exit, etc – come pre-subscribed to numerous verbs, so you don't have to explicitly subscribe new instances to every verb. When we create a key, it's already subscribed as an indirect object to lock and unlock. And the doors we made are already subscribed as direct objects of lock and unlock. But in order to associate key with door, we need to pass in some additional information to identify the relationship between them.

Give it a try! Get in there and "take key" then "unlock door with key".

// game constructor block...
// player constructor block...
// dungeon antechamber constructor block...
// north exit constructor block...
// wood planked door constructor block...
// central chamber constructor block...
// south exit constructor block...
// iron bound door constructor block...
MyGame.createAsset({
  class: "Key",
  name: "beaten iron key",
  iov: { unlock: { with_assets: ['wood planked door'], }, },
  description: "It's a thick, heavy key made of beaten iron. ",
  article: "a",
  adjectives: [ ],
  place: { in: "Dungeon Antechamber" },
});
#TMI Setting iov vs dov

You might ask, since we specified that the key is an indirect object of unlock by setting MyGame.$("beaten iron key").iov.unlock.with_assets['wood planked door'], do we also need to specify that the door is a direct object of unlock by setting MyGame.$("wood_planked_door").dov.unlock.with_assets['beaten iron key']?

It's an excellent question, and the answer is that we do not. Any time you set a dov or iov relationship, Adventurejs will automatically set the necessary properties on the reciprocal asset, even including setting a verb subscription on the other asset if it hasn't got one.

Now we have a door and a key to unlock it. But our key is just sitting there on the floor! Let's add a simple puzzle to make it a bit more interesting.

NEXT: Create a Puzzle