MZ Help calling a Windows prototype function

Hi all!

I'm creating a costume scene that has several windows (title, command, selections and informations).
In the information window I've this function:


JavaScript:
Window_CraftingInformation.prototype.refresh = function() {

    };

And I start by calling it here:


JavaScript:
    Scene_Crafting.prototype.create = function() {
        Scene_MenuBase.prototype.create.call(this);
        this.createCraftingTitleWindow();
        this.createInformationWindow();
        if (mainArrayReader("Mode") === "craftMode3") {
            this.createModeSelectionWindow();
            currentWindow = "Select";
        } else {
            if (mainArrayReader("Mode") === "craftMode1") {
                this.createCombineColumnAWindow();
                this.createCombineColumnBWindow();
                currentWindow = "ColA";
            } else {
                currentWindow = "Formula";
                //ToDo Formula
            }
        };
        this._craftingInformationWindow.refresh();
    };

It works without problems but if I try to call it outside from the Scene_Crafting (to update the item description) it says it cannot read "refresh" of undefined.

I'm sure that the window is created, i tried various way to call it but to no vail.
Am I missing something? What's the correct way to call another Windows function?

Thanks!
It works without problems but if I try to call it outside from the Scene_Crafting (to update the item description) it says it cannot read "refresh" of undefined.
That's not super helpful - you'd have to show us that code 😛

What is creating that window? Is it the createInformationWindow() code? You don't show that, either.

Basically if you're trying to execute a piece of code that's giving you an error but it's not this code...well, just showing this code can't help you solve it.
Also, it could be helpful to share (a screenshot of) the error trace: a record of recent function calls leading up to the error. After the error occurs, press F8 (or F12) to open the dev tools window, then look for red text on the Console tab of that window.
Maybe I didn't explain myself, the error is not happening in the "refresh" function as it works fine (it's a simple DrawText, nothing more)

The problem happens when I try to call it from another window, for example:

JavaScript:
    Window_CombineColumnB.prototype.update = function() {
        this.processCursorMove();
        this.processHandling();
        this.processTouch();
        Window_Scrollable.prototype.update.call(this);
        this._craftingInformationWindow.refresh();
    };

Instead of using ._this I tried also to use SceneManager.Scene_Crafting._craftingInformationWindow.refresh(); to the same result

I want to refresh the description window every time the Combine Column B window updates, but if I do as above it throws the error.
If it's useful here is how the description window is generated:

JavaScript:
    Scene_Crafting.prototype.createInformationWindow = function () {
      const rect = this.craftingInformationWindowRect();
      this._craftingInformationWindow = new Window_CraftingInformation(rect);
      this.addWindow(this._craftingInformationWindow);
    };

    Scene_Crafting.prototype.craftingInformationWindowRect = function() {
      const ww = Graphics.boxWidth;
      const wh = Graphics.boxHeight * 0.15;
      const wy = Graphics.boxHeight * 0.85;
      return new Rectangle(0, wy, ww, wh);
    };

The error is that this._craftingInformationWindow is "undefined" when calling it from another window

Code:
rmmz_managers.js:2036 TypeError: Cannot read property 'refresh' of undefined
    at Window_CombineColumnB.update (WD_Multicrafting.js:1315)
    at WindowLayer.update (rmmz_core.js:4287)
    at Scene_Crafting.Scene_Base.updateChildren (rmmz_scenes.js:129)
    at Scene_Crafting.Scene_Base.update (rmmz_scenes.js:52)
    at Scene_Crafting.Scene_MenuBase.update (rmmz_scenes.js:1105)
    at Function.SceneManager.updateScene (rmmz_managers.js:2103)
    at Function.SceneManager.updateMain (rmmz_managers.js:2063)
    at Function.SceneManager.update (rmmz_managers.js:1942)
    at Function.Graphics._onTick (rmmz_core.js:811)
    at TickerListener.emit (pixi.js:9093)
