MZ Displaying Item and Item Amount using Window_ItemList

Hello, I know this question is really specific but it has honestly been a pain in my back for a while.

I've made a Window to display Items that the player can interact with, the details aren't that important, these items should have values, similar to the Player's Inventory, which the player can effect, for example Health Potion : 1.

How would I
1. Make an Item as a value in _data within the Window
2. Give it a unique amount
There are two parts to this:
  1. Store the additional data
  2. Display the additional data
I'll write "additional data" as AD for short now~
  1. Storage​

    The AD can technically be stored anywhere you like, but some setups may be easier depending on how you have coded your window. For example, if your window inherits from Window_ItemList, then by default _data is expected to be an array of $dataItems references. For display reasons, you should be able to get from an item's index (i.e. position in the list) to its AD. Remember that the first item is index 0.

    A couple of ideas:
    • Attach the AD to the party's item information, e.g. via Game_Item.
    • Use a separate array for the AD and make sure it stays up-to-date with the inventory.

  2. Display​

    Once you have the data, you just need to tell the game how to show that data to the user. For example, here's the drawItem method for the default Window_ItemList:
    Spoiler: Window_ItemList#drawItem and related methods (rpg_windows.js)
    JavaScript:
    Window_ItemList.prototype.drawItem = function(index) {
        var item = this._data[index];
        if (item) {
            var numberWidth = this.numberWidth();
            var rect = this.itemRect(index);
            rect.width -= this.textPadding();
            this.changePaintOpacity(this.isEnabled(item));
            this.drawItemName(item, rect.x, rect.y, rect.width - numberWidth);
            this.drawItemNumber(item, rect.x, rect.y, rect.width);
            this.changePaintOpacity(1);
        }
    };
    
    Window_ItemList.prototype.isEnabled = function(item) {
        return $gameParty.canUse(item);
    };
    
    Window_Base.prototype.drawItemName = function(item, x, y, width) {
        width = width || 312;
        if (item) {
            var iconBoxWidth = Window_Base._iconWidth + 4;
            this.resetTextColor();
            this.drawIcon(item.iconIndex, x + 2, y + 2);
            this.drawText(item.name, x + iconBoxWidth, y, width - iconBoxWidth);
        }
    };
    
    Window_ItemList.prototype.drawItemNumber = function(item, x, y, width) {
        if (this.needsNumber()) {
            this.drawText(':', x, y, width - this.textWidth('00'), 'right');
            this.drawText($gameParty.numItems(item), x, y, width, 'right');
        }
    };
    
    Window_Base.prototype.drawIcon = function(iconIndex, x, y) {
        var bitmap = ImageManager.loadSystem('IconSet');
        var pw = Window_Base._iconWidth;
        var ph = Window_Base._iconHeight;
        var sx = iconIndex % 16 * pw;
        var sy = Math.floor(iconIndex / 16) * ph;
        this.contents.blt(bitmap, sx, sy, pw, ph, x, y);
    };
    
    Window_Base.prototype.drawText = function(text, x, y, maxWidth, align) {
        this.contents.drawText(text, x, y, maxWidth, this.lineHeight(), align);
    };
    You can see that it is passed an index, which allows it to get the data associated with the currently selected item, as well as the screen coordinates of the item in the list. It draws the item's name and quantity, faded out if the item is disabled (i.e. cannot be used). If you can get an item's AD from its index in the list, all that remains is deciding where to draw it.
Hope that helps! 🙏
There are two parts to this:
  1. Store the additional data
  2. Display the additional data
