MZ About index in Window classes

In Window_MenuStatus, it uses actor(index) to get data of the game party member at that index.

JavaScript:
Window_MenuStatus.prototype.actor = function(index) {
    return $gameParty.members()[index];
};

Window_MenuStatus.prototype.processOk = function() {
    Window_StatusBase.prototype.processOk.call(this);
    const actor = this.actor(this.index());
    $gameParty.setMenuActor(actor);
};

Window_MenuStatus.prototype.isCurrentItemEnabled = function() {
    if (this._formationMode) {
        const actor = this.actor(this.index());
        return actor && actor.isFormationChangeOk();
    } else {
        return true;
    }
};

But I don't understand this part, why the index is init as -1. And when the prototype.drawItem(index) run, the index will be like: 0, 1, 2, 3, 4... depends on the value that prototype.maxItems returns, could you tell me more about this?

JavaScript:
Window_Selectable.prototype.initialize = function(rect) {
    Window_Scrollable.prototype.initialize.call(this, rect);
    this._index = -1;
}

Window_Selectable.prototype.index = function() {
    return this._index;
};
-1 is a commonly-used value in selectable lists to denote "nothing is selected". The usual reason for this when working with arrays is that 0 is also a valid index in an array. If they initialised the value at 0, you'd always have to at least have the first window option selected and there'd be no way to completely clear the selection.

drawItem is a function that gets called in a loop, so yeah, it'll start at 0 and go up to whatever maxItems is.
-1 is a commonly-used value in selectable lists to denote "nothing is selected". The usual reason for this when working with arrays is that 0 is also a valid index in an array. If they initialised the value at 0, you'd always have to at least have the first window option selected and there'd be no way to completely clear the selection.

drawItem is a function that gets called in a loop, so yeah, it'll start at 0 and go up to whatever maxItems is.
Thank you, it all makes sense. May I ask another question?

I'd like to know where's the originality of index, as for now I feel like it come from nowhere, when a scene create a window, it doesn't pass any index parameter. And the index of drawItem function is another thing, or it overwrites the original index? This part is so confusing @.@.

I saw index in the Window_Base class, but I'm not sure what it is?

JavaScript:
Window_Base.prototype.createTextState = function(text, x, y, width) {
    const rtl = Utils.containsArabic(text);
    const textState = {};
    textState.text = this.convertEscapeCharacters(text);
    textState.index = 0;
   
Window_Base.prototype.processCharacter = function(textState) {
    const c = textState.text[textState.index++];
    if (c.charCodeAt(0) < 0x20) {
        this.flushTextState(textState);
        this.processControlCharacter(textState, c);
    } else {
        textState.buffer += c;
    }
};
I saw index in the Window_Base class, but I'm not sure what it is?

JavaScript:
Window_Base.prototype.createTextState = function(text, x, y, width) {
const rtl = Utils.containsArabic(text);
const textState = {};
textState.text = this.convertEscapeCharacters(text);
textState.index = 0;

Window_Base.prototype.processCharacter = function(textState) {
const c = textState.text[textState.index++];
if (c.charCodeAt(0) < 0x20) {
this.flushTextState(textState);
this.processControlCharacter(textState, c);
} else {
textState.buffer += c;
}
};
"index" here does not have the "this" keyword next to it. That means it is an entirely different thing than "this._index".

When a variable has "this" on its left, it means that it lives for the whole object; in this case, it is a Window object.

When a variable has something different on its left, like here, "textState" then it means it is bound to "textState".

And "textState" doesn't have "this" or a name on its left, but "const". That means it lives only inside the function, and after the function is completed, it is abandoned.

I think "createTextState" and "processCharacter" are some utility functions you don't need to fully understand as a beginner.
Thank you, it all makes sense. May I ask another question?

I'd like to know where's the originality of index, as for now I feel like it come from nowhere, when a scene create a window, it doesn't pass any index parameter. And the index of drawItem function is another thing, or it overwrites the original index? This part is so confusing @.@.

I saw index in the Window_Base class, but I'm not sure what it is?

JavaScript:
Window_Base.prototype.createTextState = function(text, x, y, width) {
    const rtl = Utils.containsArabic(text);
    const textState = {};
    textState.text = this.convertEscapeCharacters(text);
    textState.index = 0;
 
Window_Base.prototype.processCharacter = function(textState) {
    const c = textState.text[textState.index++];
    if (c.charCodeAt(0) < 0x20) {
        this.flushTextState(textState);
        this.processControlCharacter(textState, c);
    } else {
        textState.buffer += c;
    }
};
Okay, so the index *property* in scene windows is part of the window itself, but I think you're getting a bit confused because it's used differently in different places. There's a _index property, which is what index() returns, but there's also places where "index" is passed as a parameter to a function, and then it exists only within that function and is not the same thing as _index.

Basically how it works is that _index stores the currently-selected item, and it usually defaults to -1. The select function takes index as a parameter, which is pretty much always whatever item is about to be selected, and it sets _index to that value.

drawItem doesn't quite work the same way as the selection: it loops through each item and uses index to determine what rectangular area to draw it in, and what data to display there.
"index" here does not have the "this" keyword next to it. That means it is an entirely different thing than "this._index".

When a variable has "this" on its left, it means that it lives for the whole object; in this case, it is a Window object.

When a variable has something different on its left, like here, "textState" then it means it is bound to "textState".

And "textState" doesn't have "this" or a name on its left, but "const". That means it lives only inside the function, and after the function is completed, it is abandoned.

I think "createTextState" and "processCharacter" are some utility functions you don't need to fully understand as a beginner.
Okay, so the index *property* in scene windows is part of the window itself, but I think you're getting a bit confused because it's used differently in different places. There's a _index property, which is what index() returns, but there's also places where "index" is passed as a parameter to a function, and then it exists only within that function and is not the same thing as _index.

Basically how it works is that _index stores the currently-selected item, and it usually defaults to -1. The select function takes index as a parameter, which is pretty much always whatever item is about to be selected, and it sets _index to that value.

drawItem doesn't quite work the same way as the selection: it loops through each item and uses index to determine what rectangular area to draw it in, and what data to display there.
Thank you, I've learned a lot 😀

Original thread: https://forums.rpgmakerweb.com/threads/about-index-in-window-classes.155417/