MV How to add complex messages to the drawText function

Hey everyone, I'm dabbling a bit of advanced script calling and need some help.

Basically I'm making a evented skill tree and when the player touches an event in the skill tree map, it shows a custom text windows that's suppose to show information about the skill, it's description as it is in the database, name, etc.

And I have the message showing and closing part working fine. The thing is:
  • The drawText function doesn't allow any text codes as show message does, and I was hoping I could use Yanfly's Evaltext to show the skill name and other things. I also wanted to change text size and color too.
  • I need more space to write skill descriptions and other things.
Are there any solutions to this?
My javascript knowledge very limited, so begginer friendly solutions would work wonders 😅

Here's the script call I'm using atm (with an evaltext that doesn't work):
Code:
var width = Graphics.boxWidth - 400;
SceneManager._scene._myCustomWindow = new Window_Base(350, 25, width, 600);
SceneManager._scene.addChild(SceneManager._scene._myCustomWindow)
SceneManager._scene._myCustomWindow.drawText("\evalText<<$dataSkills[8].name>>", -25, 0, width, "center");
Try using drawTextEx; it processes text codes. You don't get an alignment parameter with that, though. Also it may not work with *custom* text codes, just giving you fair warning.

...either that or just put $dataSkills[8].name as the text. There's nothing in that that needs evaluated.

Also since you're not referencing the window afterwards, you can give it a shorter identifier for convenience: "const myWin" and then using "myWin" to refer to it will work just as well as SceneManager._scene._myCustomWindow will.
Try using drawTextEx; it processes text codes. You don't get an alignment parameter with that, though. Also it may not work with *custom* text codes, just giving you fair warning.

...either that or just put $dataSkills[8].name as the text. There's nothing in that that needs evaluated.

Also since you're not referencing the window afterwards, you can give it a shorter identifier for convenience: "const myWin" and then using "myWin" to refer to it will work just as well as SceneManager._scene._myCustomWindow will.
Hey Trihan, thanks for the reply I tried your solution but I think I didn't apply the text properly, it doesn't show anything
Code:
var width = Graphics.boxWidth - 400;
myWin = new Window_Base(350, 25, width, 570);
SceneManager._scene.addChild(myWin)
myWin.drawTextEx;("\C[2]Spring", -25, 0, width);

1749166614535.png
Here's the script call I'm using atm (with an evaltext that doesn't work):
Code:
var width = Graphics.boxWidth - 400;
SceneManager._scene._myCustomWindow = new Window_Base(350, 25, width, 600);
SceneManager._scene.addChild(SceneManager._scene._myCustomWindow)
SceneManager._scene._myCustomWindow.drawText("\evalText<<$dataSkills[8].name>>", -25, 0, width, "center");

Wouldn't this work just fine?
Code:
var width = Graphics.boxWidth - 400;
SceneManager._scene._myCustomWindow = new Window_Base(350, 25, width, 600);
SceneManager._scene.addChild(SceneManager._scene._myCustomWindow)
SceneManager._scene._myCustomWindow.drawText($dataSkills[8].name, -25, 0, width, "center");
Hey Trihan, thanks for the reply I tried your solution but I think I didn't apply the text properly, it doesn't show anything
Code:
var width = Graphics.boxWidth - 400;
myWin = new Window_Base(350, 25, width, 570);
SceneManager._scene.addChild(myWin)
myWin.drawTextEx;("\C[2]Spring", -25, 0, width);

View attachment 342245
Does it work with drawText with what I suggested to you there?
Wouldn't this work just fine?
Code:
var width = Graphics.boxWidth - 400;
SceneManager._scene._myCustomWindow = new Window_Base(350, 25, width, 600);
SceneManager._scene.addChild(SceneManager._scene._myCustomWindow)
SceneManager._scene._myCustomWindow.drawText($dataSkills[8].name, -25, 0, width, "center");
Thank you! This does work, I just tested it, however I still have the issue where I wanted to use text codes to have colorful text and also be able to have long texts/skill descriptions etc

@Trihan you mean the myWin label? Yes it does! Here's how it looks:


Code:
var width = Graphics.boxWidth - 400;
myWin = new Window_Base(350, 25, width, 570);
SceneManager._scene.addChild(myWin)
myWin.drawText($dataSkills[8].name, -25, 0, width, "center");
Depending on exactly what you want, there is also the option of making your text colourful without using text codes. Unless you're aiming for something like rainbow text, you can just add this line before your drawText call:

Code:
SceneManager._scene._myCustomWindow.changeTextColor("#abcdef")
Thank you! This does work, I just tested it, however I still have the issue where I wanted to use text codes to have colorful text and also be able to have long texts/skill descriptions etc

@Trihan you mean the myWin label? Yes it does! Here's how it looks:


Code:
var width = Graphics.boxWidth - 400;
myWin = new Window_Base(350, 25, width, 570);
SceneManager._scene.addChild(myWin)
myWin.drawText($dataSkills[8].name, -25, 0, width, "center");
Also with the drawTextEx example, have you tried removing the semicolon you put before the brackets in the function call? 😛 In my post, that was just a grammatical construct to separate my clauses, not something you were meant to paste.
Hey everyone sorry it took me so long to reply! I got a bit busy and I also decided to dig through the rpg_window js file to see if I could find something that could help me.

Also with the drawTextEx example, have you tried removing the semicolon you put before the brackets in the function call? 😛 In my post, that was just a grammatical construct to separate my clauses, not something you were meant to paste.
Depending on exactly what you want, there is also the option of making your text colourful without using text codes. Unless you're aiming for something like rainbow text, you can just add this line before your drawText call:

Code:
SceneManager._scene._myCustomWindow.changeTextColor("#abcdef")
SolarFlare thank you for the tip! It works perfectly when using the drawText function, however, It doesn't when It comes to drawTextEx (that I used properly this time, thank you for the clarification Trihan 😂) as you can see:

Code:
var width = Graphics.boxWidth - 400;
myWin = new Window_Base(350, 25, width, 570);
SceneManager._scene.addChild(myWin)
myWin.changeTextColor("#dea987");
myWin.drawTextEx("ABCDEFGHIJKLMNOPQRSTUVWXYZ\nABC", 0, 0);
myWin.resetTextColor();

Spoiler: drawTextEx
1749510210606.png
Spoiler: drawText
1749510281517.png

Now my issues kind of still remain? It feels like I already made progress and learned new things but am still stuck in place 😅
  • I now know I can use drawTextEx to use escape codes, however, drawTextEx doesn't process any $ commands, so I can't draw any skill descriptions AND have line breaks using \n.
  • Even if I could, lines don't break automatically, I did find this thread that has a script that line breaks automatically, I tried saving it as a plugin and using the .drawTextAutoWrap function, but I must be using it incorrectly since It doesn't seem to work.
  • Let's say I decide to try and manually break lines, I can't seem to find any functions that draws text on the next line without using escape codes.
I would really appreciate some suggestions to fix these problems 😢, thank you all so much for the replies and support so far
however, It doesn't when It comes to drawTextEx (that I used properly this time, thank you for the clarification Trihan 😂)
Well, I wouldn't expect it to work with drawTextEx. If you call drawTextEx, it probably calls changeTextColor and overrides whatever you set.

I now know I can use drawTextEx to use escape codes, however, drawTextEx doesn't process any $ commands, so I can't draw any skill descriptions AND have line breaks using \n.
I'm not sure what you mean by $ commands.

Let's say I decide to try and manually break lines, I can't seem to find any functions that draws text on the next line without using escape codes.
When you call drawText, you give it the location you want to draw the text at. For example, here:

JavaScript:
SceneManager._scene._myCustomWindow.drawText($dataSkills[8].name, -25, 0, width, "center");

You've told it to draw the text at coordinates (-25,0). (That's very strange, by the way – normally you shouldn't have negative coordinates here. But I'll set that aside for now…)

Now, if you have two lines of text to draw instead of one, you can do this:

JavaScript:
SceneManager._scene._myCustomWindow.drawText(line1, -25, 0, width, "center");
SceneManager._scene._myCustomWindow.drawText(line2, -25, this.lineHeight(), width, "center");

And for three lines:

JavaScript:
SceneManager._scene._myCustomWindow.drawText(line1, -25, 0, width, "center");
SceneManager._scene._myCustomWindow.drawText(line2, -25, this.lineHeight(), width, "center");
SceneManager._scene._myCustomWindow.drawText(line3, -25, this.lineHeight() * 2, width, "center");

Or if you have an unknown number of lines:

JavaScript:
var lines = ["line1", "line2", "line3", "line4", "line5"]; // however many lines you need
for(var i = 0; i < lines.length; i++) {
  SceneManager._scene._myCustomWindow.drawText(lines[i], -25, this.lineHeight() * i, width, "center");
}
Well, I wouldn't expect it to work with drawTextEx. If you call drawTextEx, it probably calls changeTextColor and overrides whatever you set.

I'm not sure what you mean by $ commands.

When you call drawText, you give it the location you want to draw the text at. For example, here:

JavaScript:
SceneManager._scene._myCustomWindow.drawText($dataSkills[8].name, -25, 0, width, "center");

You've told it to draw the text at coordinates (-25,0). (That's very strange, by the way – normally you shouldn't have negative coordinates here. But I'll set that aside for now…)

