MV Using game variable in default rpg_windows js file to control window size

I know yanflys extended message plugin can do this but I dont want to use it.

In the rpg_windows js file under window message:

Code:
//-----------------------------------------------------------------------------
// Window_Message
//
// The window for displaying text messages.

function Window_Message() {
    this.initialize.apply(this, arguments);
}

Window_Message.prototype = Object.create(Window_Base.prototype);
Window_Message.prototype.constructor = Window_Message;

Window_Message.prototype.initialize = function() {
    var width = this.windowWidth();
    var height = this.windowHeight();
    var x = (Graphics.boxWidth - width) / 2;
    Window_Base.prototype.initialize.call(this, x, 0, width, height);
    this.openness = 0;
    this.initMembers();
    this.createSubWindows();
    this.updatePlacement();
};

I can change the width of text windows at "var width."

However, I want to be able to control this (and later height/x/y positions) using game variables.
I've tried changing it to var width = $gameVariables.value(14); but it doesn't work (14 used as an example, 14 being variable 14 in-game).

Is it possible to reference to specific in-game variables in these js files?

Thank you.
Ok, first things first, I wouldn't recommend modifying the rpg_... JS files, it gets hard to keep track of them and you could get issues later and not be able to debug them because your code is all over the place. Instead, create a plugin! That's what they're for.

If you put this on a JS file and install it as a plugin, it'll essentially have the same effect as what you did:
Code:
Window_Message.prototype.windowWidth = function() {
    return $gameVariables.value(14);
};

(It still doesn't work though)

The issue there is that this function is used only once: when the window is created on the scene. At that moment, initially, the value of the game variable was most likely 0. Thus, the window doesn't show up!

To demonstrate this, do this test: change the variable, now open and close the menu (this will cause the window to be recreated), then talk to an NPC. The window will have the desired size.

What you have to do in order for this to work is update the window size once the variable changes:

Code:
Window_Message.prototype.windowWidth = function() {
    return $gameVariables.value(14);
};

Window_Message.prototype.updateWidthIfNeeded = function() {
    var variableValue = $gameVariables.value(14);
    if (variableValue !== this.width) {
        this.width = variableValue;
        this.createContents(); // not sure if this is bad, could cause memory leaks :d
    }
};

(function () {
    var alias = Window_Message.prototype.update;
    Window_Message.prototype.update = function() {
        alias.apply(this, arguments);
        this.updateWidthIfNeeded();
    }
})();

The way I dealt with it was to constantly lookup the variable on Window_Message and then update the size if it's different, it's not the cleanest way to do it, but hey, it works.

Also, you'll probably want to use a plugin parameter for the variable ID. I'll leave it as an exercise to the reader 😉
Thanks! That makes sense and works. Plugin parameters will definitely help me keep it organized.

I'm not sure how to modify the x and y position of the textbox though. For height I can just replace windowWidth with windowHeight, but "windowX" and "windowY" aren't a thing looking at the rpg_windows js file.

Code:
Window_Message.prototype.windowX = function() {
    return $gameVariables.value(16);
};

Window_Message.prototype.updateXIfNeeded = function() {
    var variableValue = $gameVariables.value(16);
    if (variableValue !== this.x) {
        this.x = variableValue;
        this.createContents();
    }
};

(function () {
    var alias = Window_Message.prototype.update;
    Window_Message.prototype.update = function() {
        alias.apply(this, arguments);
        this.updateHeightIfNeeded();
    }
})();

I saw in the rpg_windows file "var x = (Graphics.boxWidth - width) / 2;" which I think has to do with it.
Unfortunately, x and y are not as easy to modify because they're hardcoded on the default class. As an advice: if you're aiming for extensibility, never hardcode values or calculations on methods that do not exclusively return them; this way you can always override those methods that do the calculation (like windowWidth and windowHeight).

Anyway, to work around that, we can define our own methods for that purpose and overwrite the default x and y values after constructor (which is not ideal, but I can't think of a better solution in this case):

JavaScript:
Window_Message.prototype.windowX = function() {
    return $gameVariables.value(16);
};

Window_Message.prototype.windowY = function() {
    return $gameVariables.value(17);
};

(function () {
    var alias = Window_Message.prototype.initialize;
    Window_Message.prototype.initialize = function() {
        alias.apply(this, arguments);
        this.x = this.windowX();
        this.y = this.windowY();
    }
})();

By the way, since you're doing this for x, y, width and height, you can use the move method instead of doing it for each parameter:
JavaScript:
Window_Message.prototype.needsToMove = function() {
    return $gameVariables.value(16) !== this.x || $gameVariables.value(17) !== this.y
           || $gameVariables.value(14) !== this.width || $gameVariables.value(15) !== this.height;
};

Window_Message.prototype.moveIfNeeded = function() {
    if (this.needsToMove()) {
        this.move(
            $gameVariables.value(16), // x
            $gameVariables.value(17), // y
            $gameVariables.value(14), // width
            $gameVariables.value(15)  // height
        );
        this.createContents();
    }
};

(function () {
    var alias = Window_Message.prototype.update;
    Window_Message.prototype.update = function() {
        alias.apply(this, arguments);
        this.moveIfNeeded();
    }
})();
Awesome, thank you so much for taking time to teach and help me!
Commenting so I remember to read this thoroughly later.

Original thread: https://forums.rpgmakerweb.com/threads/using-game-variable-in-default-rpg_windows-js-file-to-control-window-size.126444/