Hello everyone,
I'm quite new to twine, coding and forums in general so please excuse any obnoxious ignorance.
I'm currently trying to make an inventory system with Sugarcube 2.11. Using examples created by others (most of it from this discussion), I've managed to make a system that allows me to relatively easily pick-up, buy and sell items from my inventory. An important part of this is the ability to pick up multiples of an item and having them stack.
My problem arises around the amount of a certain item I have. I will use potatoes as an example. I can successfully buy as many potatoes as necessary using the <<addToConsumables>> macro. I can then also sell these potatoes using the <<removeSingleConsumables>> macro. However, if I sell all the potatoes and then try buying new ones, the amount of potatoes I have does not start from 0. Instead it seems to start from the last maximum amount I bought in one go.
I imagine that I am incorrectly controlling the "amount" of an item I have in my inventory. Specifically when this amount goes to 0 and then back up.
My question is therefore how can I fix the following javascript to allow me to sell/remove items from my inventory while properly updating the amount I currently have. Thanks for any help on this matter.
<<set $potatoes ={
id: "potatoes",
amount: 1,
cost: 2,
heal: 2,
}>>
Inventory System Javascript:
window.getConsumables = function () {
return State.variables.Consumables;
};
Macro.add([ "initConsumables", "emptyConsumables" ], {
handler : function () {
State.variables.Consumables = [];
State.variables.Consumables.findByID = function(obj) {
if (this.length === 0) {
return false;
}
if (typeof obj !== "object" || !obj.hasOwnProperty("id")) {
return false;
}
var idx = this.findIndex(function (item) {
return item.id === obj.id;
});
return (idx !== -1);
};
}
});
Macro.add("addToConsumables", {
handler : function () {
if (this.args.length === 0) {
return this.error("no Consumables item specified");
}
var obj = this.args[0];
if (typeof obj !== "object" || !obj.hasOwnProperty("id")) {
return this.error("Consumables item malformed");
}
var idx = State.variables.Consumables.findIndex(function (item) {
return item.id === obj.id;
});
if (idx === -1) {
State.variables.Consumables.push(obj);
}
else {
State.variables.Consumables[idx].amount++;
}
}
});
Macro.add("removeSingleConsumables", {
handler : function () {
if (this.args.length === 0) {
return this.error("no Consumables item specified");
}
var obj = this.args[0];
if (typeof obj !== "object" || !obj.hasOwnProperty("id")) {
return this.error("Consumables item malformed");
}
var idx = State.variables.Consumables.findIndex(function (item) {
return item.id === obj.id;
});
if (idx !== -1) {
if(State.variables.Consumables[idx].amount > 1){
State.variables.Consumables[idx].amount--;
}
else if (State.variables.Consumables[idx].amount == 1) {
State.variables.Consumables.splice(idx , 1);
}}
}
});
Macro.add("removeFromConsumables", {
handler : function () {
if (this.args.length === 0) {
return this.error("no Consumables item specified");
}
var obj = this.args[0];
if (typeof obj !== "object" || !obj.hasOwnProperty("id")) {
return this.error("Consumables item malformed");
}
var idx = State.variables.Consumables.findIndex(function (item) {
return item.id === obj.id;
});
if (idx !== -1) {
State.variables.Consumables.splice(idx , 1);
}
}
});
Macro.add("Consumables", {
handler : function () {
if (State.variables.Consumables.length === 0) {
new Wikifier(this.output, 'nothing');
} else {
new Wikifier(
this.output,
'[[' + State.variables.Consumables.map(function (item) {
return item.id;
}).join(']]<br>[[') + ']]');
}
}
});
Macro.add('ConsumablesWithLinks', {
handler : function () {
var $list = $(document.createDocumentFragment());
if (State.variables.Consumables.length === 0) {
$list.text('nothing');
}
else {
State.variables.Consumables.forEach(function (item) {
$list
.wiki('[[' + item.id + ']]')
.append('\u00A0\u00D7' + item.amount)
.append('<br>');
});
}
$list.appendTo(this.output);
}
});