-
Notifications
You must be signed in to change notification settings - Fork 17
NPCs ‐ Give
If you have NPCs you are likely to want to let the player give items to them. The NPC says he will help if the player gets the MacGuffin; the player gets it, now she has to give it to him.
We can see giving an item as a two step process. The first step involves the giver, and is handled by "getAgreementGive" (and "canManipulate"), which is discussed elsewhere.
What we are concerned with here is the second step, the character receiving the item and perhaps reacting, or refusing it. By default, NPCs will refuse any item the player gives him, but the player will accept any item given to her. But you can change that on an item-by-item basis for each character.
This is handled by two attributes, "receiveItems" and "receiveItemsFailMsg". The former is an array Quest will look though for a suitable response. The latter is a string or function that is used if the search fails.
The array, "receiveItems", is made up of dictionaries, each containing an "item" attribute that is a specific item or a "test" that is a function (or both). This entry will be used if the item matches or the test returns true (or both, if both are present). If there is a match, the function, "script" is run, and the search terminates. It will be passed a dictionary with attributes "item" and "npc".
You can have is print a message as well as or instead of running a script using the "msg" attribute, but usually the script is required as you need some code to handle transferring the item. The best way to do the transfer is with util.giveItem
, which should be passed the parameters and will just sort it out for you.
Here is an example for a carrot-obsessed rabbit called Lara. She will grudgingly accept the knife, but rejects anything else that is not a carrot. Note that carrots are clones, so we check the name starts with "carrot" in the second entry.
receiveItems:[
{
item:w.knife,
script:function(p) {
msg("'A knife?' says Lara. 'I guess I could use that... for something?'")
util.giveItem(p)
}
},
{
test:function(options) {
return options.item.name.startsWith('carrot')
},
script:function(options) {
msg("'A carrot!' says Lara with delight, before stuffing it in her mouth. 'So, do you have any more?'")
// move first to ensure reactions like afterMove will run, then delete as it gets eaten
util.giveItem(p)
delete options.item.loc
}
},
{
msg:"'That's not a carrot,' Lara points out.",
failed:true,
},
],
Note the last entry, which has no test, so catches everything else. The item is not transferred to Lara, so no script used. It is flagged as a failed command.
The second example is for a character who will accept anything, but has a special response for the book.
receiveItems:[
{
item:"book",
script:function(p) {
msg("'Oh!' says Kyle. 'Is this a book?'")
util.giveItem(p)
return true
}
},
{
test:function() { return true },
script:function(p) {
msg("{multi}Done.", p)
util.giveItem(p)
return true
}
},
],
The functions here use a single parameter, which is a dictionary with the following name-value pairs (they are named to be consistent with other parts of Quest 6; the last two are for "moveToFrom"):
Name | Description |
---|---|
item | The item being given |
char | The giver (usually the player) |
npc | The receiver |
toLoc | The receiver's name |
fromLoc | The giver's name |
NOTE: The "item" attribute can be set to either the item itself, as done for the first example, or to the item name, as in the second example. If you use the item itself, the item must be defined earlier (either higher up the file or in a file that loads earlier). If it is not, "item" will be set to undefined
and it will match things it should not and general confusion will result!
You can also code your own give function for each character, by giving him his own "handleGiveTo". Note that this is an attribute of the character receiving the item, and will be passed a dictionary with the attributes described above.
It is up to you to check the giver has the item, is willing and able to hand it over, and to handle the receiver dealing with it.
It should return a Boolean indicating success or failure.
Tutorial
- First steps
- Rooms and Exits
- Items
- Templates
- Items and rooms again
- More items
- Locks
- Commands
- Complex mechanisms
- Uploading
QuestJS Basics
- General
- Settings
- Attributes for items
- Attributes for rooms
- Attributes for exits
- Naming Items and Rooms
- Restrictions, Messages and Reactions
- Creating objects on the fly
- String Functions
- Random Functions
- Array/List Functions
- The
respond
function - Other Functions
The Text Processor
Commands
- Introduction
- Basic commands (from the tutorial)
- Complex commands
- Example of creating a command (implementing SHOOT GUN AT HENRY)
- More on commands
- Shortcut for commands
- Modifying existing commands
- Custom parser types
- Note on command results
- Meta-Commands
- Neutral language (including alternatives to "you")
- The parser
- Command matching
- Vari-verbs (for verbs that are almost synonyms)
Templates for Items
- Introduction
- Takeable
- Openable
- Container and surface
- Locks and keys
- Wearable
- Furniture
- Button and Switch
- Readable
- Edible
- Vessel (handling liquids)
- Components
- Countable
- Consultable
- Rope
- Construction
- Backscene (walls, etc.)
- Merchandise (including how to create a shop)
- Shiftable (can be pushed from one room to another)
See also:
- Custom templates (and alternatives)
Handing NPCs
- Introduction
- Attributes
- Allowing the player to give commands
- Conversations
- Simple TALK TO
- SAY
- ASK and TELL
- Dynamic conversations with TALK TO
- TALK and DISCUSS
- Following an agenda
- Reactions
- Giving
- Followers
- Visibility
- Changing the player point-of-view
The User Experience (UI)
The main screen
- Basics
- Printing Text Functions
- Special Text Effects
- Output effects (including pausing)
- Hyperlinks
- User Input
The Side Panes
Multi-media (sounds, images, maps, etc.)
- Images
- Sounds
- Youtube Video (Contribution by KV)
- Adding a map
- Node-based maps
- Image-based maps
- Hex maps
- Adding a playing board
- Roulette!... in a grid
Dialogue boxes
- Character Creation
- Other example dialogs [See also "User Input"]
Other Elements
- Toolbar (status bar across the top)
- Custom UI Elements
Role-playing Games
- Introduction
- Getting started
- Items
- Characters (and Monsters!)
- Spawning Monsters and Items)
- Systema Naturae
- Who, When and How NPCs Attack
- Attributes for characters
- Attacking and guarding
- Communicating monsters
- Skills and Spells
- Limiting Magic
- Effects
- The Attack Object
- [Extra utility functions](https://github.com/ThePix/QuestJS/wiki/RPG-Library-%E2%80%90-Extra Functions)
- Randomly Generated Dungeon
- Quests for Quest
- User Interface
Web Basics
- HTML (the basic elements of a web page)
- CSS (how to style web pages)
- SVG (scalable vector graphics)
- Colours
- JavaScript
- Regular Expressions
How-to
Time
- Events (and Turnscripts)
- Date and Time (including custom calendars)
- Timed Events (i.e., real time, not game time)
Items
- Phone a Friend
- Using the USE verb
- Display Verbs
- Change Listeners
- Ensembles (grouping items)
- How to spit
Locations
- Large, open areas
- Region,s with sky, walls, etc.
- Dynamic Room Descriptions
- Transit system (lifts/elevators, buses, trains, simple vehicles)
- Rooms split into multiple locations
- Create rooms on the fly
- Handling weather
Exits
- Alternative Directions (eg, port and starboard)
- Destinations, Not Directions
Meta
- Customise Help
- Provide hints
- Include Achievements
- Add comments to your code
-
End The Game (
io.finish
)
Meta: About The Whole Game
- Translate from Quest 5
- Authoring Several Games at Once
- Chaining Several Games Together
- Competition Entry
- Walk-throughs
- Unit testing
- Debugging (trouble-shooting)
Releasing Your Game
Reference
- The Language File
- List of settings
- Scope
- The Output Queue
- Security
- Implementation notes (initialisation order, data structures)
- Files
- Code guidelines
- Save/load
- UNDO
- The editor
- The Cloak of Darkness
- Versions
- Quest 6 or QuestJS
- The other Folders
- Choose your own adventure