MZ $gameParty.allItems(); how to display the name of item desc, id etc. on window [SOLVED]

Hi again guys learning javascript here
Just doing some javascript training and I'm stuck on what I'm doing
basically I just want to call the $gameParty.allItems(); and display in the window in a text form
its name desc. id etc. I tried doing $gameParty.allItems($dataItems.name); but its an error it display
all data from the array but i just want to display the item name only how to do that?

thank you guys for the help

edit: I tried this one too
this.drawItemName(7, 0, 0, 300);

btw here's my code(sorry forgot to include)
JavaScript:
/** /*:
 * @target MZ
 *
 * @author name
 * @plugindesc hehhey
 *
 * @command open
 * @text Open Window
 *
 */

 ;
 (() => {
    PluginManager.registerCommand('customitem', 'open', args => {
        SceneManager.push(Scene_CustomItem);
    });
    class Window_CustomItem extends Window_Base {
        constructor(rect) {
            super(rect);
        }

        refresh() {
            super.update();
            this.contents.clear();
            this.make_draw_item();
        }

        make_draw_item() {
            this.drawItemName(7, 0, 0, 300);
        }
    }

    class Scene_CustomItem extends Scene_Base {
        constructor() {
            super();
       
            this.wci = new Window_CustomItem(new Rectangle(0, 0, 500, 416));
            this.addChild(this.wci);
        }

        refresh() {
            this.contents.clear();
        }
    }


}) ();
That's not close to correct syntax.
1 - $dataItems.name doesn't mean anything because dataItems is an array, so you need to specify which element of the array (in this case, the item ID) you want to display the name of.

2 - Even if you were referencing the array correctly, why would you try to pass it as an argument into allItems()?

It seems like you need to take a few steps back and review your training on syntax for arrays and functions before trying to jump into your game and implement new things. Also,

3 - Displaying in a window inside the game is actually really not simple. I don't know what you have for that code, but there's a fair amount of code to write your own window and scene and format its contents properly.

Aside from all of that...allItems() just returns an array, so you simply look at its elements like you would with any other. For example, $gameParty.allItems()[2].name

If you want to list things off for all of the items the party is carrying, then you need to iterate through the array and get the member variables you want for each array element. You could do that with a for loop, or with JavaScript's forEach() array method.
That's not close to correct syntax.
1 - $dataItems.name doesn't mean anything because dataItems is an array, so you need to specify which element of the array (in this case, the item ID) you want to display the name of.

2 - Even if you were referencing the array correctly, why would you try to pass it as an argument into allItems()?

It seems like you need to take a few steps back and review your training on syntax for arrays and functions before trying to jump into your game and implement new things. Also,

3 - Displaying in a window inside the game is actually really not simple. I don't know what you have for that code, but there's a fair amount of code to write your own window and scene and format its contents properly.

Aside from all of that...allItems() just returns an array, so you simply look at its elements like you would with any other. For example, $gameParty.allItems()[2].name

If you want to list things off for all of the items the party is carrying, then you need to iterate through the array and get the member variables you want for each array element. You could do that with a for loop, or with JavaScript's forEach() array method.

hi thanks for the reply

I saw this code in

rmmz_windows
JavaScript:
Window_Base.prototype.drawItemName = function(item, x, y, width) {
    if (item) {
        const iconY = y + (this.lineHeight() - ImageManager.iconHeight) / 2;
        const textMargin = ImageManager.iconWidth + 4;
        const itemWidth = Math.max(0, width - textMargin);
        this.resetTextColor();
        this.drawIcon(item.iconIndex, x, iconY);
        this.drawText(item.name, x + textMargin, y, itemWidth);
    }
};

its like drawCharacter or drawFace
i just have to call it like this.drawCharacter(blah,blahh); and it will display the face or the sprite in the window I thought i could do the same with drawItemName but it display in the undefined

1655617384075.png