I'll write "additional data" as AD for short now~
  1. Storage​

    The AD can technically be stored anywhere you like, but some setups may be easier depending on how you have coded your window. For example, if your window inherits from Window_ItemList, then by default _data is expected to be an array of $dataItems references. For display reasons, you should be able to get from an item's index (i.e. position in the list) to its AD. Remember that the first item is index 0.

    A couple of ideas:
    • Attach the AD to the party's item information, e.g. via Game_Item.
    • Use a separate array for the AD and make sure it stays up-to-date with the inventory.

  2. Display​

    Once you have the data, you just need to tell the game how to show that data to the user. For example, here's the drawItem method for the default Window_ItemList:
    Spoiler: Window_ItemList#drawItem and related methods (rpg_windows.js)
    JavaScript:
    Window_ItemList.prototype.drawItem = function(index) {
        var item = this._data[index];
        if (item) {
            var numberWidth = this.numberWidth();
            var rect = this.itemRect(index);
            rect.width -= this.textPadding();
            this.changePaintOpacity(this.isEnabled(item));
            this.drawItemName(item, rect.x, rect.y, rect.width - numberWidth);
            this.drawItemNumber(item, rect.x, rect.y, rect.width);
            this.changePaintOpacity(1);
        }
    };
    
    Window_ItemList.prototype.isEnabled = function(item) {
        return $gameParty.canUse(item);
    };
    
    Window_Base.prototype.drawItemName = function(item, x, y, width) {
        width = width || 312;
        if (item) {
            var iconBoxWidth = Window_Base._iconWidth + 4;
            this.resetTextColor();
            this.drawIcon(item.iconIndex, x + 2, y + 2);
            this.drawText(item.name, x + iconBoxWidth, y, width - iconBoxWidth);
        }
    };
    
    Window_ItemList.prototype.drawItemNumber = function(item, x, y, width) {
        if (this.needsNumber()) {
            this.drawText(':', x, y, width - this.textWidth('00'), 'right');
            this.drawText($gameParty.numItems(item), x, y, width, 'right');
        }
    };
    
    Window_Base.prototype.drawIcon = function(iconIndex, x, y) {
        var bitmap = ImageManager.loadSystem('IconSet');
        var pw = Window_Base._iconWidth;
        var ph = Window_Base._iconHeight;
        var sx = iconIndex % 16 * pw;
        var sy = Math.floor(iconIndex / 16) * ph;
        this.contents.blt(bitmap, sx, sy, pw, ph, x, y);
    };
    
    Window_Base.prototype.drawText = function(text, x, y, maxWidth, align) {
        this.contents.drawText(text, x, y, maxWidth, this.lineHeight(), align);
    };
    You can see that it is passed an index, which allows it to get the data associated with the currently selected item, as well as the screen coordinates of the item in the list. It draws the item's name and quantity, faded out if the item is disabled (i.e. cannot be used). If you can get an item's AD from its index in the list, all that remains is deciding where to draw it.

First of all I just want to say thank you, this has been the problem of a few late nights and its great to see someone put in the time to explain the topic in detail.

I'll probably go with setting the values in a different array, I've already tried using $dataItems, but it can make my code hard to read, and even harder to reset if you want to change values.

I think the line of code:

this.drawText($gameParty.numItems(item), x, y, width, 'right');

sets up the players items, but how would I get the index, would I have to send the value a previous function?
For clarity: drawItem is called once per item in the list. It draws the appropriate information for a single item...in this case, its icon & name, as well as the number of that item available. It's used to redraw visible items whenever the window refreshes, e.g. when the list is scrolled. 👋

I think the line of code:

this.drawText($gameParty.numItems(item), x, y, width, 'right');

sets up the players items, but how would I get the index, would I have to send the value a previous function?
The line of code you've cited says "get the number of item in the party's inventory, then draw that number at the specified coordinates; if the text is wider than the given value, squash up the text until it fits". The item name is drawn elsewhere. I assume you'll want to change the text that's being drawn, perhaps the other values as well.

index is passed to the drawItem method as its first argument, so you can reference it the same as a local variable within that method. If you want to access the index in another method/function, you can pass the value over, directly or otherwise. For instance, Window_ItemList uses the index to get the corresponding item reference:
JavaScript:
var item = this._data[index];
That item reference is then passed to the methods for drawing the name/quantity, e.g.
JavaScript:
this.drawItemNumber(item, rect.x, rect.y, rect.width);

(Quick note: technically the code I cited in my original post was for MV... I did a quick check just now, though, and the MZ code seems to be basically the same there, so no worries~)
For clarity: drawItem is called once per item in the list. It draws the appropriate information for a single item...in this case, its icon & name, as well as the number of that item available. It's used to redraw visible items whenever the window refreshes, e.g. when the list is scrolled. 👋


The line of code you've cited says "get the number of item in the party's inventory, then draw that number at the specified coordinates; if the text is wider than the given value, squash up the text until it fits". The item name is drawn elsewhere. I assume you'll want to change the text that's being drawn, perhaps the other values as well.

index is passed to the drawItem method as its first argument, so you can reference it the same as a local variable within that method. If you want to access the index in another method/function, you can pass the value over, directly or otherwise. For instance, Window_ItemList uses the index to get the corresponding item reference:
JavaScript:
var item = this._data[index];
That item reference is then passed to the methods for drawing the name/quantity, e.g.
JavaScript:
this.drawItemNumber(item, rect.x, rect.y, rect.width);

(Quick note: technically the code I cited in my original post was for MV... I did a quick check just now, though, and the MZ code seems to be basically the same there, so no worries~)
sorry about the late reply, I've managed to display it to mirror the player inventory overall and I can now add stuff to it through _data, the details you have provided have been very helpful thanks again!

Original thread: https://forums.rpgmakerweb.com/threads/displaying-item-and-item-amount-using-window_itemlist.133472/