Tutorial 6: More items - ThePix/QuestJS GitHub Wiki

... Continued from Items and rooms again.

A simple torch

The player has a problem that if the basement is dark, she cannot see the light switch to turn it on. We will give her a torch. This illustrates that we can combine templates; the torch is both TAKEABLE and SWITCHABLE.

createItem("torch", TAKEABLE(), SWITCHABLE(false, 'providing light'), {
  loc:"lounge",
  examine:"A small black torch.",
  synonyms:["flashlight"],
  lightSource:function() {
    return this.switchedon ? world.LIGHT_FULL : world.LIGHT_NONE
  },
})

The SWITCHABLE template takes two parameters. The first determines if the item starts turned on, as we used fore the light switch. The second a string to add to the name when it is turned on.

The "lightSource" function we saw in the basement. Items by default return world.LIGHT_NONE; we want the torch to return world.LIGHT_FULL when it is turned on.

You should now be able to grab the torch, open the trapdoor, go down to the basement and turn on the light.

A torch that runs down

Let us suppose the torch has limited power, and will die after so many turns. We need a turnscript! A turnscript is something that can exist on its own, but they can also be attached to other things. We will attach ours to the torch. In fact, we add five new attributes: three functions and two numbers.

createItem("torch", TAKEABLE(), SWITCHABLE(false, 'providing light'), {
  loc:"lounge",
  examine:"A small red torch.",
  synonyms:["flashlight"],
  lightSource:function() {
    return this.switchedon ? world.LIGHT_FULL : world.LIGHT_NONE
  },
  eventPeriod:1,
  eventIsActive:function() {
    return this.switchedon
  },
  eventScript:function() {
    this.power--;
    if (this.power === 2) {
      msg("The torch flickers.")
    }
    if (this.power < 0) {
      msg("The torch flickers and dies.{once: Perhaps there is a charger in the garage?}");
      this.doSwitchoff()
    }
  },
  testSwitchOn () {
    if (this.power < 0) {
      msg("The torch is dead.")
      return false
    }
    return true
  },
  power:3,
})

JavaScript uses && instead of and in conditional statments (and || instead of or, ! instead or not).

The += operator adds the value on the right to that on the left. x += 4 is actually the same as x = x + 4, but less typing. You can use ++ to increase a number by 1 and -- to decrease it by 1,

The "eventIsActive" function is how Quest decides if the turnscript should run. We want it to run if the torch is on, so we just return the value of the "switchedon" attribute. The "eventPeriod" attribute makes the script run every turn (if it was 3, it would run every three turns). Set it to false - or just leave it out - and the event becomes a one-off; it will fire once only.

The "eventScript" function is the actual turnscript. The first line reduces the power by 1. Then we check the value and react accordingly. If the power is getting low, we give a warning. If it gets to zero, a message and turn off the torch.

The "power" number records how much power is left. I have set that to 3 just so we can test the torch easily.

The "testSwitchOn" function is used by the SWITCHABLE template before turning on an item. If it returns false, the item cannot be turned on (broken, out of power, whatever).

It is worth emphasising that the torch has only two attribute that change, "switchedon" and "power". However, it has various functions that return values depending on those attributes.

Continues in Locks...