Other Functions - ThePix/QuestJS GitHub Wiki
io.finish()
Run this function to end the game. The side pane will be be removed (if used), and replaced by vertical text saying "Game over". The text input will also be disabled. You will probably want to add your own text to make clear the game is over. For example (
is an HTML code for a hard space):
msg("You stand on the giant spider's corpse and raise your fist into the air, triumphant!")
blankLine()
msg("T H E E N D", {}, 'centred')
io.finish()
If transcript recording is turned on, a message will be given, with a link that can be clicked to show the transcript.
You can pass true
to the function, and then Quest will give the player the opportunity to click links to UNDO or RESTART. This is especially useful if the player has just died!
lang.npc_moving_msg(npc, exit)
Prints a message saying the NPC is moving via the given exit. If the player is in the room the exit starts from, it will say the NPC is leaving that room, if the player is in the room it goes to, the message will say the NPC is entering the room.
If the NPC is going either to or from with a "visibleFrom" attribute, and this list of strings includes the name of the room the player is in, the message will be given, with the original room's "visibleFromPrefix" prepended. This means the movements of an NPC can be observed from different locations. This example has a window; when the player is in the dining room, she will see NPCs going into and leaving the garden.
createRoom("garden", {
desc:"Very overgrown. The garden opens onto a road to the west, whilst the conservatory is east. There is a hook on the wall.",
east:new Exit("conservatory"),
visibleFrom:["dining_room"],
visibleFromPrefix:"Through the window you can see",
})
doOnce(object, flag)
If you want something to just happen once, you can use this function together with if
:
if (doOnce(obj)) obj.singleTimeFunction()
Behind the scenes, the function will add an attribute to the object and return true
the first time it is called for that object. Subsequent calls will find the attribute is there, and return false
. This ensures the function is only every run once for that object.
That is fine if there is just one such function for a particular object. If you have several, you will need to provide names for the flags, to ensure each is handled separately.
if (doOnce(obj, 'myCustomFlag')) obj.singleTimeFunction()
log(anything)
Sends the given thing to the console. This is just a shortcut for console.log
; it can be sent a string, a number, an array, an object, whatever.
runCmd(s)
You can use this to run a command as though it was typed by the user. It will get echoed to the screen, and then sent to parser.parse
.
printOrRun(char, item, attname, options)
This function is designed to be used within a command. It is common for a command to relate to an item, and to access an attribute of that item, which might be a string or a function. For examine, the TAKE command accesses the "take" attribute of the item.
With this in mind, then, the function has these parameters:
- char: the character doing the command; usually the player, but the player might ask an NPC to take the hat, for example
- item: the item, obviously
- attName: the name of the attribute
- options: an optional dictionary of further parameters (the function will set "char" to the character and "item" to the item unless these are already set)
If the attribute is a string, the string is printed, using options as text processor parameters. If the attribute is a function, the function is called, using options as the only parameter.
NOTE: If you know the attribute is a function, it is probably better to just call the function yourself.
item.attName()
item.attName(options)
util.getObj(name)
Will get the named object. If the name variable is actually an object itself, just returns the object. f the object is not found, or has no "name" attribute an exception is thrown.
This is useful for functions that could be sent an object or the name of an object.
util.clamp(num, min, max)
Returns the given number, num, but restricted to lie between min and max; i.e., if number is less than min, then min will be returned instead.
Scope functions
function scopeReachable
Returns a list of reachable items.
function scopeHereListed
Returns a list of items at the current location and not scenery (i.e., listed if the user types LOOK).
function scopeHeldBy(chr, situation)
Returns a list of items held (or worn) by the NPC.
function scopeNpcHere(ignoreDark), scopeAllNpcHere(ignoreDark)
Returns a list of NPCs who are here. The former excludes those flagged as scenery, the latter does not.
scopeBy(func)
The getObs
function gets an array of objects (items and rooms) for which the given function returns true
. This simple example gets a list of items for which the "candidate" attribute is true (or indeed any non-falsy value).
const candidates = scopeBy(function(o) {
return o.candidate
})
JavaScript has a shorthand that is useful for simple functions. This example is exactly the same as the above:
const candidates = scopeBy(o => o.candidate)
Other Scope-like functions
Use "getContents()" on a container to get a list of items in it.
const contents = w.chest.getContents()
For an NPC or the player you can use "getHolding()", "getWearing()" and "getCarrying()".
const inHerHands = w.Lara.getHolding()
const yourClothes = player.getWearing()
const listAll = w.orc_boss.getCarrying()