MV Guidance on custom escape characters?

Hey All,

I'm wondering if someone might know of any good tutorials or explanations on calling code from the "Show Text" event using custom escape characters, or maybe even just a very basic example someone new might have a little more luck making sense of? I'm trying to do some custom work around the message window and I feel like I'm just struggling to grasp how the process works. If anyone has something they could share I would appreciate it

Thanks!
The official text codes are defined in the help file and visible on the help-text when hovering over the entry window of the event command.

Anything beyond that depends on the plugin that adds more codes - there is no common definition at all, and there have been cases where different messaging plugins/scripts have used the same code for different functions.
The two most commonly used messaging systems are the ones from Yanfly (available for both VXAce and MV) and Modern Algebra (Available for VX and VXA, don't know if it was ported to MV as well)
It depends on exactly which aspect you're having trouble with. The default methods handling those things are all found in rpg_windows.js:
  • convertEscapeCharacters - en-masse replacements before displaying text, e.g. \v[x] or \n[x];
  • processEscapeCharacter - called when it would otherwise be displayed, e.g. \. or \$; and
  • obtainEscapeParam - retrieves the x value in things like \v[x].
If you want to handle non-numeric escape params, you'll need to code your own string-matching routine, for which I'd suggest regular expressions:

I have plugins for MV/MZ that'll let you define your own escape/text codes and code the effects without having to worry about what methods to alias or how to match the codes from the text. Here are links in case you're interested:
There are limitations, though, e.g. I don't think you can create a "change text display rate" code with that alone, since it requires additional infrastructure: a variable delay between successive characters in messages.
@Andar, @caethyril

Thank you both for your responses. I think that I may be lacking some of the basic lingo to convey exactly what my aim is, but it looks like it's the processEscapeCharacter function is where I need to be working and (at least, I think) the obtainEscapeParam function is the key piece that I wasn't realizing.

caethyril, the notes in your plugin gives actually gave me a much better understand of what I'm looking at. I want to figure out how to do this myself, but it was still super helpful. Thank you for sharing!
If you want to add your own escape character that will call some code, you can do it as follows.

Override this method in the MV code:

Window_Base.prototype.obtainEscapeCode = function(textState) {
textState.index++;
var regExp = /^[\%\$\.\|\^!><\{\}\\]|^[A-Z]+/i;
var arr = regExp.exec(textState.text.slice(textState.index));
if (arr) {
textState.index += arr[0].length;
return arr[0].toUpperCase();
} else {
return '';
}
};

Put your custom escape code where the red is. I used \%, but you can choose your own as long as it's different from the ones already in the game.


Then change:

Window_Base.prototype.processEscapeCharacter = function(code, textState) {
switch (code) {
case 'C':
this.changeTextColor(this.textColor(this.obtainEscapeParam(textState)));
break;
case 'I':
this.processDrawIcon(this.obtainEscapeParam(textState), textState);
break;
case '{':
this.makeFontBigger();
break;
case '}':
this.makeFontSmaller();
break;
case '%':
this.myCustomMethod();

break;
}
};


And then, of course, write whatever you want it to do.


Window_Base.prototype.myCustomMethod = function()
{
//your code here
};
If you want to add your own escape character that will call some code, you can do it as follows.

Override this method in the MV code:

Window_Base.prototype.obtainEscapeCode = function(textState) {
textState.index++;
var regExp = /^[\%\$\.\|\^!><\{\}\\]|^[A-Z]+/i;
var arr = regExp.exec(textState.text.slice(textState.index));
if (arr) {
textState.index += arr[0].length;
return arr[0].toUpperCase();
} else {
return '';
}
};

Put your custom escape code where the red is. I used \%, but you can choose your own as long as it's different from the ones already in the game.


Then change:

Window_Base.prototype.processEscapeCharacter = function(code, textState) {
switch (code) {
case 'C':
this.changeTextColor(this.textColor(this.obtainEscapeParam(textState)));
break;
case 'I':
this.processDrawIcon(this.obtainEscapeParam(textState), textState);
break;
case '{':
this.makeFontBigger();
break;
case '}':
this.makeFontSmaller();
break;
case '%':
this.myCustomMethod();

break;
}
};


And then, of course, write whatever you want it to do.


Window_Base.prototype.myCustomMethod = function()
{
//your code here
};

@GE_Peter, thank you for this! One question, if I may. I didn't touch the first method obtainEscapeCode, because I didn't understand what it did and I was able to build my plugin without out, but with your example it appears that what this is doing is specifying the actual escape characters the engine will recognize (The "\" that we're used to in RPG Maker, or in your example "%"). Am I following that right?
Yep, that crazy complicated line of code is a Regular Expression that searches through the text for specific characters. You'll see the escape characters in there that we know and love from the 'show text' command in the engine, like \$ for open gold window and \| for wait one second.

You can put your own escape character in there, "\(whatever)", then when the game sees it in a text window, it will run whatever code you put in the switch statement for the case of your character in processEscapeCharacter.

So yes, the "\" is the one we're used to in RPG Maker.
Clarification: \% in @GE_Peter's regular expression matches a single % character (\\% would match "\%", but backslashes are substituted for the \x1b escape character prior to processing, so that's a moot point).

As you can see, obtainEscapeCode is a method used to match and return the escape code, e.g. the $ of \$, or the I of \I[x].

I would caution against overwriting the default methods. It is better, for the sake of compatibility, to patch/alias methods (see example below). Also note that it is generally advisable to put overrides/aliases/etc in a separate file rather than to edit the core scripts directly, since that allows edits to be tracked, attributed, updated, and transferred independently of the core scripts.

Here's an example that will (I think, haven't tested) add a custom text code \ABC[x], which causes a message to appear stating the value of x when it processes:
Spoiler: Example plugin code
JavaScript:
(function() {  // for scope
  var myTextCode = function(value) { alert('Your number is ' + value); };
  var _Window_Base_processEscapeCharacter = Window_Base.prototype.processEscapeCharacter;
  Window_Base.prototype.processEscapeCharacter = function(code, textState) {
    if (code === 'ABC') {  // custom mid-message text code
      myTextCode(this.obtainEscapeParam(textState));  // do the stuff
    } else {
      _Window_Base_processEscapeCharacter.apply(this, arguments);  // invoke default method
    }
  };
})();
The default obtainEscapeCode method handles any sequence of pure A~Z letters (upper- or lower-case), so if you stick to that then you shouldn't need to change anything there. Also note that, as I mentioned previously, the default obtainEscapeParam method only checks for a single integer, so you'll need to code your own check (or exercise some ingenuity) for anything more complex.

Original thread: https://forums.rpgmakerweb.com/threads/guidance-on-custom-escape-characters.127299/