NPCs: Followers - ThePix/QuestJS GitHub Wiki

Sometimes we want an NPC to follow either another NPC, or more often the player.

NOTE: If you want to do this in an agenda, see that page. It uses the same attributes, so is fully compatible, but the commands are in the agenda so are different.

Following with a command

The player can ask an NPC to follow her with [NPC], FOLLOW ME or TELL [NPC] TO FOLLOW ME. To stop the NPC following, use [NPC], STOP FOLLOWING ME or TELL [NPC] TO WAIT HERE, etc.

You can use "getAgreement" or "getAgreementFollow" to have the NPC decide whether or not to follow the player; this is discussed more here.

If you are using dynamic conversations, there are some example topics on that page.

Handling in code

Behind the scenes, the commands use these functions too.

To have an NPC follow someone, use the setLeader function. In this example, Lara will follow Kyle.

  w.Lara.setLeader(w.Kyle)

Lara will get her "leaderName" attribute set to "Kyle", and Kyle will have "Lara" added to his "followers" attribute (which is an array of strings).

You can use "getFollowers" on the leader to get a list of follower objects, rather than the names.

Use the "setLeader" function without a paramter to stop an NPC following. Unsetting a leader when there is no leader set will have no effect, so can be used if you are unsure. It is also safe to set a new leader when there is already one set.

  w.Lara.setLeader()

You can give an NPC a "testFollowTo" function attribute, which can be used to stop the NPC going in certain rooms. There is an example here.

Note that if you want an NPC to follow the player from the start of the game, you should put the code inside settings.setup to ensure player has been set.

settings.setup = function() {
  w.magpie.setLeader(player)
}

If an NPC will be following the player for much of the game, it is a good idea to mix up the text a bit. The best way to do that is with the NPC's "movingMsg" function attribute.

For this example, I have assigned an "enviro" attribute to every location, and I am using that to determine what the magpie does. A location can also have its own "magpieFollowMsg" to further individualise it.

  followMsgs:{
    town:'The magpie perches on a {random:window ledge:gutter:ridgetile}.', 
    forest_road:'The magpie hops along the road behind you.', 
    forest:'The magpie jumps onto a branch, and looks at you quizzically.', 
    marsh:'The magpe follows, circling for a moment before alighting on a relatively dry patch of ground.', 
    cave:'The magpie follows you, jumping from rock to rock.',
  },  
  movingMsg:function(exit, isFollowing) {
    if (isFollowing && currentLocation.magpieFollowMsg) {
      currentLocation.magpieFollowMsg()
    }
    else if (isFollowing) {
      msg(this.followMsgs[currentLocation.enviro])
    }
    else if (exit.msgNPC) {
      exit.msgNPC(char)
    }
    else {
      lang.npc_leaving_msg(this, exit)
      lang.npc_entering_msg(this, exit)
    }    
  },

The tavern then has this:

  magpieFollowMsg() {
    msg("The magpie flies into tavern too, and perches on a beam.")
    if (!this.magpieflag) {
      this.magpieflag = true
      msg("'Get that filthy bird out of here,' says the innkeeper.")
      msg("'Don't look at me,' you say. 'Stupid thing follows me round everywhere.' You wave your arms at it, and get it to fly to another beam.")
      msg("'If it craps in here...' mutters the innkeeper.")
    }    
  },