-
Notifications
You must be signed in to change notification settings - Fork 17
Custom Text Processor Directives
You can add your own text processor directives using the tp.addDirective
function. This needs a name and a function. The function should take a string array as a parameter (the input text, split on colons) and the parameters you passed to msg
, and return a string.
Here is an example:
tp.addDirective("fancy", function(arr, params) {
return '<span style="font-family:Montserrat">' + arr.join(":") + "</span>";
});
Note that the parameter arr
is joined with colons, as this is how it was broken up. You may want to use the first one to set a value, as in this example:
tp.addDirective("fancy2", function(arr, params) {
const font = arr.shift();
return '<span style="font-family:' + font + '">' + arr.join(":") + "</span>";
});
This example looks at an attribute on the player; if true
, the second section is shown, otherwise the third is.
tp.addDirective("lore", function(arr, params) {
return player.status === 'Lore' ? arr[0] : arr[1]
});
This example is similar, but calls a function.
tp.addDirective("cloakHere", function(arr, params) {
return cloakHere() ? arr[0] : arr[1]
});
You can use this to do anything. It could just insert a standard piece of text, similar to a macro. This is currently the way to do the equivalent of "eval" in the Quest 5 text processor (JavaScript can handle eval
but it is considered insecure, so I am trying to avoid it).
tp.addDirective("title", function(arr, params) {
return settings.title;
});
Custom directives offer a way to keep location and item descriptions as strings, but still dynamic. The "examine" attribute could then be as simple as:
examine:"The hourglass has two connected glass bulbs.{hourglass}"
All the complicated stuff, in this case how much time has passed in effect, is done in the text processor directive.
tp.addDirective("hourglass", function(arr, params) {
return "The upper bulb is about " + Math.round(this.state) + "% full."
}
Whether this is actually easier is debatable...
The standard text processor has a "once" command that ensures a section of text is only seen one time, but is limited to be used in just one string. What if you want the text to be only seen once, but it could be embedded in several strings? Let us suppose a section of your game has some introductory text, but the player might first arrive in any of four different locations. One way to handle this is with a custom directive.
Here is an example. We need to save a flag on one of the rooms, it does not matter which. If the flag is already true
, just return an empty string. Otherwise, set the flag, and return the introductory text.
tp.addDirective("greenhouseIntro", function(arr, params) {
if (w.greenhouse_west.seenFlag) return ''
w.greenhouse_west.seenFlag = true
return 'Just for a moment, you think you are outside - all you can see is trees, bushes and plants. But no, above there is a roof, and walls can be seen all around you; this is a greenhouse. '
})
Each description then needs the directive at the start:
desc:"{greenhouseIntro}You are stood on a catwalk that skirts the north side of the great greenhouse. Even from here, the huge oak tree towers over you.",
It is bad practice to put anything important in text that will only be seen once, as users can miss it, and then have no opportunity to see it again.
Here is a "title" text processor that will give the contents of the NPC's "title attribute, or just "Mr" of "Ms" if not set. It illustrates how to use paramters, using the in-built "_findObject" function. It also tests that the found object is an NPC.
tp.addDirective("title", function(arr, params) {
const npc = tp._findObject(arr[0], params, arr)
if (!npc) errormsg("Failed to find object '" + name + "' in text processor 'title' (" + params.tpOriginalString + ")")
if (npc.title) return npc.title
return npc.isFemale ? 'Ms' : 'Mr'
})
We can use a text processor inside a text processor! This example gives the title and the name, using the above function and the built-in "nameFunction".
tp.text_processors.tnm = function(arr, params) {
return tp.text_processors.title(arr, params) + ' ' + tp.nameFunction(arr, params, false)
}
In addition to any parameters you send, params will also contain:
- tpOriginalString: The original string
- tpFirstTime: Will be true the first time this string has been done.
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