SetHandler ERROR

Hi guys,

I'm new to Javaskript and I don't quite get the setHandler functions.
What I'm trying to do:
I created a new menu option called "worldtree".
It's supposed to open a window and display an image.
And when you press ESC you get back to the menu.

I used some tutorials to create the window and draw the image, BUT whenever I open the window, I'm stuck.
I can't get back to the menu.
If I got it right (?), I need a setHandler to do this. The thing is, everytime I add the setHandler to my code I get an ERROR (TypeError undefined is not a function).
So my question is, how do these setHandlers work and what do I have to do to close the window?

Thanks in Advance 🙂

Code:
//Scene

Scene_Menu.prototype.commandWorldtree = function() {
    SceneManager.push(Worldtree_Scene);
};

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

Worldtree_Scene.prototype = Object.create(Scene_MenuBase.prototype);
Worldtree_Scene.prototype.constructor = Worldtree_Scene;

Worldtree_Scene.prototype.initialize = function() {
   Scene_MenuBase.prototype.initialize.call(this);
};

Worldtree_Scene.prototype.stop = function() {
    Scene_MenuBase.prototype.stop.call(this);
};

Worldtree_Scene.prototype.terminate = function() {
    Scene_MenuBase.prototype.terminate.call(this);
};

Worldtree_Scene.prototype.start = function() {
    Scene_MenuBase.prototype.start.call(this);
};

Worldtree_Scene.prototype.create = function() {
    Scene_MenuBase.prototype.create.call(this);
    this._worldtreeWindow = new Window_Worldtree(0,0);
    this._worldtreeWindow.setHandler('cancel', this.popScene.bind(this));
    this._worldtreeWindow.activate();
    this.addWindow(this._worldtreeWindow);
};
And where do you have your Window_Worldtree defined?

It looks like this is at fault.

Code:
Worldtree_Scene.prototype.create = function() {
    Scene_MenuBase.prototype.create.call(this);
    this._worldtreeWindow = new Window_Worldtree(0,0);
    this._worldtreeWindow.setHandler('cancel', this.popScene.bind(this));
    this._worldtreeWindow.activate();
    this.addWindow(this._worldtreeWindow);
};
I don't see where you defined Window_Worldtree, if you even defined it.
Oh, it's here:
Code:
function Window_Worldtree() {
    this.initialize.apply(this, arguments);
}

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

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

Window_Worldtree.prototype.windowWidth = function() {
    return Graphics.width;
};

Window_Worldtree.prototype.windowHeight = function() {
    return Graphics.height;
};

//Draw picture function
Window_Worldtree.prototype.drawPicture = function(filename, x, y) {
    var bitmap = ImageManager.loadPicture(filename);
    this.contents.blt(bitmap, 0, 0, bitmap._canvas.width, bitmap._canvas.height, x, y);   
};
    
Window_Worldtree.prototype.refresh = function() {
    this.contents.clear();
    this.drawTextEx("Worldtree",0,0);
    this.drawPicture('worldtree',0,0);

};

Window_Worldtree.prototype.open = function() {
   this.refresh();
  Window_Base.prototype.open.call(this);
};
I see where the problem is.

Code:
Window_Worldtree.prototype = Object.create(Window_Base.prototype);
Window_Base doesn't have setHandler defined. setHandler is a prototype of Window_Selectable. So you need to set your window as a prototype of Window_Selectable.

Also, a couple of tips for cleaner code.
Code:
Worldtree_Scene.prototype.stop = function() {
   Scene_MenuBase.prototype.stop.call(this);
};

This is a totally pointless function. Since Worldtree_Scene is a prototype of Scene_MenuBase, it inherits every function from Scene_Menubase. So unless you want to change it, it's pointless to redefine it.

Code:
Worldtree_Scene.prototype.create = function() {
   Scene_MenuBase.prototype.create.call(this);
   this._worldtreeWindow = new Window_Worldtree(0,0);
   this._worldtreeWindow.setHandler('cancel', this.popScene.bind(this));
   this._worldtreeWindow.activate();
   this.addWindow(this._worldtreeWindow);
};

This is not a pointless function, because after it calls the parent function, it adds some information unique for itself.
I see, makes sense.
Didn't even know Window_Selectable existed xD

Thank you very much.
You'll learn a lot from reading the core scripts 😀

Original thread: https://forums.rpgmakerweb.com/threads/sethandler-error.87074/