here's the updated code:
Code:
/** /*:
 * @target MZ
 *
 * @author name
 * @plugindesc hehhey
 *
 * @command open
 * @text Open Window
 *
 */

 ;
 (() => {
    PluginManager.registerCommand('customitem', 'open', args => {
        SceneManager.push(Scene_CustomItem);
    });
    class Window_CustomItem extends Window_Base {
        constructor(rect) {
            super(rect);
            this.refresh();
        }

        refresh() {
            super.update();
            this.contents.clear();
            this.drawItemName($dataItems[1].name, 20, 20, 200);
        }

    }

    class Scene_CustomItem extends Scene_Base {
        constructor() {
            super();
        
            this.wci = new Window_CustomItem(new Rectangle(0, 0, 500, 416));
            this.addChild(this.wci);
        }

        refresh() {
            this.contents.clear();
        }
    }


}) ();
The first argument of drawItemName is a database item reference, i.e. $dataItems[1] in this case.

You may want to explain the purpose of this window? For example, if you want a selectable list of items, it could be better to inherit from Window_Selectable or Window_ItemList instead.
The first argument of drawItemName is a database item reference, i.e. $dataItems[1] in this case.

You may want to explain the purpose of this window? For example, if you want a selectable list of items, it could be better to inherit from Window_Selectable or Window_ItemList instead.
thanks it works I've tried to do a little test what I want to do is check if I have the item on my inventory
if I have I will display it using the
this.drawItemName($dataItems, 20, 20, 200);
I used for loop to do this here is the code
JavaScript:
 let i = 1;
            for (i = 1; $gameParty.hasItem($dataItems[i]) == false; i =+ 1) {
                this.drawItemName($dataItems[i], 20, 20, 200);
            }
but the game hang I think i mess up lol
It should be i += 1 at the end of the for loop.
It should be i += 1 at the end of the for loop.
still hang bro i'll post the complete code maybe i mess on different function
JavaScript:
/** /*:
 * @target MZ
 *
 * @author name
 * @plugindesc hehhey
 *
 * @command open
 * @text Open Window
 *
 */

 ;
 (() => {
    PluginManager.registerCommand('customitem', 'open', args => {
        SceneManager.push(Scene_CustomItem);
    });
    class Window_CustomItem extends Window_Base {
        constructor(rect) {
            super(rect);
            this.refresh();
        }

        refresh() {
            super.update();
            this.contents.clear();
            this.letsDrawItemFromInventory();
            
        }

        letsDrawItemFromInventory() {
            
            let i = 1;
            for (i = 1; $gameParty.hasItem($dataItems[i]) == false; i += 1) {
                this.drawItemName($dataItems[i], 20, 20, 200);
            }
        }

    }

    class Scene_CustomItem extends Scene_Base {
        constructor() {
            super();
        
            this.wci = new Window_CustomItem(new Rectangle(0, 0, 500, 416));
            this.addChild(this.wci);
        }

        refresh() {
            this.contents.clear();
        }
    }


}) ();
JavaScript:
            let i = 1;
            for (i = 1; $gameParty.hasItem($dataItems[i]) == false; i += 1) {
                this.drawItemName($dataItems[i], 20, 20, 200);
            }
This is incorrect. The second input of a for loop is the condition. The loop ends when the condition is false: in this case, that's when $gameParty.hasItem($dataItems[i]) is true (i.e. the party has at least one of that item).
Also, you tell it to draw the item name at the same coordinates (x = 20, y = 20, width 200) each time, which I imagine will cause a mess with multiple items.

what I want to do is check if I have the item on my inventory
The important question here is: which item?
thanks it works I've tried to do a little test what I want to do is check if I have the item on my inventory
if I have I will display it using the
this.drawItemName($dataItems, 20, 20, 200);
I used for loop to do this here is the code
JavaScript:
 let i = 1;
            for (i = 1; $gameParty.hasItem($dataItems[i]) == false; i =+ 1) {
                this.drawItemName($dataItems[i], 20, 20, 200);
            }
but the game hang I think i mess up lol
You might need to explain what you're trying to do in slightly more detail, because that's kind of a crazy loop, and your intentions aren't entirely clear to me. Setting aside the syntax error that NhatNguyen mentioned, you've got a few other issues as well.

