Hello, i’m making the crafting weapons code and i have some mental confusion on a part of code, when i click the tool i want to forge have to check if all the resources needed are in inventory and if they are have to delete that resources. I had no problem since the resources was all with differents names but if i need 3 irons it became more complicated.
Here is the code:
self.recipe=item.id;
for (var i=0;i<self.player.inventory.length;i++) {
if (self.player.inventory[i].name===item.ingr1) {
var i1=i;
}
if (item.ingr2 && self.player.inventory[i].name===item.ingr2) {
var i2=i;
}
if (item.ingr3 && self.player.inventory[i].name===item.ingr3) {
var i3=i;
}
}
if (!i1) {app.root.findByName('Root').script.Output.write("You don't have "+ item.ingr1); return;}
if (!i2 && item.ingr2) {app.root.findByName('Root').script.Output.write("You don't have "+ item.ingr2); return;}
if (!i3 && item.ingr3) {app.root.findByName('Root').script.Output.write("You don't have "+ item.ingr3); return;}
app.root.findByName('Root').script.Output.write("You start forging "+ item.name);
With the code as it is if i have just 1 iron it pass the check…also if 3 irons are needed.
And after that i need to remove the resources from inventory so i need to sort i1-i2-i3 from higher to lower and splice them from inventory…i have some problem sorting them