MV How to grab the database item currently selected in the menu?

It seems like this function of Window_ItemList is responsible for returning the database item:

Code:
Window_ItemList.prototype.item = function() {
    var index = this.index();
    return this._data && index >= 0 ? this._data[index] : null;
};

However, when I invoke this function in my own script, it always returns null for some reason instead of returning the item that is currently selected in the menu... I don't understand why?
this._data is an empty array which is truthy, and the _index property shouldn't be -1 once the selectable window is already set up. Maybe someone can explain what I'm missing here
In what context are you evoking this? It would be helpful to see your script if thats okay. The most likely scenario that the window its being called on was deselected and the index was set to -1 but its hard to say without context.
I invoke it here:

Code:
Window_EquipStatus.prototype.item = function() {
    return Window_ItemList.prototype.item()
}

So you're saying since Window_EquipStatus is deselected the index goes to -1?
How can I avoid this?
I don't think that's correct syntax - it has no reference to the current object.

You'd want to do
Code:
Window_ItemList.prototype.item.call(this);
I don't think that's correct syntax - it has no reference to the current object.

You'd want to do
Code:
Window_ItemList.prototype.item.call(this);

I actually tried that first, but it also did not work for a different reason
When I used .call(this), the error said that "this.index() is not a function"

index() is inherited from Window_Selectable so I also don't get why that is happening either
Is Window_EquipStatus inheriting from Window_ItemList or Window_Selectable?
It sounds like you should probably just inherit from Window_ItemList, that way you won't need to change prototype.item at all since you don't have any extra logic there.

IE:
Code:
Window_EquipStatus.prototype = Object.create(Window_ItemList.prototype);
Is Window_EquipStatus inheriting from Window_ItemList or Window_Selectable?
Neither, apparently it inherits from Window_Base. Which is why:
When I used .call(this), the error said that "this.index() is not a function"
Because it isn't. Neither Window_EquipStatus nor Window_Base has an index() function. You can't just call methods from random other object types.

The EquipStatus window doesn't even have anything to do with what you're talking about in your original post - you're not selecting anything in that window. That's the window that shows what your parameters will look like if you equip that item.
Oh Window_EquipStatus is a core window oops, I assumed it was part of a plugin they were working on.

@AmVa you're probably trying to get the currently selected item in Scene_Equip then, right? From a script call that would be
Code:
SceneManager._scene._itemWindow.item()
You'll only need to change function prototypes if you're creating a new window type or editing the functionality of an existing one.
@AmVa you're probably trying to get the currently selected item in Scene_Equip then, right? From a script call that would be
Code:
SceneManager._scene._itemWindow.item()
You'll only need to change function prototypes if you're creating a new window type or editing the functionality of an existing one.

Yes, exactly!

SceneManager._scene._itemWindow.item() also returns null :/
Hmm, maybe you're looking at Window_EquipSlot then? In which case it would be
Code:
SceneManager._scene._slotWindow.item()
Hmm, maybe you're looking at Window_EquipSlot then? In which case it would be
Code:
SceneManager._scene._slotWindow.item()

Nope, it's definitely SceneManager._scene._itemWindow.item()

Window_EquipSlot is where you select your current equipped items, I'm referring to the items you select from your inventory to replace them.
Right, so if the item list is currently open and selected (your cursor is on it) and the item list has some items in it ( SceneManager._scene._itemWindow._data has some items in it ) then SceneManager._scene._itemWindow.item() should return the database entry of the current item. Are you testing by entering this into the console or are you calling it from somewhere else? If you're calling it from somewhere else maybe its happening before or after the item window is active?
Are you testing by entering this into the console or are you calling it from somewhere else? If you're calling it from somewhere else maybe its happening before or after the item window is active?

I'm calling it from somewhere else. This is the code that leads to its invocation:

Code:
Window_EquipStatus.prototype.refresh = function() {
    this.contents.clear();
    if (this._actor) {
        this.drawActorName(this._actor, this.textPadding(), 0);
        this.drawItem(0, this.lineHeight() * 1, 0);
        this.drawItem(0, this.lineHeight() * 2, 1);
        this.drawItem(0, this.lineHeight() * 3, 2);
        this.drawItem(0, this.lineHeight() * 4, 3);
        this.drawVariable(0, this.lineHeight() * 5, 34);
    }
};

Window_EquipStatus.prototype.drawVariable = function(x, y, id) {
    this.drawVarName(x + this.textPadding(), y, id);
    if (this._actor) {
        this.drawCurrentVarValue(x + 140, y, id);
    }
    this.drawRightArrow(x + 188, y);
    if (this._tempActor) {
        this.drawNewVarValue(x + 222, y, id);
    }
};

Window_EquipStatus.prototype.drawNewVarValue = function(x, y, id) {
    var item = this.item()
    var newValue = item.meta.buff
    console.log(item)
    var diffvalue = newValue - $gameVariables.value(id);
    this.changeTextColor(this.paramchangeTextColor(diffvalue));
    this.drawText(newValue, x, y, 48, 'right');
};

Window_EquipStatus.prototype.item = function() {
    return SceneManager._scene._itemWindow.item()
}

I belive that is indeed the problem, that the function is being called "too early" so the window is not active yet...
Right, if you take a look at Scene_Equip.prototype.onItemOk the Window_EquipStatus refresh is called after this._itemWindow.deselect(); so the item window index will always be -1.

It might help if you went into a bit more detail on what you're trying to do with this code.
Are you sure it's Scene_Equip.prototype.onItemOk that is causing the issue though?

I think the problem is with Scene_Equip.prototype.onSlotOk (when you click on an item that is already equipped in order to replace it)
.onItemOk is the one when you choose an item from the list, I can never get to this part because as soon as I click on the equipped item the game already crashes with the error that SceneManager._scene._itemWindow.item() returned null
Well the main problem is that your draw function can be called in contexts when the item window is not active. Without knowing exactly what you want to do, Id suggest having the item window "cache" the most recently selected item so that you can check for it even when the window is inactive.
Code:
myoverride__Window_ItemList_prototype_select = Window_ItemList.prototype.select;
Window_ItemList.prototype.select = function(index) {
    myoverride__Window_ItemList_prototype_select.call(this, index);
    this._latestItem = this.item();
}
Then
Code:
Window_EquipStatus.prototype.item = function() {
    return SceneManager._scene._itemWindow._latestItem;
}
I see what you mean, but not sure how to fix it.
I want Window_EquipStatus to draw info that is taken from an item notetag in the database, but in order to get the right notetag, first I need to grab the item in the database. I can't know which item it is without relying on Window_ItemList

I tried your solution but SceneManager._scene._itemWindow._latestItem still returns null...
I would add some defaulting in for cases where there are no items ie either:
Code:
Window_EquipStatus.prototype.item = function() {
    return SceneManager._scene._itemWindow._latestItem || { meta: {buff:0 } };
}
Or likely better changing drawNewVarVal and whereever else you're using item() to have some defaulting such as
Code:
var newValue = item ? item.meta.buff : 0;
Or likely better changing drawNewVarVal and whereever else you're using item() to have some defaulting such as
Code:
var newValue = item ? item.meta.buff : 0;

OMG this worked!!

I don't fully understand why lol but thank you so much 😀
Glad I could help. The reason is that there's likely a point where the equip_status window is refreshed before the item window has been called leading to that null error happening before the item window even selects something.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-grab-the-database-item-currently-selected-in-the-menu.156546/