Need help debugging "Cannot Read Property 'Apply' of undefined"

Hi, I usually am able to debug on my own, but I have been absolutely stumped on this. As far as I can tell, everything is done correctly. Let me know if you need any other code snippet:

Code:
//////////////////////////////////////////////////////////////////////////////////////////////
// Window_Start_2_Options
//
// 

var testingMode = PluginManager.Paramaters('testing Mode');

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

Window_Start_2_Options.prototype = Object.create(Window_Command.prototype);
Window_Start_2_Options.prototype.constructor = Window_Start_2_Options;

Window_Start_2_Options.prototype.initialize = function() {
    Window_Command.prototype.initialize.call(this, 0, 0);
    var width = this.windowWidth();
    var Height = this.windowHeight();
};

Window_Start_2_Options.prototype.windowWidth = function() {
    return 240;
};

Window_Start_2_Options.prototype.windowHeight = function() {
    if (testingMode === true) {
        return this.fittingHeight(4);
    } else {
        return this.fittingHeight(3);
    }
  
};

Here is the error I'm getting:

Code:
TypeError: Cannot read property 'apply' of undefined
    at new Window_Start_2_Options (/C:/Users/Lorence/Desktop/Resistance%20is%20Feudal/Phase%202/Resistance%20is%20Feudal%206/js/plugins/ResistanceIsFeudalGameStart.js:139)
    at Scene_Map.createStartWindow2Options (/C:/Users/Lorence/Desktop/Resistance%20is%20Feudal/Phase%202/Resistance%20is%20Feudal%206/js/plugins/ResistanceIsFeudalGameStart.js:65)
    at Scene_Map.update (/C:/Users/Lorence/Desktop/Resistance%20is%20Feudal/Phase%202/Resistance%20is%20Feudal%206/js/plugins/ResistanceIsFeudalGameStart.js:30)
    at Function.SceneManager.updateScene (rpg_managers.js:2024)
    at Function.SceneManager.updateMain (rpg_managers.js:1983)
    at Function.SceneManager.update (rpg_managers.js:1907)
This is unrelated but
Code:
var testingMode = PluginManager.Paramaters('testing Mode');
parameters is spelled wrong and should be lower-case.

