#need help with getItem()

1 messages · Page 1 of 1 (latest)

agile crest
#
function saveKit(target) {
    const equ = target.getComponent(`equipable`)
    const inv = target.getComponent(`inventory`)
    let i = 0
    for (i=0;i<9;i++) {
        let item = inv.container.getItem(i)
        console.warn(item.typeId, item.slot)
    }
}```
ive done everything right except for `item.slot` as i dont know what should go there, im trying to retrieve the slot index of the item.
`item.slot` came back as undefined.
rigid finch
#

You mean the position where the itemStack is? u just need put "i" at that

agile crest
rigid finch
agile crest
rigid finch
#

console.warn(item.typeId, i)

agile crest
agile crest
agile crest
rigid finch
#

Don't worry, sometimes Idk what i'm doing to 🙂

agile crest
#
function saveKit(target) {
    const equ = target.getComponent(`equipable`);
    const inv = target.getComponent(`inventory`);
    let i = 0;
    for (i=0;i<9;i++) {
        let item = inv.container.getItem(i);
        if (!item === undefined) {
            console.warn(item.typeId, i);
        } else console.warn("empty", i)
    };
};```
#

it makes everything go through the else dtatment

rigid finch
#

!(item ...) or item !== ... or !item

agile crest
agile crest
#

i got to an empty slot and it said this:

#

[Scripting][warning]-minecraft:totem_of_undying 0

[Scripting][warning]-minecraft:golden_apple 1

[Scripting][warning]-minecraft:glowstone 2

[Scripting][warning]-minecraft:ender_pearl 3

[Scripting][warning]-minecraft:obsidian 4

[Scripting][error]-TypeError: cannot read property 'typeId' of undefined at saveKit (index.js:433)
at <anonymous> (index.js:114)

#

new code:

#
function saveKit(target) {
    const equ = target.getComponent(`equipable`);
    const inv = target.getComponent(`inventory`);
    let i = 0;
    for (i=0;i<9;i++) {
        let item = inv.container.getItem(i);
        if (item.typeId !== undefined) {
            console.warn(item.typeId, i);
        } else console.warn("empty", i)
    };
};```
rigid finch
rigid finch
agile crest