Now, if you have two lines of text to draw instead of one, you can do this:

JavaScript:
SceneManager._scene._myCustomWindow.drawText(line1, -25, 0, width, "center");
SceneManager._scene._myCustomWindow.drawText(line2, -25, this.lineHeight(), width, "center");

And for three lines:

JavaScript:
SceneManager._scene._myCustomWindow.drawText(line1, -25, 0, width, "center");
SceneManager._scene._myCustomWindow.drawText(line2, -25, this.lineHeight(), width, "center");
SceneManager._scene._myCustomWindow.drawText(line3, -25, this.lineHeight() * 2, width, "center");

Or if you have an unknown number of lines:

JavaScript:
var lines = ["line1", "line2", "line3", "line4", "line5"]; // however many lines you need
for(var i = 0; i < lines.length; i++) {
  SceneManager._scene._myCustomWindow.drawText(lines[i], -25, this.lineHeight() * i, width, "center");
}
Thanks for the quick reply SolarFlare! 👋and by $ commands I mean things like: $dataSkills[8].name or $dataSkills[8].description.

I did try to use your scripts but for some reason it only shows line1 ?

Did I miss a step?
Code:
var width = Graphics.boxWidth - 400;
myWin = new Window_Base(350, 25, width, 570);
SceneManager._scene.addChild(myWin)
myWin.drawText(line1, -25, 0, width, "center");
myWin.drawText(line2, -25, this.lineHeight(), width, "center");
myWin.drawText(line3, -25, this.lineHeight() * 2, width, "center");



