OldSailor
Newbie
- Sep 11, 2021
- 60
- 21
Help me, I'm losing my mind.
I'm coding an HTML-based game using Tweego on a SugarCube framework. I wanted to create a simple inventory-store and buy-sell system for my game. What should have been a very basic feature turned into a huge mess, and even though I spent the entire day on this damn code, I still couldn't make a properly working buy-sell system.
The best I managed to do was a messy piece of code that allowed the user to buy and sell items from the store. But guess what happened? No matter which item you bought from the damn store, the code always assumed it was the last item. So basically, you could only buy one single item from the store. The more I tried to fix it, the messier it got, and in the end, I had to trash the entire code and revert back to my original backup.
I'm asking for help from the generous developers here. What I need from you is a buy-sell system that works seamlessly with my inventory system. I need to be able to buy products from the store and sell items from my inventory. If you can do this, I will be REALLY GRATEFUL and I will add credits for you in my game and let you know.
P.S. My inventory system seems to be working fine for now.
Inventory passage:
StoryInit:
I'm coding an HTML-based game using Tweego on a SugarCube framework. I wanted to create a simple inventory-store and buy-sell system for my game. What should have been a very basic feature turned into a huge mess, and even though I spent the entire day on this damn code, I still couldn't make a properly working buy-sell system.
The best I managed to do was a messy piece of code that allowed the user to buy and sell items from the store. But guess what happened? No matter which item you bought from the damn store, the code always assumed it was the last item. So basically, you could only buy one single item from the store. The more I tried to fix it, the messier it got, and in the end, I had to trash the entire code and revert back to my original backup.
I'm asking for help from the generous developers here. What I need from you is a buy-sell system that works seamlessly with my inventory system. I need to be able to buy products from the store and sell items from my inventory. If you can do this, I will be REALLY GRATEFUL and I will add credits for you in my game and let you know.
P.S. My inventory system seems to be working fine for now.
Inventory passage:
Code:
:: Inventory
<h2>Inventory</h2>
<<if $inventory.length > 0>>
<div style="display:flex; flex-wrap:wrap; gap:15px;">
<<for _item range $inventory>>
<div style="width:180px; border:1px solid #555; border-radius:10px; padding:10px; text-align:center; background-color:#1a1a1a; color:white;">
<<= '<img src="' + _item.image + '" style="width:128px; height:128px; object-fit:contain; border-radius:5px; border:1px solid #444;">' >>
<h3><<=_item.name>></h3>
<p><<=_item.desc>></p>
<p><b>Quantity:</b> <<=_item.quantity>></p>
</div>
<</for>>
</div>
<<else>>
<p>Your inventory is empty.</p>
<</if>>
<<script>>
window.setTimeout(() => {
for(let i = 0; i < State.variables.inventory.length; i++) {
var img = document.createElement('img');
img.style.width = '128px';
img.style.height = '128px';
img.style.objectFit = 'contain';
img.style.borderRadius = '5px';
img.style.border = '1px solid #444';
img.src = State.variables.inventory[i].image;
var container = document.getElementById("img-container-" + i);
if(container) {
container.appendChild(img);
}
}
}, 0);
<</script>>
Code:
<<script>>
/* Inventory system variable */
if (!State.variables.inventory) {
State.variables.inventory = [];
}
/* Item adding function */
setup.addItem = function(name, image, desc, quantity = 1) {
const inv = State.variables.inventory;
const existingItem = inv.find(it => it.name === name);
if (existingItem) {
existingItem.quantity += quantity;
} else {
inv.push({
name: name,
image: image,
desc: desc,
quantity: quantity
});
}
};
/* Item delete function */
setup.removeItem = function(name, quantity = 1) {
const inv = State.variables.inventory;
const index = inv.findIndex(it => it.name === name);
if (index !== -1) {
inv[index].quantity -= quantity;
if (inv[index].quantity <= 0) {
inv.splice(index, 1);
}
}
};
/* Starting items for testing purposes */
setup.addItem("Gun", "images/items/gun.png", "A fucking gun.", 1);
setup.addItem("Water Bottle", "images/items/water.png", "Yeah. Usual, water.", 3);
setup.addItem("Radio", "images/items/radio.png", "Bzzt. Bzzt.", 1);
<</script>>