addCommand function not passing ext argument

Here's the setup: I have a block of states (called "starsigns") that are meant to be applied as passives to actors and enemies. In a plugin I'm trying to set up a menu that the player can choose a starsign from for each actor. I want the menu to be able to change dynamically based on the size and content of the block of states, with me only needing to change a couple of parameters ("firstSign" and "lastSign") to tell the plugin where the block begins and ends.

To do this I'm using a for loop to create the menu options and I want to use the "ext" argument (of the addCommand function) to pass the index of the state each option refers to. Everything else is working as expected, but the ext value always ends up being null.

Here's the relevant bit of code:

Code:
function starsignMenu() {
    var window = Object.create(Window_Command.prototype);

    window.makeCommandList = function () {
        for (i = firstSign; i <= lastSign; i++) {
            window.addCommand($dataStates[i].name, "starsign" , true, i);
            alert(Window_Command.prototype.currentExt());
        }
    };

    window.initialize(0, 0);

    window.setHandler("starsign", function () {
        alert(Window_Command.prototype.currentExt());
    });

    return window;
};

The alerts are there for testing purposes and ALWAYS say "null" no matter what I feed into the addCommand function. Can someone tell me how to get the ext value?



EDIT: Okay, part of my problem is that I shouldn't be using "Window_Command.prototype.currentExt()" like that. But replacing it with "this.currentExt()" isn't working either. I'm still getting "null" for everything.
Prototype functions are only supposed to be used on an instance of the class. You are calling it on the class itself, not an I stance, and therefore it has no data. How would it know what window you want to get the data from if it is not called on a specific window?

One option is to bind the function to the window, and then use "this" to call the function on it.

Another, if you're interested in fancy new technology (web deployments might act weird on older browsers that don't support said new technology), you could use arrow functions.

Just Google around for Javascript arrow functions, or function binding, and you should be set.
One option is to bind the function to the window, and then use "this" to call the function on it.

I'll look up binding, but I don't follow the logic here. Wouldn't I need to attach the function to the individual options somehow rather than the window that contains them all? I need the ext value from the specific option the player selects. The window itself shouldn't even have an ext value unless I'm misunderstanding something.

EDIT: Also, since there's already a Window_Command.prototype.currentExt function, isn't this already built into the engine?
Sorry, I'm on my phone and didn't notice you try to access the current ext multiple times. I was referring to the function used in setHandler. You need to bind that function to "window" (Total side note, you should change the variable name, as window is technically a reserved word in js, and may be confusing to some people) so that you can access "this" in the handler function, where "this" refers to "window".

In the first example, you'd want to just use this.currentExt, not the whole prototype. But that also probably won't work, because adding a command doesn't change the selection, and currentExt works on the current selection.
I was referring to the function used in setHandler. You need to bind that function to "window" so that you can access "this" in the handler function, where "this" refers to "window".

Hang on... You mean bind the unnamed function that appears as the second argument of the setHandler function?


(Total side note, you should change the variable name, as window is technically a reserved word in js, and may be confusing to some people)

Done.


In the first example, you'd want to just use this.currentExt,

I guess you missed the edit in my first post? Yeah, I tried that and, as you say, it didn't work.
Sorry for the double post, but I just wanted to let anyone who was working on this know that they can stop now. The value I need is stored in "windowName._list[windowName._index].ext". Of course, I had to trace my way through a bunch of rpg_windows.js code to figure that out. I am not enjoying my time with javascript so far.

Thanks to anyone who was trying to help!
Here you can get the core files as splitted files, it really helps to find stuff
https://github.com/rpgtkoolmv/corescript/tree/master/js

Also if you are just starting with JS, check out that second link in my signature.🙂
I wanted to drop in and apologize for giving a bad example @DGM. While window is not a reserved word it is a bad name to use.

Anywho, all you have to do is call windowName.currentExt()to get that value in your handler.

Spoiler: Example
Code:
function customMenu() {
    "use strict";

    var myWin = Object.create(Window_Command.prototype);

    myWin.windowWidth = function () {
        return 200;
    };

    myWin.windowHeight = function () {
        return 400;
    };

    myWin.makeCommandList = function () {
        var i;

        for (i = 0; i < 10; i += 1) {
            myWin.addCommand("Option " + i, "symbol", true, "value" + i);
        }
    };

    myWin.initialize(0, 0);

    myWin.setHandler("symbol", function () {
        console.log("ExtValue: " + myWin.currentExt());
        myWin.activate();
    });

    myWin.setHandler("cancel", function () {
        SceneManager._scene.popScene();
    });
 
    return myWin;
}
I wanted to drop in and apologize for giving a bad example

Not a problem. And your sample code was very helpful.

Original thread: https://forums.rpgmakerweb.com/threads/addcommand-function-not-passing-ext-argument.79789/