By the by I did manage to find a way to show $dataSkills[8].name in DrawTextEx using a variable like so:

Code:
var width = Graphics.boxWidth - 400;
myWin = new Window_Base(350, 25, width, 570);
var skillName = myWin.drawText($dataSkills[8].name, -25, 0, width, "center")
SceneManager._scene.addChild(myWin)
myWin.drawTextEx(skillName, 0, 0);

Spoiler: $ in variable
1749513842075.png
Thanks for the quick reply SolarFlare! 👋and by $ commands I mean things like: $dataSkills[8].name or $dataSkills[8].description.

I did try to use your scripts but for some reason it only shows line1 ?

Did I miss a step?
Code:
var width = Graphics.boxWidth - 400;
myWin = new Window_Base(350, 25, width, 570);
SceneManager._scene.addChild(myWin)
myWin.drawText(line1, -25, 0, width, "center");
myWin.drawText(line2, -25, this.lineHeight(), width, "center");
myWin.drawText(line3, -25, this.lineHeight() * 2, width, "center");


Spoiler: Screenshot

By the by I did manage to find a way to show $dataSkills[8].name in DrawTextEx using a variable like so:

Code:
var width = Graphics.boxWidth - 400;
myWin = new Window_Base(350, 25, width, 570);
var skillName = myWin.drawText($dataSkills[8].name, -25, 0, width, "center")
SceneManager._scene.addChild(myWin)
myWin.drawTextEx(skillName, 0, 0);

Spoiler: $ in variable
You're setting skillName to the return value of the call to drawText. It doesn't return anything, so it's an invalid parameter to provide to drawTextEx.
I did try to use your scripts but for some reason it only shows line1 ?

Did I miss a step?
Code:
var width = Graphics.boxWidth - 400;
myWin = new Window_Base(350, 25, width, 570);
SceneManager._scene.addChild(myWin)
myWin.drawText(line1, -25, 0, width, "center");
myWin.drawText(line2, -25, this.lineHeight(), width, "center");
myWin.drawText(line3, -25, this.lineHeight() * 2, width, "center");
Um, I don't know why that even shows line1. That code should display nothing, because "line1" and "line2" and "line3" are the names of variables that weren't defined.

For now, try putting quotes around them.
You're setting skillName to the return value of the call to drawText. It doesn't return anything, so it's an invalid parameter to provide to drawTextEx.
That actually makes sense 👋, but then why does it show the skill name correctly? Shouldn't it say null or show nothing?

Um, I don't know why that even shows line1. That code should display nothing, because "line1" and "line2" and "line3" are the names of variables that weren't defined.

For now, try putting quotes around them.
Oh I didn't realize they were variables I had to define 😅 I thought I only had to define them if I were doing the "unknown number of lines" method

I did put quotes around them, line1 still displays and line2 and 3 are nowhere
1749514236591.png


EDIT:

By the by I realized that, in the Skill.js file the skill descriptions already come with \n in them, so putting them in a drawTextEx draws them with the line breaks, however, since my window is of a irregular width, It cuts the text. So I would need to be able to draw the text in a smaller size and auto line break it too 😢

Here's how it looks:

Spoiler: TextEx with SkillDesc
Code:
var width = Graphics.boxWidth - 400;

myWin = new Window_Base(350, 25, width, 570);

SceneManager._scene.addChild(myWin)

