MV ProcessEscapeCharacter as processNormalCharacter

Hello all,

I got a funny challenge which I always get, "NaN" of undefined from textState.

The problem comes from processEscapeCharacter inside message, which I took into a cool effect
for my base plugin, it works, the problem only, the escape character output that line
in 1 sweep instead of letter by letter by default.

here is the code I use for it. (untested in MZ, but functional in MV)
as for the record, you may place it in your own plugin if credits is given!

for my process character code, message part for the updates, unsure how to add
processNormalCharacter to use in combination with processEscapeCharacter
JavaScript:
    SDJB_Base._WB_processEscapeCharacter = Window_Base.prototype.processEscapeCharacter;
        Window_Base.prototype.processEscapeCharacter = function(code, textState) {
            switch (code) {
                // \WAIT[n]
                case "WAIT":
                    let delay = this.obtainEscapeParam(textState);
                    this.startWait(delay);
                    break;
                // \UV[variableID, SystemTime, Custom]
                case "UV":
                    this.updateDynamicVariables(textState);
                    break;
                case "SM": case "STOP":
                    if (this instanceof Window_Message) {
                        this._stopMessage = true;
                        Input.keyRepeatWait = Infinity;
                        Input.keyRepeatInterval = Infinity;
                    }
                    break;
                case "???":
                    // New Function
                    break;
                default:
                    SDJB_Base._WB_processEscapeCharacter.call(this, code, textState);
                    break;
            }
        };

    Window_Base.prototype.updateDynamicVariables = function(textState) {
        const paramMatch = /^\[(.+?)\]/.exec(textState.text.slice(textState.index));
            if (!paramMatch) return;
            const params = paramMatch[1].split(",").map(s => s.trim().replace(/["']/g, ""));
            const varId = parseInt(params[0]);
            const mode = params[1] || null;
            const custom = params[2] || null;
            if (!this._dynamicVars) this._dynamicVars = [];
            this._dynamicVars.push({
                id: varId,
                x: textState.x,
                y: textState.y,
                width: 0,
                systemTime: mode,
                customFormat: custom,
                lastValue: null,
                active: true
            });
            //this._dynamicVars[this._dynamicVars.length - 1].active = true;
            textState.index += paramMatch[0].length;
    };

    Window_Base.prototype.updateLiveVariables = function() {
        for (let spot of this._dynamicVars) {
            if (!spot.active) continue;
            let value = spot.systemTime ? getTime(spot.systemTime, spot.customFormat) : $gameVariables.value(spot.id);
            if (String(value) !== spot.lastValue) {
                let w = this.textWidth(value) + 4;
                this.contents.clearRect(spot.x, spot.y, w, this.lineHeight());
                this.drawText(value, spot.x, spot.y, w);
                spot.width = this.textWidth(value);
                spot.lastValue = String(value);
            }
        }
    };

//update inside messages for the variables or time.
    SDJB_Base._WindowMessage_update = Window_Message.prototype.update;
        Window_Message.prototype.update = function() {
            if (this._stopMessage) {
                if (Input.isTriggered("ok") || Input.isTriggered("cancel") || TouchInput.isTriggered()) {
                    //=> restore RepeatWait and RepeatInterval
                    Input.keyRepeatWait = OriginalKRW;
                    Input.keyRepeatInterval = OriginalKRI;
                    this._stopMessage = false;
                } else if (this._dynamicVars && this.isOpen()) {
                    this.updateLiveVariables();
                }
            }
            SDJB_Base._WindowMessage_update.call(this);
                if (!this._stopMessage && this._dynamicVars && this.isOpen()) {
                    this.updateLiveVariables();
                }
        };

    SDJB_Base._WindowMessage_terminateMessage = Window_Message.prototype.terminateMessage;
        Window_Message.prototype.terminateMessage = function() {
            this._dynamicVars = null;
            this._stopMessage = false;
            Input.keyRepeatWait = OriginalKRW;
            Input.keyRepeatInterval = OriginalKRI;
            SDJB_Base._WindowMessage_terminateMessage.call(this);
        };

while the update works, how can I add so it prints letter by letter effect (default) or if it
changes to edit the effect, using that?

I try to loop over the processEscapeCharacter and textState.index, but that gave the error "NaN"
which conflict during update, but I don't know if there is a work around nicely?

so my question is: How can I use letter by letter or any effect to print out the UV processEscapeCharacter,
instead that it prints out in 1 go? (if its even possible).
if anyone know an way, even a guess I can try, please tell so, a for loop fails,
but I'm open to any suggestions that make this processEscapeCharacter to work.

if using \UV[4, "format", "DD-MM-YYYY hour:min:sec") it prints it out instantly.
for \uv[4] => single variable whatever value you live update here doesn't matter much
as its barely visible, but a long value or string values if in words, prints it out directly.

I like this effect as processNormalCharacter if able (specially if it can work with with and without
TSR_TextColorAddon (which I use as well), so if you have a clean way or a method, please
share it, as it will be inside my base.

as the UV is a really neat feature.
still looking, if you know how or love a challenge,
give it a shot as I don't get it done either error or still instant.

so bumping this as I love too if it can use processNormalCharacter when typing it out.
still looking for a way if possible please 🙂
Bumping this as I still look a way for this as it looks better if its shows
letter by letter instead show it all in once.
Can't help you, but maybe there'd be a better chance of getting help if you just asked about the custom text code, instead of showing all that code? Or did you test that the custom text code works without all that other code (I mean if you just use clearRect and drawText to show fixed value)? Or did I understand wrong what the whole problem was?

I tried making a custom text code with VS Message Core where it uses clearRect and drawText and it worked properly with the remaining text appearing one letter at a time after the text code made the "edit". And then I edited the message window update method to use clearRect and drawText variable that increased in parallel event and that seemed to work properly too. But yeah, that was in MZ and other than that, too difficult for me.
Can't help you, but maybe there'd be a better chance of getting help if you just asked about the custom text code, instead of showing all that code? Or did you test that the custom text code works without all that other code (I mean if you just use clearRect and drawText to show fixed value)? Or did I understand wrong what the whole problem was?

I tried making a custom text code with VS Message Core where it uses clearRect and drawText and it worked properly with the remaining text appearing one letter at a time after the text code made the "edit". And then I edited the message window update method to use clearRect and drawText variable that increased in parallel event and that seemed to work properly too. But yeah, that was in MZ and other than that, too difficult for me.
I use MV, not MZ, but the code can call time in a variable, so it also directly update that variable
with variable++ on counting while the message window is open or variable-- in parallel process.

but if I show a long text as my systemTime, it throw the entire text in 1 time and everything behind it.
instead doing it like the letter by letter like the default text does.

instant text or using the escape to show the line was normal, but this process the update variable.
but in 1x which I hope there is a way to make it letter by letter as default way does too.

the question is just, how to do that.
Ah okay, I guess I misunderstood your point. And maybe I still partly do, because doesn't the base code print those variable text codes one letter at a time? Or do you mean that you want an updating variable that replaces text already drawn to appear one letter at a time? But yeah, maybe I'm too tired and couldn't focus on everything you wrote 😀 Anyway, good luck!
doesn't the base code print those variable text codes one letter at a time?
no it doesn't, like a sample:

text: this is a long text, print out as letter by letter:
text this text \uv[3] show it in 1x.

UV to update has a text: timeSystem("HH-MM-SS") etc

so it print out: Time: 18-22-21 and count up, the above = incorrect sample, but it can
add the hous, minuts, seconds, month, year, in any way you want it to and replace it.

so you can let it show: hour: 20, minutes: 44, seconds: 10, year 2025.

but it does print in 1x, not letter by letter, which I hope if there is a way for it.
if not, than I have to keep it how it is, as if I loop through, I need to let the index
know where it is called, but than it shows: hour: NaN, minutes: NaN etc.
something I don't need.

that's why this question if there is something that could be done.

1-5 letters in 1x doesn't matter much, but if its long, its very visible.
You can use convertEscapeCharacter to replace before the message shows.

Alternatively, via processEscapeCharacter(code, textState) (mid-message replacement), you could manually insert your string into the text property of textState, e.g. textState.text.splice(textState.index, 0, "TEST");.

Edit: 😴 I keep forgetting that JS does not define String, i.e. an array of characters, as an Array of characters. You'd have to do something like this instead:

textState.text = textState.text.slice(0, textState.index) + "TEST" + textState.text.slice(textState.index);
You can use convertEscapeCharacter to replace before the message shows.

Alternatively, via processEscapeCharacter(code, textState) (mid-message replacement), you could manually insert your string into the text property of textState, e.g. textState.text.splice(textState.index, 0, "TEST");.
this is issue as I don't know when or where it will be placed, and if I do it through convertEscapeCharacter,
how would I go to update the variable after that correctly as I know the convert doesn't update
automatically like the process would.

I did try the splice, but than it gets overlapped or NaN output because it update incorrectly.

that is the trouble I get, but if it could be done by convertEscapeCharacter and update afterwards
directly in the way it does in the process, I love to know the way for it.

as this is a tricky thing so far I know.
(In case you missed it, I edited my post: in JS, strings do not have a native splice method.)

NaN, i.e. "not a number", suggests to me that there may be a mistake in the code you are using to generate the replacement text.

The convertEscapeCharacters method literally just uses String#replace to swap out text codes for their replacements, before the message starts displaying. I'm not sure if that would be suitable for your use case. E.g. the \V[x], \N[x], and \P[x] text codes are all "convert" type. It's simpler because there's no textState to deal with. E.g. (untested):
JavaScript:
void (function(alias) {
  Window_Message.prototype.convertEscapeCharacters = function(text) {
    text = alias.apply(this, arguments);
    text = text.replace(/\x1bUV/gi, function() { return "TEST"; });
    return text;
  };
})(Window_Message.prototype.convertEscapeCharacters);
Obviously you would use your own replacement function there.
(In case you missed it, I edited my post: in JS, strings do not have a native splice method.)

NaN, i.e. "not a number", suggests to me that there may be a mistake in the code you are using to generate the replacement text.

The convertEscapeCharacters method literally just uses String#replace to swap out text codes for their replacements, before the message starts displaying. I'm not sure if that would be suitable for your use case. E.g. the \V[x], \N[x], and \P[x] text codes are all "convert" type. It's simpler because there's no textState to deal with. E.g. (untested):
JavaScript:
void (function(alias) {
  Window_Message.prototype.convertEscapeCharacters = function(text) {
    text = alias.apply(this, arguments);
    text = text.replace(/\x1bUV/gi, function() { return "TEST"; });
    return text;
  };
})(Window_Message.prototype.convertEscapeCharacters);
Obviously you would use your own replacement function there.
I will test the code out to se what it does, but the UV = UpdateVariable method.
so parallel with variable update every 30 frames show up in the message window correctly.

I try to do the same with the time system I have as well, so if your method works when printing
out whatever the variable holds, its a nice thing to use.

the NaN error is only if I loop over the string and can crash, the way it currently is coded works.
but I test the code when I got time or tomorrow, hopefully it works as I really want this to work
with the processCharacter so far I understand it, it updates where convertCharacters does not.
(In case you missed it, I edited my post: in JS, strings do not have a native splice method.)

NaN, i.e. "not a number", suggests to me that there may be a mistake in the code you are using to generate the replacement text.

The convertEscapeCharacters method literally just uses String#replace to swap out text codes for their replacements, before the message starts displaying. I'm not sure if that would be suitable for your use case. E.g. the \V[x], \N[x], and \P[x] text codes are all "convert" type. It's simpler because there's no textState to deal with. E.g. (untested):
JavaScript:
void (function(alias) {
  Window_Message.prototype.convertEscapeCharacters = function(text) {
    text = alias.apply(this, arguments);
    text = text.replace(/\x1bUV/gi, function() { return "TEST"; });
    return text;
  };
})(Window_Message.prototype.convertEscapeCharacters);
Obviously you would use your own replacement function there.
testing out the code, but while it's window base and use that, the output is the same (static)
so I need to return to processCharacter somehow and it may work the way I need too.

but if I try to return, it throws errors of slice issue, so I don't know how return that value
to process character if its even possible. so it the values inside UV can update as normal.

but I get the feeling it won't be easy or not even possible.
the output is the same (static)
If you want the text to update after it has been drawn, that means you will, at minimum, have to redraw that specific text whenever the value updates. That will require additional structure, e.g.
  1. Measure/log the bounding area (x, y, width, height) of the text that needs redrawing.
  2. Patch e.g. the setValue or onChange method of Game_Variables to request a redraw when appropriate.
  3. Patch e.g. the update method of Window_Base to redraw specified areas when a redraw is requested.
Alternatively, e.g. if a change of value can change the width of the draw area, you could redraw the entire contents (up to the current point) when the variable changes.
if I try to return, it throws errors of slice issue
If you press F8 or F12 during playtest, that should show the dev tools window. Its Console tab should have more detailed error information, helping you (and, if you share a screenshot here, other people) to figure out what's going wrong.

Good luck~ 👋
the error if I do loops, but I did some rewire from convertEscapeCharacter => to processCharacters.
from there to updateMessage => always become instant.

so for variables structure, I didn't test that, but all ways for update becomes instant, as it seems
impossible to get it done correctly, so I rather live with it as it may be above my head to restructure
or redraw to get the default typing effect, as I'm of options.

the effect update works, so I stick with it for now, maybe when I got another brainwave, but I keep
the Game_Variables in mind in the hope it may work.

Original thread: https://forums.rpgmakerweb.com/threads/processescapecharacter-as-processnormalcharacter.180402/