[SOLVED] Change array value problem

Hello, i have this code that add an item to the inventory

            item.lootID=this.player.lootUID;
            this.player.inventory.push(item);
            //this.player.inventory[this.player.inventory.length-1].lootID=this.player.lootUID;
            this.player.lootUID++;
            this.player.gold -= item.value;

i have tried to change the 3rd line with the 1st because if i buy two of the same objects the lootID in the inventory array are changed in the last one and in the previous one as well, so instead having an unique ID in all the inventory objects i have doubles…that don’t happens if the items are in inventory from the start. Any idea why?
EDIT: inventory and lootUID are variables of player.js and the same thing happens also when picking up objecs whose code is pretty the same inside player.js i also noticed that when picking a new object from dbase.json the lootID value is already changed with the lootUID variable

Code looks fine. Is this.player.lootUID modified anywhere else in the code?

no it’s just a value that is incremented every time i add an item in inventory to give an unique id, also when i drop the item the lootUID is not touched, and is incremented as a item is picked up or bought. That’s why i’m so lost. I checked it putting tons of break in code and confusion just piled up. EXAMPLE: in inventory item [5] is Healing potion and has lootID=6 if i buy another one both [5] and [6] get lootID=7 also if the item is [5] and the new item is [9] with the same name both lootID get changed, but that not happens with the Dagger i have from start in inventory and i buy a new one, but if i buy 2 new ones the 2 new ones have the lootID changed but not the original one

Is the item object unique? ie. If you buy two potions, are they two different objects in memory? Or are they both reference the same template/database object?

It use the database to get x objects and build the shop archive when buy it get the item from shop and push it into the inventory if a ground object it get the item with the same name from database and push it into inventory and after it change the lootID as shown in the code above

So in this code, is item a reference to an entry in the database?

Yes there the item is the one in the shop archive that is built from database

var btype="";
                var now =new Date();
                if (this.templeInventory.length>0){
                    if(this.templeInventory[0].date+60000<now.getTime()) {
                        this.templeInventory=[];
                        this.templeInventory.push('[date:'+now.getTime()+']');
                        for (var i=0; i<6;i++) {
                            var obj=Math.floor(Math.random()*this.player.dataPotions.length);
                            this.templeInventory.push(this.player.dataPotions[obj]);
                        }
                    }
                } else {
                    this.templeInventory.push('[date:'+now.getTime()+']');
                    for (var i=0; i<6;i++) {
                        var obj=Math.floor(Math.random()*this.player.dataPotions.length);
                        this.templeInventory.push(this.player.dataPotions[obj]);
                    }
                }

populate shop archive

Without looking too deeply at it, if you are buying the same potion twice, they are both referencing the same object in memory so when you change a property like lootUID, it ‘looks’ like two items in your inventory have heard their UID changed.

When you add an item to the inventory, it needs to either be a clone or you create a new object that references the item in the database and has it’s own UID.

eg

this.player.inventory.push({item = item, uid = this.player.lootUID});
this.player.lootUID++;
this.player.gold -= item.value;

OH i see!!! now i get it @yaustar you have enlighten me :slight_smile: thanks a lot!!!

Solved this way

var nitem =JSON.parse(JSON.stringify(item));
            nitem.lootID=this.player.lootUID;
            this.player.inventory.push(nitem);