Supporting USE - ThePix/QuestJS GitHub Wiki
The USE command is a bit of an odd one being so generic. The player might try it to do anything, and it does not really have an obvious place in the documentation.
It has broadly two forms:
USE object
USE object1 WITH/ON object2
One object
You can just give the item a "use" attribute; either a string or a function that will be sent a dictionary with the keys "char" and "item", and should return true
or false
.
use:function(options) {
msg("{nv:char:cannot:true} work out how to use it.", options)
return false
},
Alternatively, you can tell Quest to use another command by giving it a "useDefaultsTo" attribute, a function that will be sent the character doing it, and should return a string, the name of the command to use. This example is from the WEARABLE template (and "useDefaultsTo" is really designed for templates); if the character is the player, we want USE to default to the "Wear" command,
useDefaultsTo:function(char) {
return char === player ? 'Wear' : 'NpcWear'
},
Two objects
When there are two objects involved, it obviously gets more complicated. For one thing, we have to decide who is going to take responsibility.
If the first object has a "useWith" attribute, it gets the responsibility. If not, Quest will look at whether the second object has a "withUse" attribute, and if so, it will go with that.
If both of them fail, it will look at "useWithDefaultsTo" and "withUseDefaultsTo", which work analogousky to "useDefaultsTo" discussed before.
Both "useWith" and "withUse" must be functions, taking two parameters, the character and the other object, and should return true
or false
.
This example is for a key that be be used for winding something up - anything with a "dowindup" attribute in fact.
useWith:function(char, obj) {
if (obj.dowindup) {
return obj.dowindup()
}
else {
return falsemsg("Mandy wonders what she could use the clock key for. Besides winding up the clock, that is.")
}
},