NPCs: Attributes - ThePix/QuestJS GitHub Wiki

Standard attributes

Most of the standard attributes also apply to NPCs. See here for more.

Moving a character

For items you should generally use their "moveToFrom" function attribute, but for characters, including the player, "moveChar" is used.

If you do need to just set "loc" for the player - and there are times - you should call a couple of functions after to make sure things are updated.

player.loc = 'ballroom'
world.update()
world.enterRoom(exit)

The world.enterRoom function runs the various on entry functions and gives the room description. The exit parameter is required if there is any possibility of the player having followers.

moveChar(exit)

Moves the given character (which could be the player) to the given location. Characters are different to items for a number of reasons. If a hat is moving, it is because something else is doing it, and we can rely on that thing to tell the user about it. NPCs move on their own, so we may want to tell the user about, they may have followers, they might be carry an item that does something special when moved, etc.

Note that this moves the player silently - though it may trigger reactions that are not silent. To keep the player informed, you should also call "movingMsg".

Occasionally you will want to move the player to a location with no convenient exit. The solution is to create a disposable exit on the fly.

  west:new Exit('lounge', {
    simpleUse:function(char) {
      if (w.lounge.rift_open) {
        char.moveChar(new Exit('interdimensional_rift', this)
        return true
      }
      return util.defaultSimpleExitUse(char, this)
    }
  },

Note that I am using this, which refers to the original exit. The new exit will get all the attributes of the original exit except the name, so this sorts out all the attributes set in the background. Otherwise, you need to set "dir" to a string, and "origin" to a location.

char.moveChar(new Exit('interdimensional_rift', {dir:'west', origin:w[char.loc]})

movingMsg(exit)

This is a function on the NPC; you can give the NPC a custom function to give it specific messages when it moves.

You should call this before you use "moveChar" in your own code to ensure the user is told the character is moving. Quest will work out if the player should be able to see it, and report accordingly.

This is generally best called before the move, so reactions to the move are reported after it happened.

Generally not used for the player character - you can just use a standard msg command.

Setting loc

Sometimes it is better to just set "loc". Generally I advise again for moving items, as you cannot be sure it is using the "loc" attribute, but NPCs do, so we are safe wth regards to that.

Be aware that none of the reactions will trigger if you do it this way. It is good for when you are introducing the character to the game for the first time, or removing her entirely, but not so good when she is walking around, and should be using the standard exits.

For the player, there is some housekeeping you will need to do. Once the "loc" is set, you need to call world.update to tell quest to update various things, and then world.enterRoom to trigger the room enterng scripts and print a description of the new room.

  msg("You head down the road to...")
  player.loc = 'battlefield'
  world.update()
  world.enterRoom(exit)

Note that world.enterRoom expected to be given an exit. If the player cannot possibly have any followers when this happens, this can be omitted.

Attributes to Customise an NPC

You can replace these with your own values to make each NPC unique.

askOptions, tellOptions

Arrays for ASK/ABOUT and TELL/ABOUT. See here for more.

agenda, followers, suspended

Used by the agenda system. See here for more.

reactions

Used by the reactions system. See here for more.

canManipulate, canMove, canPosture, canTalk

These are called before certain commands. If they return true, the NPC can do it, otherwise, they should print a message and then return false. This allows you to have the NPC incapacitated in various ways.

getAgreement(cmdName, item)

Used to determine whether the NPC will do as the player asks. If they return true, the NPC is willing to do it, otherwise, they should print a message and then return false. This will be sent the name of the command and the item, if applicable, so you can finely control what the NPC will or will not do.

Utility Functions.

Generally you should not change these; they are there to make it easier for you to do things.

getHolding(), getWearing(), getWearingVisible(), getTopics()

Each will return a list of objects for this NPC.

isHere()

Returns true if the player is in the same location as the NPC.

msg(s)

Prints the given string only if the player is in the same location as the NPC.

multiMsg(array)

Prints the given array of strings, in order, one string per call of the function (the first time the function is called, the first string is print, etc.). Once it gets to the end of the list, it will repeat the last item.

As with msg, only printed if the player is in the same location as the NPC (if the player is not present, the list does not iterate, so no entries are missed).

Note that this uses the first entry to create a counter attribute (stripping out all non-letters). This could cause a naming collision, but should be fine as long as the first entry is long and unique for this NPC.

setLeader(npc)

Assign a leader to this NPC. The NPC will become part of a group following the leader's agenda.

pause()

Stops the NPC following his agenda (or the group following his leader's agenda) for a single turn.

moveWithDescription(dest)

Move the NPC to the destination. If the player is at the destination or the original location, she will see a message that the NPC has moved.

getOuterWearable(slot)

Returns the outer-most (i.e. visible) garment at the given slot.