MZ Window_Base

Hi!

I've been trying to tackle the Window_Base class and extending it to make my own custom windows.
But I lack knowledge about how they actually work...

Is there a tool somewhere with the core map classes? (Like the spreadsheet here)

The part where you create the custom class and extend Window_Base, then add it to Scene_Map is alright...

But I can't get it to show up and I keep getting error messages, mostly this one :
1722001234093.png


Thanks for any help or advice. 👋
best i can provide in terms of documentation is this.

I suggest searching a guide (Poryg has a good video one), looking inside the windows and scenes js files, and then looking inside some plugins to see how others make and call windows
best i can provide in terms of documentation is this.
I love that site, but windows are one of the big ways that MZ's code is different from MV, so that's not entirely helpful. Same would go for Poryg's MV guides.

Looking through the existing window classes to see how your code is different is good advice.

Speaking of code, @Artille, it would of course be easier to tell you what you're doing wrong if we saw your actual code.

Although I am kind of leery when looking at that error trace - it looks like you might be trying to create a window through a Script command in an event, which is really not the best way to approach it.
I love that site, but windows are one of the big ways that MZ's code is different from MV, so that's not entirely helpful. Same would go for Poryg's MV guides.

Looking through the existing window classes to see how your code is different is good advice.

Speaking of code, @Artille, it would of course be easier to tell you what you're doing wrong if we saw your actual code.

Although I am kind of leery when looking at that error trace - it looks like you might be trying to create a window through a Script command in an event, which is really not the best way to approach it.
OMG!

I actually started some tests as pure scripts in events that ran in parrallel and ehm... forgot to delete them. 😐

Now there are no more issues with the code itself. 😀
But it still does not show up.

The code is basic (as in, just created a sprite and added it in Scene_Map) but if it can help :
Spoiler: Code
JavaScript:
(() => {
    class Window_Sprite extends Window_Base {
        constructor(rect) {
            console.log("Initializing Window_Sprite with rect:", rect);
            super(rect);
            this.initializeSprite();
        }

        initializeSprite() {
            console.log("Initializing sprite...");
            const bitmap = new Bitmap(200, 50); // Create a bitmap with the specified width and height
            bitmap.drawText("Hello World", 0, 0, 200, 50, 'center'); // Draw text on the bitmap

            this.sprite = new Sprite(bitmap); // Create a sprite with the bitmap
            this.addChild(this.sprite); // Add the sprite to the window
            console.log("Sprite initialized and added to the window.");
        }
    }

    const _Scene_Map_createAllWindows = Scene_Map.prototype.createAllWindows;
    Scene_Map.prototype.createAllWindows = function () {
        console.log("Creating all windows...");
        _Scene_Map_createAllWindows.call(this);
        this.createSpriteWindow();
    };

    Scene_Map.prototype.createSpriteWindow = function () {
        const rect = new Rectangle(Graphics.width - 220, 20, 200, 50); // Define the rectangle for the window
        console.log("Creating sprite window with rect:", rect);
        this._spriteWindow = new Window_Sprite(rect); // Create the new window
        console.log("Sprite window created:", this._spriteWindow);
        this.addWindow(this._spriteWindow); // Add the window to the scene
        console.log("Sprite window added to the scene.");
    };

})();