Can you also post the next two functions in the stack? The function which has
'new Window_Start_2_Options' (I'm guessing it is 'Scene_Map.prototype.createStartWindow2Options') and the function which calls that (which is 'Scene_Map.prototype.update')

The reason I ask for that is because I'm confused about why the windows are being created in the 'update' function which runs every frame (60x per second). Is that on purpose?
Aloe again Guvner!

1. Thanks for the catching that, the spelling was corrected.

2. Being called in update was intentional. I want the window to be triggered by a game event, so tying it to update and using an if statement to check the status of a switch was the best idea I had (I have no doubt that there was a better way to do this. But it does work.)

Here is the code:

Code:
(function()
{
var _Scene_Map_update = Scene_Map.prototype.update;
var startScreenProgress = 0;
Scene_Map.prototype.update = function() {
    _Scene_Map_update.call(this);
    if (!this._startWindow1 && startScreenProgress === 0) {
        startScreenProgress++;
        this.createStartWindow1();
    };

    if (Input.isPressed('ok') && startScreenProgress === 1) {
        this.close1();
        startScreenProgress++;
        this.createStartWindow2();
        this.createStartWindow2Options();
    };

    /*if (Input.isPressed('ok') && startScreenProgress === 2) {
        this.close2();
        startScreenProgress++;
        //this.createStartWindow3();
    };

    if (Input.isPressed('ok') && startScreenProgress === 3) {
        this.close3();
    };*/

};

Scene_Map.prototype.createStartWindow1 = function() {
    this._startWindow1 = new Window_Start_1(0, 0);
    this._startWindow1.x = 10;
    this._startWindow1.y = 10;
    this._startWindow1.drawText ("This is what Resistance is Feudal is. I hope you", 0, 0, 0, 'left' );
    this._startWindow1.drawText ("enjoy!!!", 0, 35, 0, 'left' );
    this._startWindow1.drawText ("Press enter to continue", 425, 525, 0, 'left' );
    this.addWindow(this._startWindow1);
};

Scene_Map.prototype.createStartWindow2 = function() {
    this._startWindow2 = new Window_Start_2(0, 0);
    this._startWindow2.x = 10;
    this._startWindow2.y = 10;
    this._startWindow2.drawText ("This sure is working, huh?", 0, 0, 0, 'left' );
    this._startWindow2.drawText ("Press enter to continue", 425, 525, 0, 'left' );
    this.addWindow(this._startWindow2);
};

Scene_Map.prototype.createStartWindow2Options = function() {
    this.startWindow2Options = new Window_Start_2_Options(0, 0);
    this.startWindow2Options.x = 10;
    this.startWindow2Options.y = 10;
    this.addWindow(this.startWindow2Options);
};
[redacted since I'm 99% sure I was totally wrong]
The problem is this function:
Code:
function Window_Start_2_Options() {
    this.initialize.apply(this, arguments);
};

Because of how you've called it,
there is no "this". Since "this" is undefined, so is the function apply().

Edit: "this" refers to the base "Window", and Window.initialize is undefined, therefore so is Window.initialize.apply().
Do you know how to solve this?
Ummm. Now that I'm looking at how the other windows are defined, I can't figure out why they work, actually. I may be wrong. I'm going to go look into it some more...Sorry!

Edit: @TragicNumberOne I guess I've been away from writing plugins for too long. I really do apologize for that first really bad bit of advice. However, it would be very helpful if you could post the entire code for ResistanceIsFeudalGameStart.js, since I could easily check the line numbers that are being called. If you don't want to put it out here for everyone, you could PM me.
Ummm. Now that I'm looking at how the other windows are defined, I can't figure out why they work, actually. I may be wrong. I'm going to go look into it some more...Sorry!
Take your time, I appreciate any help I can get!
Quick hunch is it could be something to do with sending arguments to the constructor when it isn't expecting any arguments. Not sure if that is really the issue but it's the first strange thing I see.
What I mean is:
Code:
Scene_Map.prototype.createStartWindow2Options = function() {
   this.startWindow2Options = new Window_Start_2_Options(0, 0);
(above) The arguments of (0,0) are sent to the constructor and applied to the initialize method, but the initialize method is not expecting any arguments (below).
Code:
function Window_Start_2_Options() {
   this.initialize.apply(this, arguments);
};

Window_Start_2_Options.prototype = Object.create(Window_Command.prototype);
Window_Start_2_Options.prototype.constructor = Window_Start_2_Options;

Window_Start_2_Options.prototype.initialize = function() {
   Window_Command.prototype.initialize.call(this, 0, 0);
   var width = this.windowWidth();
   var Height = this.windowHeight();
};

I'm not convinced this would cause a crash though / don't really think it matches with the error message you got.


Otherwise, it could be something with the order things are loaded. I'm not good with Scene_Map yet, but it could be some timing issue where the Window Layer isn't created yet (map isn't finished loading) before it tries to create the first window. Not so sure about that either.
I have no problem sharing the whole file. The only reason I didn't was because I don't want to overwhelm anyone. This particular file is short enough though, so that hopefully won't be a problem. Feel free to ask for anything else you need, and thanks for helping!

Code:
/*:
@plugindesc Resistance is feudal start screens.

@author Redacted

@help testhelp

@param Testing Mode
@type boolean
@parent ---General---
@desc enables the "developer" past option will be available, enabling all traits.

*/
(function()
{
var _Scene_Map_update = Scene_Map.prototype.update;
var startScreenProgress = 0;
Scene_Map.prototype.update = function() {
    _Scene_Map_update.call(this);
    if (!this._startWindow1 && startScreenProgress === 0) {
        startScreenProgress++;
        this.createStartWindow1();
    };

    if (Input.isPressed('ok') && startScreenProgress === 1) {
        this.close1();
        startScreenProgress++;
        this.createStartWindow2();
        this.createStartWindow2Options();
    };

    /*if (Input.isPressed('ok') && startScreenProgress === 2) {
        this.close2();
        startScreenProgress++;
        //this.createStartWindow3();
    };

    if (Input.isPressed('ok') && startScreenProgress === 3) {
        this.close3();
    };*/

};

Scene_Map.prototype.createStartWindow1 = function() {
    this._startWindow1 = new Window_Start_1(0, 0);
    this._startWindow1.x = 10;
    this._startWindow1.y = 10;
    this._startWindow1.drawText ("This is what Resistance is Feudal is. I hope you", 0, 0, 0, 'left' );
    this._startWindow1.drawText ("enjoy!!!", 0, 35, 0, 'left' );
    this._startWindow1.drawText ("Press enter to continue", 425, 525, 0, 'left' );
    this.addWindow(this._startWindow1);
};

Scene_Map.prototype.createStartWindow2 = function() {
    this._startWindow2 = new Window_Start_2(0, 0);
    this._startWindow2.x = 10;
    this._startWindow2.y = 10;
    this._startWindow2.drawText ("This sure is working, huh?", 0, 0, 0, 'left' );
    this._startWindow2.drawText ("Press enter to continue", 425, 525, 0, 'left' );
    this.addWindow(this._startWindow2);
};

Scene_Map.prototype.createStartWindow2Options = function() {
    this.startWindow2Options = new Window_Start_2_Options(0, 0);
    this.startWindow2Options.x = 10;
    this.startWindow2Options.y = 10;
    this.addWindow(this.startWindow2Options);
};
//////////////////////////////////////////////////////////////////////////////////////////////
// Window_Start_1
//
// The first start window explaining the game.

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

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

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

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

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

Scene_Base.prototype.close1 = function() {
    this._startWindow1.close();
};

//////////////////////////////////////////////////////////////////////////////////////////////
// Window_Start_2
//
// The first start window explaining the game.

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

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

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

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

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

Scene_Base.prototype.close2 = function() {
    this._startWindow2.close();
};

//////////////////////////////////////////////////////////////////////////////////////////////
// Window_Start_2_Options
//
// The window for selecting which selecting which past you have.

var testingMode = PluginManager.parameter('testing Mode');

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

Window_Start_2_Options.prototype = Object.create(Window_Command.prototype);
Window_Start_2_Options.prototype.constructor = Window_Start_2_Options;

Window_Start_2_Options.prototype.initialize = function() {
    Window_Command.prototype.initialize.call(this, 0, 0);
    var width = this.windowWidth();
    var Height = this.windowHeight();
};

Window_Start_2_Options.prototype.windowWidth = function() {
    return 240;
};

Window_Start_2_Options.prototype.windowHeight = function() {
    if (testingMode === true) {
        return this.fittingHeight(4);
    } else {
        return this.fittingHeight(3);
    }
  
};
/*
Window_Start_2_Options.prototype.makeCommandList = function() {
    this.addCommand("Diplomat", 'diplomat', true);
    this.addCommand("Financier", 'financier', true);
    this.addCommand("Warlord", 'warlord', true);
    if (testingMode === true) {
        this.addCommand("God", 'god', true);
    }
};
*/
})();
@TragicNumberOne I think I figured it out from the line numbers alone; you need to place your prototype for Window_Start_2_Options BEFORE the place in your code where you call it. Prototypes functions don't appear to be hoisted the way regular functions are.
@TragicNumberOne I think I figured it out from the line numbers alone; you need to place your prototype for Window_Start_2_Options BEFORE the place in your code where you call it. Prototypes functions don't appear to be hoisted the way regular functions are.
It didn't work. Not only that, but now no windows comes up at all. (This plugin is supposed to have another window to appear before this problematic one, and until I made your suggested change, it worked.)
When I copy it directly as is and try it, I get an error in the console before the title screen even boots.

Uncaught TypeError: undefined is not a function @ line 134

This is line 134:
Code:
var testingMode = PluginManager.parameter('testing Mode');

This was the line that I noticed in the first post, but didn't think it was related. Actually, it is related. Apparently when RMMV encounters an uncaught error, it stops executing the rest of the plugin. This means that your function Window_Start_2_Options() is actually undefined, because it was after line 134.

And the original (caught) error now totally makes sense:
TypeError: Cannot read property 'apply' of undefined

If you fix line 134, you'll be able to proceed past this error.

Code:
var params = PluginManager.parameters("YourFileName");
var testingMode = params["Testing Mode"];
When I copy it directly as is and try it, I get an error in the console before the title screen even boots.

Uncaught TypeError: undefined is not a function @ line 134

This is line 134:
Code:
var testingMode = PluginManager.parameter('testing Mode');

This was the line that I noticed in the first post, but didn't think it was related. Actually, it is related. Apparently when RMMV encounters an uncaught error, it stops executing the rest of the plugin. This means that your function Window_Start_2_Options() is actually undefined, because it was after line 134.

And the original (caught) error now totally makes sense:
TypeError: Cannot read property 'apply' of undefined

If you fix line 134, you'll be able to proceed past this error.

Code:
var params = PluginManager.parameters("YourFileName");
var testingMode = params["Testing Mode"];
Wow... that fixed it. I never would have caught that in a million years!

As always, thank you so much for the help. I always do appreciate it.

Thanks @Llareian as well. Everyone here is just so darn friendly! Thanks again!

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/need-help-debugging-cannot-read-property-apply-of-undefined.91258/