MZ Code help (Why window isn't appearing)

Hello!

Can someone please help me understand why, when I run this plugin, the window doesn't appear during battle? The idea is that this is a plugin with a note tag of <HPInput> that goes into a skill should then, in theory, cause a window to pop up when the skill is used, that lets the user input an amount of their HP they'd like to use to determine their skill's damage. But when I run it, the skill does some damage, but the window is never popping up 🙁

JavaScript:
(() => {



let hpInputValue = 0;



const _Scene_Battle_onSkillOk = Scene_Battle.prototype.onSkillOk;

Scene_Battle.prototype.onSkillOk = function() {

    const skill = BattleManager.inputtingAction().item();



    if (skill && skill.meta.HPInput) {

        this.openHPInputWindow();

    } else {

        _Scene_Battle_onSkillOk.call(this);

    }

};



// Create input window

Scene_Battle.prototype.openHPInputWindow = function() {

    this._hpInputWindow = new Window_NumberInput(new Rectangle(0, 0, 240, 120));

    this._hpInputWindow.x = Graphics.boxWidth / 2 - 120;

    this._hpInputWindow.y = Graphics.boxHeight / 2 - 60;



    this._hpInputWindow.start();



    this._hpInputWindow.setHandler("ok", () => {

        hpInputValue = $gameVariables.value(1);



        const actor = BattleManager.actor();

        const maxSpend = actor.hp - 1;



        if (hpInputValue > maxSpend) {

            hpInputValue = maxSpend;

        }



        actor.gainHp(-hpInputValue);



        this.removeChild(this._hpInputWindow);

        _Scene_Battle_onSkillOk.call(this);

    });



    this._hpInputWindow.setHandler("cancel", () => {

        this.removeChild(this._hpInputWindow);

    });



    this.addChild(this._hpInputWindow);

};



// Use in damage formula of skill

window.getHPSpent = function() {

    return hpInputValue;

};



})();

Thank you!
When you're posting code, instead of using a quote tag, you should put it in the Code tags (the </> on the edit bar). That will preserve indentation and braces and make it more readable.

As it is, it's not immediately clear which things go where - although it is clear that you start off by declaring a global (to your plugin) variable, and I don't see why you'd need that. You could make it be a property of the Scene_Battle, or of the window like you do with your getHPSpent() function...it's all a bit haphazard.

Other things I notice...
- You're creating the window incorrectly. new Window_NumberInput() doesn't take a rectangle (or anything) as an argument.

- It's not correct practice to try to create and destroy the window every time it's used. It's more correct to create it as a part of the scene, then show and hide it at the appropriate times (see Scene_Battle.createAllWindows() ).

- Why are you setting hpInputValue equal to game variable 1? The number window will try to use variable 0 (which doesn't exist) unless $gameMessage._numInputVariableId is set (which I don't see you do in your code).

My first guess would be nothing is showing up because you're trying to call the window's start() method before you actually add it as a child to the scene. Someone else might be willing to comb through it in greater depth.
Didn't really think through the rest of the code, but about the very start: the onSkillOk sets the inputting action. But you're checking the inputting action skill before it has even been set. At least I think so.

EDIT: To be clear the base code onSkillOk has the code to set the inputting action
When you're posting code, instead of using a quote tag, you should put it in the Code tags (the </> on the edit bar). That will preserve indentation and braces and make it more readable.

As it is, it's not immediately clear which things go where - although it is clear that you start off by declaring a global (to your plugin) variable, and I don't see why you'd need that. You could make it be a property of the Scene_Battle, or of the window like you do with your getHPSpent() function...it's all a bit haphazard.

Other things I notice...
- You're creating the window incorrectly. new Window_NumberInput() doesn't take a rectangle (or anything) as an argument.

- It's not correct practice to try to create and destroy the window every time it's used. It's more correct to create it as a part of the scene, then show and hide it at the appropriate times (see Scene_Battle.createAllWindows() ).

- Why are you setting hpInputValue equal to game variable 1? The number window will try to use variable 0 (which doesn't exist) unless $gameMessage._numInputVariableId is set (which I don't see you do in your code).

My first guess would be nothing is showing up because you're trying to call the window's start() method before you actually add it as a child to the scene. Someone else might be willing to comb through it in greater depth.

Thank you for all the input! (I've also edited my post to use a code tag instead. Thanks)

I'll review this and try to improve it.

Original thread: https://forums.rpgmakerweb.com/threads/code-help-why-window-isnt-appearing.183375/