Firstly, you don't need to declare the variable i outside of the for loop like that. Declare it inside the for loop instead, like so: for (let i = 1; blah blah; blah). Declaring it outside the loop like that isn't technically causing an issue in this particular situation, but it's an unnecessary step, and it could cause undesirable behavior in other situations.

Secondly, your condition in the for loop ($gameParty.hasItem($dataItems[i]) == false) is almost certainly incorrect. Your loop has the potential to execute 0 times, and it also has the potential to execute infinitely. But I can't really tell you what to set it as, since it's unclear to me exactly what you're trying to accomplish.

Thirdly, if you're trying to draw multiple items, you will have a problem, since you're trying to draw the text in the exact same place every time.

Since I feel bad not giving you *some* type of advice, I'm just going to pretend that what you're trying to do is loop through every possible item in the database, check if the player has it, and if so, then draw its name on a new line in the window. The code for that could look like this:
JavaScript:
var numLines = 0;
for (const item of $dataItems) {
    if ($gameParty.hasItem(item)) {
        this.drawItemName(item, 20, 20 + this.lineHeight() * numLines, 200);
        numLines++;
    }
}

Though, honestly, if you're just trying to display the party's inventory, then there is a better way to do that, without having to iterate through the entire database. Also, your window isn't scrollable, so you won't be able to see any of the item names that get drawn beyond the bounds of the window height.
You might need to explain what you're trying to do in slightly more detail, because that's kind of a crazy loop, and your intentions aren't entirely clear to me. Setting aside the syntax error that NhatNguyen mentioned, you've got a few other issues as well.

Firstly, you don't need to declare the variable i outside of the for loop like that. Declare it inside the for loop instead, like so: for (let i = 1; blah blah; blah). Declaring it outside the loop like that isn't technically causing an issue in this particular situation, but it's an unnecessary step, and it could cause undesirable behavior in other situations.

Secondly, your condition in the for loop ($gameParty.hasItem($dataItems[i]) == false) is almost certainly incorrect. Your loop has the potential to execute 0 times, and it also has the potential to execute infinitely. But I can't really tell you what to set it as, since it's unclear to me exactly what you're trying to accomplish.

Thirdly, if you're trying to draw multiple items, you will have a problem, since you're trying to draw the text in the exact same place every time.

Since I feel bad not giving you *some* type of advice, I'm just going to pretend that what you're trying to do is loop through every possible item in the database, check if the player has it, and if so, then draw its name on a new line in the window. The code for that could look like this:
JavaScript:
var numLines = 0;
for (const item of $dataItems) {
    if ($gameParty.hasItem(item)) {
        this.drawItemName(item, 20, 20 + this.lineHeight() * numLines, 200);
        numLines++;
    }
}

Though, honestly, if you're just trying to display the party's inventory, then there is a better way to do that, without having to iterate through the entire database. Also, your window isn't scrollable, so you won't be able to see any of the item names that get drawn beyond the bounds of the window height.
Hi, sorry for late replay.
Thank you so much for this, This is what actually I wanna do.
I just want to know or learn how the procedure of displaying all current data in a window for practice purpose, I can't figure out the logic of how to display all data that you posses but what I miss is the for of loop thank you for giving me the good.
Hi, sorry for late replay.
Thank you so much for this, This is what actually I wanna do.
I just want to know or learn how the procedure of displaying all current data in a window for practice purpose, I can't figure out the logic of how to display all data that you posses but what I miss is the for of loop thank you for giving me the good.
No problem. Just for reference, here is how you would do it without for..of:
JavaScript:
var numLines = 0;
for (let i = 1; i < $dataItems.length; i++) {
    if ($gameParty.hasItem($dataItems[i])) {
        this.drawItemName($dataItems[i], 20, 20 + this.lineHeight() * numLines, 200);
        numLines++;
    }
}

Original thread: https://forums.rpgmakerweb.com/threads/gameparty-allitems-how-to-display-the-name-of-item-desc-id-etc-on-window-solved.148682/