Adding items to the inventory

So I’ve posted here a few times asking about building an inventory. From the help of someone here @Milton I was able to remove items from the database, but it also changed the way I manage items.

this.items['Potion'] = { quantity: 2}

How would it be possible to set the addItem function from this point on?. Because of the change of code I’m unsure how to set this up correctly.

addItem(item) {
      let existingKey = Object.keys(this.items).find(
        (item) => this.items[item].name === item.name
      );
      // let existingKey = Object.keys(this.items).find((item) => this.items[newItem] === item)
      // console.log(this.items)
        console.log(existingKey)
      if (existingKey) {
      //   console.log("key exists");
      this.items[item].quantity = this.items[item].quantity + 1;
        
        // this.items[item].quantity += item.quantity;
      } else {
        return this.items[item]
      }

      // if (this.items[item].quantity > 1) {
        // this.items[item].quantity = this.items[item].quantity + 1;
      // } else {
        // this.items.push(item)
      // }    
    }

This code doesn’t work but thought I’d post it here anyway. Heres a link to my latest code pen test.

Any help would be much appreciated!!!

Winston

Hey Winston :slight_smile:

    if (this.items[item]) {
        this.items[item].quantity = this.items[item].quantity + 1;
    } else {
        this.items[item] = { quantity: 1 };
    }
1 Like

Thanks once again!!. I change the this.items[item].quantity + 1 to this.items[item].quantity + quantity; passing a variable so I can set how many items I want to add.

Btw, looking at your codepen, you’re still doing it wrong :slight_smile:
Ask @rexrainbow how to update his widgets, because just calling add on each refresh is probably not the way…

1 Like

The latest minify file of rexUI is for phaser3.55.2, please update the phaser3 link to https://cdn.jsdelivr.net/npm/phaser@3.55.2/dist/phaser.min.js

1 Like

Ah yip, should be working now. Thanks again. @Milton @rexrainbow