Changing the Point of View - ThePix/QuestJS GitHub Wiki

In most games, the user controls a single character throughout the game, but occasionally, you might want the user to be able to control more than one. Perhaps in the first part, the user plays the part of the villain, and in the second part, the hero who has to thwart him. Perhaps the user can swap between any of the four members of the the gang, and each has his or her own special abilities.

There is nothing special about an NPC the user can control - indeed, the only real issue here is that you must not use the PLAYER template because when the user swaps to another character, the old one will need the infrastructure of a proper NPC.

So now the player just looks like any other NPC, perhaps like this:

createItem("Buddy", NPC(false), { 
  loc:"lounge",
  money:10,
  properName:true,
  examine:'An orangutan!',
})

Before the game starts, however, we need to set who the player is. This needs to go after the above code and also after the code for the location the player starts in. If these are in different files, you may need to check which gets loaded first.

util.changePOV(w.Buddy)

This is the same function used to change the player character; suppose we want to change to the character Lara. You can use the name as a string instead of the object if you prefer.

util.changePOV("Lara")

Giving and receiving

Note that the PLAYER template will cause the player object to accept anything given to him or her. As we are not using that, you will have to deal with that yourself. If you want all the characters the player can be to accept anything they are given, just given them all this:

  receiveItems:[
    {
      test:function() { return true },
      f:function(options) { 
        msg(lang.done_msg, options)
        options.item.loc = this.name
        return true
      }
    },
  ],