Found the solution!
This thread gave the right answer to access the function :eswt:

JavaScript:
SceneManager._scene._craftingInformationWindow.refresh();
Found the solution!
This thread gave the right answer to access the function :eswt:

JavaScript:
SceneManager._scene._craftingInformationWindow.refresh();
That's definitely a way to do it. However, the (arguably) more proper way to do it would probably be to assign the crafting window object to a property in the Window_CombineColumnB class:

JavaScript:
Scene_Crafting.prototype.createCombineColumnBWindow = function() {
    const rect = this.combineColumnBRect();
    this._combineColumnBWindow = new Window_CombineColumnB(rect);
    this._combineColumnBWindow.setCraftingInformationWindow(this._craftingInformationWindow);
    this.addWindow(this._craftingInformationWindow);
};

Window_CombineColumnB.prototype.setCraftingInformationWindow = function(craftingInformationWindow) {
    this._craftingInformationWindow = craftingInformationWindow;
};

I was just guessing what your Scene_Crafting.prototype.createCombineColumnBWindow method might look like, so you'd obviously want to adapt it to suit your situation. The important part is the 4th line, where the crafting information window object gets passed from Scene_Crafting to the combine column B window.
Thanks, I'll try the proper way!
Thanks, I'll try the proper way!
I think one thing that may not quite have clicked for you yet is that "this" is a context-dependent keyword. It refers to whatever the execution context is for the code that's running, which in the case of most plugin situations is *the object the code resides in*.

You were trying to call "this._craftingInformationWindow.refresh();" from Window_CombineColumnB, but that window class doesn't *have* a _craftingInformationWindow property because you created it as a property of Scene_Crafting. The only time "this" will correctly reference it is if you're using it inside a function of that scene.
I think one thing that may not quite have clicked for you yet is that "this" is a context-dependent keyword. It refers to whatever the execution context is for the code that's running, which in the case of most plugin situations is *the object the code resides in*.

You were trying to call "this._craftingInformationWindow.refresh();" from Window_CombineColumnB, but that window class doesn't *have* a _craftingInformationWindow property because you created it as a property of Scene_Crafting. The only time "this" will correctly reference it is if you're using it inside a function of that scene.

I suspected as much, I was taken back by the fact that SceneManager.Scene_Crafting._craftingInformationWindow.refresh(); gave undefined too, probably I did something wrong there as well :eswt:
I suspected as much, I was taken back by the fact that SceneManager.Scene_Crafting._craftingInformationWindow.refresh(); gave undefined too, probably I did something wrong there as well :eswt:
You can't do it like that because SceneManager has no property or function called Scene_Crafting. That's the name of your scene, but the *instance* of it that's currently running is contained in the _scene property on SceneManager. So that's why SceneManager._scene works, because that is effectively being replaced with "The currently running scene" and when that code is inside a window class that only exists in the crafting scene, the crafting scene is the only thing the currently running scene can possibly be. If that window were present in other scenes, you'd potentially face crashes in scenes besides this one that don't have a crafting information window.
SceneManager.Scene_Crafting._craftingInformationWindow.refresh();
Well that's not a correct reference to an object.

SceneManager <- is an object
.Scene_Crafting <- the SceneManager has no property called this. That's the name of a data type, not an object.

SceneManager._scene gives the object referencing the current scene, which may be of the type Scene_Crafting. Y'know, if that's what scene you're in.

@Trihan the ninja 😉
Well that's not a correct reference to an object.

SceneManager <- is an object
.Scene_Crafting <- the SceneManager has no property called this. That's the name of a data type, not an object.

SceneManager._scene gives the object referencing the current scene, which may be of the type Scene_Crafting. Y'know, if that's what scene you're in.

@Trihan the ninja 😉
*vanishes in a puff of smoke* I was never heeeeeere...

Original thread: https://forums.rpgmakerweb.com/threads/help-calling-a-windows-prototype-function.162041/