myWin.drawTextEx($dataSkills[8].description, 0, 0);

1749514371528.png
That actually makes sense 👋, but then why does it show the skill name correctly? Shouldn't it say null or show nothing?


Oh I didn't realize they were variables I had to define 😅 I thought I only had to define them if I were doing the "unknown number of lines" method

I did put quotes around them, line1 still displays and line2 and 3 are nowhere
View attachment 342559

EDIT:

By the by I realized that, in the Skill.js file the skill descriptions already come with \n in them, so putting them in a drawTextEx draws them with the line breaks, however, since my window is of a irregular width, It cuts the text. So I would need to be able to draw the text in a smaller size and auto line break it too 😢

Here's how it looks:

Spoiler: TextEx with SkillDesc
Code:
var width = Graphics.boxWidth - 400;

myWin = new Window_Base(350, 25, width, 570);

SceneManager._scene.addChild(myWin)

myWin.drawTextEx($dataSkills[8].description, 0, 0);

View attachment 342560
No, because the initial call to drawText *does* do something. It's just that you're then storing its return value in a variable afterwards, which ends up being undefined because drawText doesn't return anything.
No, because the initial call to drawText *does* do something. It's just that you're then storing its return value in a variable afterwards, which ends up being undefined because drawText doesn't return anything.

I see, so that can cause problems in other areas right?
I see, so that can cause problems in other areas right?
It's a pretty self-contained problem. It just means that you've created a useless variable and your drawTextEx call won't work, but it won't affect anything beyond that.
Not guaranteeing anything - currently this is what I have based on what I was understanding from your posts.

Before:
Spoiler
s2.png

JavaScript:
const s = SceneManager._scene;
const win = s._myWin = new Window_Base(0, 0, 360, 240);
s.addChild(win);
const text = $dataSkills[1].description;
win.drawTextEx(text, 0, 0);

After:
Spoiler
s1.png

JavaScript:
const s = SceneManager._scene;
const win = s._myWin = new Window_Base(0, 0, 360, 240);
s.addChild(win);
const text = $dataSkills[1].description;
const split = text.split('\n');
for (const sp of split) {
    while(win.textWidth(sp) > win.width - (win.padding * 2)) {
        win.contents.fontSize -= 2;
    }
}
win.changeTextColor('#dea687');
win.drawTextEx(text, 0, 0, true);
win.resetFontSettings();

Here is the code that I replaced from the corescript - you can add this as a plugin and activate it in your project - I added a preserveText parameter so that it skips any resetting of the text font face, font size, or text color:
Spoiler
JavaScript:
Window_Base.prototype.drawTextEx = function(text, x, y, preserveText) {
    if (text) {
        var textState = { index: 0, x: x, y: y, left: x };
        textState.text = this.convertEscapeCharacters(text);
        textState.height = this.calcTextHeight(textState, false);
        this.resetFontSettings(preserveText);
        while (textState.index < textState.text.length) {
            this.processCharacter(textState);
        }
        return textState.x - x;
    } else {
        return 0;
    }
};

Window_Base.prototype.resetFontSettings = function(preserveText) {
    if (preserveText) return;
    this.contents.fontFace = this.standardFontFace();
    this.contents.fontSize = this.standardFontSize();
    this.resetTextColor();
};
This also means that I need to call a .resetFontSettings() if I need to revert everything back.

Not fully sure if this will break anything or if this is what you wanted, though. Maybe it will get you a bit closer with more testing and working through this. Hope it helps in some way.
Hey everyone! I'm very sorry for the late reply, I have found an even simpler solution using VictorEngine's Information Window and EcapeCodes.

Here's how it's looking now with the plugin:
Spoiler: ScreenShot
1750446579337.png

Here's the plugin command I used to make the text like this:
Code:
InformationWindow 1 Text: \ns[\e['$gameVariables.value(307)']]\r \n \}\e['$dataSkills[$gameVariables.value(307)].description']\r \n \n Skill Experience: \c[#f5cf6c]\e['$gameVariables.value(308)']\r

I even managed to make the text modular, by changing the variable "Current Skill" variable to match the Skill whose name and description I want shown. Like so:

1750446801427.png


I have no idea how I didn't come across this plugin before in my searches, I guess google decided to never show it to me merely because I was searching for "Custom Window plugin", instead of "Information Window plugin" 😠

Either way, I want to thank you all for your support, all of the helpful replies and the time all of you took to try and help me. Even if I ended up using a premade plugin for this, I will take all the knowledge I gained from this thread to heart, and I hope others can learn from this thread too.

This thread can be closed now.
This thread is closed due to being reported as solved. If you need it reopened, report it to let us know. Thank you!

Original thread: https://forums.rpgmakerweb.com/threads/how-to-add-complex-messages-to-the-drawtext-function.178128/