The Text Processor - ThePix/QuestJS GitHub Wiki

The text processor allows authors to put codes in text that will get translated by Quest, according to the directive given and the conditions when it is printed.

To use the text processor, you can simply add a directive in curly braces in any text that gets displayed. In this simple example, a room description is set to say that room smells only the first time the text is printed:

This is a small dungeon.{once: There is a bad smell in here.}
                        ------------------------------------
                         ^^^^

The text processor takes the entire underlined section. The directive here is "once". Note that it is followed by a colon. Some directives take several parts; these will always be split up with colons. In this case there is just the one extra part. If this is the first time the message is show, that will be added to the text. Otherwise it will not.

The basic text processor is very similar to Quest 5, and nearly all Quest 5 text will work without change, the only exception being the "eval" directive, and its short cut using the equals sign. In fact the example above is identical to the one in the Quest 5 documentation. However, there are a lot more options available is Quest 6!

The Quest 6 text processor introduces parameters. That is, when you print a string, you can also pass along a set of parameters and the text processor will use them to decide what should be printed.

This is used to a huge extent in all the built in messages. For example, when the player takes an item:

"{nv:char:take:true} {nm:item:the}."

The first directive says I want a noun-verb, using the object "char", and the verb "take", capitalised. The second says I want the name of the object "item", with the definite article. This same text can be used for some thugs getting a football, for you getting Mike the Micro-robot or for Lara taking some trousers.

Notes

Invoking the text processor

The msg function automatically uses the text processor, but sometimes you want to invoke it without msg. Use the processText function.

const s = processText("{nv:char:take:true} {nm:item:the}.", {char:player, item:w.Mike})

Line breaks

When using the msg function (and related functions), you can use a vertical bar, |, to tell Quest to start a new line. The text processor is then used with each line. What this means is that text processor directives cannot straddle line breaks.

In the first examples below the directive stops before the line break, and then starts again after it; this is the right way to do it. In the second line, the directive continues right across the line break - it will not work properly.

"Here is my first line with {i:italics.}|{Still italics} on second line."
"Here is my first line with {i:italics.|Still italics} on second line."

Note that the processText function will not convert vertical bars to new lines.

Circumventing the text processor

The msg function automatically uses the text processor, if you want to not use it, use the rawPrint function.

If you want to use the text processor, but want the special characters in you text, there are escape codes you can put in the text, which the text processor will ignore, but the msg command will convert to the character when printing to screen.

Character Code
{ @@@lcurly@@@
} @@@rcurly@@@
: @@@colon@@@
] @@@rsquare@@@
[ @@@lsquare@@@

A Note About String Interpolation

JavaScript has its own equivalent of the text processor, and there are times you may find it easier to use that. To use string interpolation, you have to use backticks to surround your string, rather than single or double quotes. You can then put your JavaScript expression inside curly braces with a dollar sign at the start.

These two are the same:

"The answer is " + myValue + "."
`The answer is ${myValue).`

You can mix both types. In this example, the description only includes the clock if the clock's display attribute is equal to the DSPY_SCENERY. We need to insert that value into the text processor command.

createRoom("kitchen", {
  desc:`A clean room{if:clock:display:${DSPY_SCENERY}:, a clock hanging on the wall}.`,
...

The clock is scenery until picked up. At that point it stops being part of the room description.

Note that JavaScript string interpolation will happen when the string is created; the text processor does its magic when the string is displayed. We need to use the text processor in that last example because the string is created at the start of the game, and we need it to change depending on the game state at the time it is used. DSPY_SCENERY, however, is a constant, so we can safely have that set in stone when the game starts.