Can't figure out what's wrong here

Howdy!

After some help with menus, I though I had a good understanding of them, however, this is clearly not the case so here I am.

My goal is just to make a window popup with text. Right now the error comes up when I try to create the window.

Here's my error and code:

Code:
TypeError: Cannot read property 'addChild' of undefined

Code:
//There is no need for this to be declared becasue the interpreterCommand from ResistanceIsFeudalGameStart.
//var interpreterCommand = Game_Interpreter.prototype.pluginCommand;

Game_Interpreter.prototype.pluginCommand = function(command, args) {
    interpreterCommand.apply(this);
    if (command === 'endScreen') {
        Scene_Map.prototype.createEndWindow();
    };
};

Scene_Map.prototype.createEndWindow = function() {
    this._endWindow = new Window_End(0, 0);
    this._endWindow.x = 10;
    this._endWindow.y = 10;
    //if (true) {
        this._endWindow.drawText ("Ending 1 text", 0, 0, 0, 'left' );
    //}
    Scene_Base.prototype.addWindow(this._endWindow);
};

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

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

Window_End.prototype.initialize = function(x, y) {  
    var width = this.windowWidth();
    var Height = this.windowHeight();
    Window_Base.prototype.initialize.call(this, x, y, width, Height);
};

Window_End.prototype.windowWidth = function() {
    return Graphics.boxWidth - 20;
};

Window_End.prototype.windowHeight = function() {
    return Graphics.boxHeight - 20;
};

Thanks for helping!
A comment unrelated to your question:
  • The way you have overwritten the Game_Interpreter.prototype.pluginCommand function will break plugin commands from all other plugins above it because you're not applying the arguments to the alias.
On to your question:

There's an important difference between a Constructor and an Instance that is useful to understand.
Scene_Map is the constructor for the map scene, but it is not the actual object that is being run during the game. Similar to how your Window_End is a constructor. What appears in the game is an instance of the constructor created with the "new" keyword.

So the plugin command should call the createEndWindow method of the actual scene, rather than the scene constructor.

Spoiler: Here's the short answer
Code:
Scene_Map.prototype.createEndWindow();
//should be replaced with
SceneManager._scene.createEndWindow();

Code:
Scene_Base.prototype.addWindow(this._endWindow);
//should be replaced with
this.addWindow(this._endWindow);
A comment unrelated to your question:
  • The way you have overwritten the Game_Interpreter.prototype.pluginCommand function will break plugin commands from all other plugins above it because you're not applying the arguments to the alias.
On to your question:

There's an important difference between a Constructor and an Instance that is useful to understand.
Scene_Map is the constructor for the map scene, but it is not the actual object that is being run during the game. Similar to how your Window_End is a constructor. What appears in the game is an instance of the constructor created with the "new" keyword.

So the plugin command should call the createEndWindow method of the actual scene, rather than the scene constructor.

Spoiler: Here's the short answer
Code:
Scene_Map.prototype.createEndWindow();
//should be replaced with
SceneManager._scene.createEndWindow();

Code:
Scene_Base.prototype.addWindow(this._endWindow);
//should be replaced with
this.addWindow(this._endWindow);
Howdy, thanks for the response. I actually solved the issue using a workaround, and have tried to close the thread, but you seem to have hit on something else with the first part of your response. The new code looks like this if you're interested. I think that this new code solves both problems:
Code:
Scene_Map.prototype.update = function() {
    _Scene_Map_update.call(this);
    if ($gameSwitches.value(6)) {
        $gameSwitches.setValue(6, false);
        this.createEndWindow();
    }
};

Scene_Map.prototype.createEndWindow = function() {
    this._endWindow = new Window_End(0, 0);
    this._endWindow.x = 10;
    this._endWindow.y = 10;
    //if (true) {
        this._endWindow.drawText ("Ending 1 text", 0, 0, 0, 'left' );
    //}
    this.addWindow(this._endWindow);
};

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

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

Window_End.prototype.initialize = function(x, y) {
    var width = this.windowWidth();
    var Height = this.windowHeight();
    Window_Base.prototype.initialize.call(this, x, y, width, Height);
};

Window_End.prototype.windowWidth = function() {
    return Graphics.boxWidth - 20;
};

Window_End.prototype.windowHeight = function() {
    return Graphics.boxHeight - 20;
};

The reason why the first attempt was done like that was because I was trying to create a solution using plugin commands and couldn't figure out why it didn't work; this lead to a domino effect of increasingly zany attempted fixes. However, this new system of flags getting checked on update seems to be smoother (if less efficient.)
@TragicNumberOne I've seen your Report asking for this to be closed, but I am now unclear whether this new development means that you want to keep it open to continue the conversation with Aloe Guvner. I am, therefore, taking no action at this point. When you are sure that you want the thread closed, please Report it again.
@TragicNumberOne I've seen your Report asking for this to be closed, but I am now unclear whether this new development means that you want to keep it open to continue the conversation with Aloe Guvner. I am, therefore, taking no action at this point. When you are sure that you want the thread closed, please Report it again.
Thanks! He responded after I sent the report, so I was unsure if you would notice.
Looks good now! From just reading it, I believe it would work fine.

I see that you are using switch #6 inside the 'update' method in order to create the window. Presumably, some event on the map is turning switch #6 to ON. The thing is that the 'update' method runs every single frame, so 60x per second, continuously checking if the switch is ON. Although it's not likely to cause any noticeable lag, I would suggest one improvement:

  1. Alias the 'Scene_Map.prototype.createAllWindows' method and call your 'createEndWindow' from inside there
  2. Add a line to the end of your 'Window_End.prototype.initialize' method with "this.hide();"
  3. In your event on the map instead of turning switch #6 on, do a script call of "SceneManager._scene._endWindow.show();"
The benefits of doing it this way - you run the code on-demand rather than 60x per second, and you don't have to consume a switch.
Looks good now! From just reading it, I believe it would work fine.

I see that you are using switch #6 inside the 'update' method in order to create the window. Presumably, some event on the map is turning switch #6 to ON. The thing is that the 'update' method runs every single frame, so 60x per second, continuously checking if the switch is ON. Although it's not likely to cause any noticeable lag, I would suggest one improvement:

  1. Alias the 'Scene_Map.prototype.createAllWindows' method and call your 'createEndWindow' from inside there
  2. Add a line to the end of your 'Window_End.prototype.initialize' method with "this.hide();"
  3. In your event on the map instead of turning switch #6 on, do a script call of "SceneManager._scene._endWindow.show();"
The benefits of doing it this way - you run the code on-demand rather than 60x per second, and you don't have to consume a switch.
Thanks for the advice, I'll try to implement these changes.

As always thanks for help!

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/cant-figure-out-whats-wrong-here.94483/