All console logs confirm it was done successfully, but ehm... nothing is showing. 😢
In MZ, Window_Base#initialize expects a rectangle as the first argument (rmmz_windows.js):
JavaScript:
Window_Base.prototype.initialize = function(rect) {
    Window.prototype.initialize.call(this);
    this.loadWindowskin();
    this.checkRectObject(rect);
    this.move(rect.x, rect.y, rect.width, rect.height);
checkRectObject throws the error you're seeing, to avoid moving the window to undefined coordinates.

You can see examples of this in rmmz_scenes.js, e.g. Scene_Title:
JavaScript:
Scene_Title.prototype.createCommandWindow = function() {
// ...
    const rect = this.commandWindowRect();
    this._commandWindow = new Window_TitleCommand(rect);
// ...
    this.addWindow(this._commandWindow);
JavaScript:
Scene_Title.prototype.commandWindowRect = function() {
// ...
    return new Rectangle(wx, wy, ww, wh);
}
I'd second @Robro33's advice from above - I think it would be helpful if you copied the actual methods from an existing window class, rather than rewriting it with your own syntax. That will make it easier to see where you're doing something different/wrong.

In this case, I think it's because of how your constructor is not referencing any of the default actions from Window_Base.initialize(), so it's not setting values to any of the basic window properties or giving it any opacity.

The standard for window classes in MZ is to add to the existing constructor through the initialize method.

Code:
Window_MapName.prototype.constructor = Window_MapName;

Window_MapName.prototype.initialize = function(rect) {
    Window_Base.prototype.initialize.call(this, rect);
    this.opacity = 0;
    this.contentsOpacity = 0;
    this._showCount = 0;
    this.refresh();
};

Code:
Window_Message.prototype.constructor = Window_Message;

Window_Message.prototype.initialize = function(rect) {
    Window_Base.prototype.initialize.call(this, rect);
    this.openness = 0;
    this.initMembers();
};

I think in your desire to write it differently, you missed some of the procedures. If I change your code to match the engine's default syntax:
Code:
(() => {
    function Window_Sprite() {
        this.initialize(...arguments);
    }

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

    Window_Sprite.prototype.initialize = function(rect) {
        Window_Base.prototype.initialize.call(this, rect);
        const bitmap = new Bitmap(200, 50); // Create a bitmap with the specified width and height
        bitmap.drawText("Hello World", 0, 0, 200, 50, 'center'); // Draw text on the bitmap

        this.sprite = new Sprite(bitmap); // Create a sprite with the bitmap
        this.addChild(this.sprite); // Add the sprite to the window
    };


    const _Scene_Map_createAllWindows = Scene_Map.prototype.createAllWindows;
    Scene_Map.prototype.createAllWindows = function () {
        console.log("Creating all windows...");
        _Scene_Map_createAllWindows.call(this);
        this.createSpriteWindow();
    };

    Scene_Map.prototype.createSpriteWindow = function () {
        const rect = new Rectangle(Graphics.width - 220, 20, 200, 50); // Define the rectangle for the window
        console.log("Creating sprite window with rect:", rect);
        this._spriteWindow = new Window_Sprite(rect); // Create the new window
        console.log("Sprite window created:", this._spriteWindow);
        this.addWindow(this._spriteWindow); // Add the window to the scene
        console.log("Sprite window added to the scene.");
    };

})();

It works as expected.

1722003493353.png


Edit: Setting the opacity is even superfluous, the default if you call Window_Base's initialize is it sets the window to opaque.
I must be missing something though, even when literally copying and pasting your version, there is nothing shown on screen.
I assume (...arguments) is used a spread syntax

I disabled all my other plugins (working on a few...) and I made sure no events are running either to make it as sterile as possible, yet I don't get the window. 🙁

I'll keep looking into it though, and thanks for the help! Appreciated. 😊
I don't know what to say, it's directly copy and pasted from my plugin and it functions per the screenshot above.
Yup, Turan's test plugin works for me too.

I disabled all my other plugins (working on a few...) and I made sure no events are running either to make it as sterile as possible
To get a cleaner test environment, you could:
  • Use Game -> Update Corescript to refresh your project's js & js/libs files; or
  • Test in a new project.
This may not help if the files in your editor's install directory have been edited/corrupted in some way. In that case you could open the RMMZ install folder, uninstall RMMZ, manually delete any leftover files there, then reinstall it.
Ok, so I recently update my MZ to the latest version (1.8.0), but didn't update the core script.

I just did, which didn't solve the issue.
I also created a new project and copy/paste the corescripts from there (just to be sure), which didn't solve the issue.

However, when I create a new project, and test the script, it works fine...

So I tried to create a new project, keep everything as is, except I copied the data folder into my new project, which results in the script no longer working again.
So the issue lies in the data folder, presumably system.json

So I tried to copy all data files (except system) into a new project, which does work.

So, I tried to narrow down what part of system.JSON creates the issue and was led to... prepare to laugh your ass off...
"screenWidth":1920,
"screenHeight":1080,
"uiAreaWidth":816,
"uiAreaHeight":624

Can you spot it? Can you see what's happening?
Silly me is trying to show an UI on screen where... UI is not existing...

Big round of applause to me for trying to create something outside of its box...

Going back to my original project, I update the UI size and what do you know... it works now...


Going back to my original code :
Spoiler: code
JavaScript:
    class Window_Sprite extends Window_Base {
        constructor(...args) {
            super(...args);
            this.initialize(...args);
        }

        initialize(rect) {
            Window_Base.prototype.initialize.call(this, rect); // Call the parent class's initialize method
            this.opacity = 255;
            this.contentsOpacity = 255;

            const bitmap = new Bitmap(200, 50);
            bitmap.drawText("Hello World", 0, 0, 200, 50, 'center');

            this.sprite = new Sprite(bitmap);
            this.addChild(this.sprite);
        }

    }

I implemented the necessary steps as Turan advised, and it works!
Thanks for the help, I'll sleep a little bit less stupid tonight.

Original thread: https://forums.rpgmakerweb.com/threads/window_base.170633/