Please specify version and format if asking for help, or apply optional tags above:
Twine Version:2.3.13
Story Format: 2.34.1
I’m back with another question about arrays and a dumb piece of code that I cannot figure out. I’m so sorry for the frequent questions. Y’all have been beyond helpful so far.
I’m trying to have it read out the array and then give the option to buy the item for each thing in the array. I can get it to read out the contents, and take away the right amount of money and even delete the item when the .stock hits 0.
However, some items have more than one in stock (see properties) and I’ve got it so that when the link is clicked the number in stock decreases but I can’t for the life of me get it to then add a copy of that item ONCE to the player’s array ($pcarr) and then ONLY change the value of .number after that. I currently have it going through using .includes but it’s giving me clones instead of just adding to the number. I wondered if someone could enlighten me, I feel like I’m staring it in the face and can’t figure it out.
I’m using the prototype setup.VarUtils provided by the lovey HiEv if that’s of use.
::Setup Passage::
<<set $pcarr to []>>
You enter a shop and see a table with three items.
<<set $money to 100>>
<<set $pouch to {
name: "A Strange Pouch",
description: "A small leather pouch with gold braided string as the cinch draw.",
category: "Utility",
value: 20,
cansell: true,
number: 0,
stock: 3,
}>>
<<set $wolf to {
name: "A Glass Wolf",
description: "A glass figurine of a wolf. It's teeth are so sharp you have to squint to see where the glass stops.",
category: "Cursed object",
value: 40,
cansell: true,
number: 0,
stock: 1,
}>>
<<set $mug to {
name: "A Stained Mug",
description: "A ceramic mug with an opalescent gleam on the outside and a myriad of rainbow stains on the inside.",
category: "Blessed object",
value: 70,
cansell: true,
number: 0,
stock: 1,
}>>
<<set $shoparr to []>>
<<set $shoparr.push($pouch, $wolf, $mug)>>
::Item Shop::
You have $money g remaining. What item would you like to buy?
TEST ITEMS To SELL
<<nobr>><<for _i to 0; _i lt $shoparr.length; _i++>>
<<set _item to $shoparr[_i]>>
<<capture _item>>
<p><big><<print $shoparr[_i].name>></big></p>
<li>Description: <<print $shoparr[_i].description>></li>
<li>Category: <<print $shoparr[_i].category>></li>
<li>Value: <<print $shoparr[_i].value>>g</li>
<li>Stock: <<print $shoparr[_i].stock>></li>
<<if $money lt $shoparr[_i].value>>You don't have enough gold for this<<else>><<if $shoparr[_i].cansell is false>><<else>>
<<link "Buy Item" 'Actual Shop 3'>>
<<set _j to $shoparr.indexOfObject(_item)>>
<<set $money -= $shoparr[_j].value>>
<<set $shoparr[_j].stock-->>
**//This is the part where it's supposed to take the item and add it to your invintory and add to the .number if it's already there. It seems to only be reading from the else statement though//**
<<set _pcitem to clone(_item)>>
<<if $pcarr.includes(_pcitem)>>
<<set _a to $pcarr.indexOfObject(_pcitem)>>
<<set $pcarr[_a].number++>>
<<else>>
<<set $pcarr.push(_pcitem)>>
<<set _a to $pcarr.indexOfObject(_pcitem)>>
<<set $pcarr[_a].number++>>
<<endif>>
<<if $shoparr[_j].stock eq 0>><<run $shoparr.delete(_item)>><<else>><<endif>>
<</link>>
<<endif>><<endif>>
<</capture>>
<</for>>
<</nobr>>
TEST PC ARRAY INVINTORY READOUT
<<nobr>><<for _i to 0; _i lt $pcarr.length; _i++>>
<<set _item to $pcarr[_i]>>
<<capture _item>>
<p><big><<print $pcarr[_i].name>></big></p>
<li>Description: <<print $pcarr[_i].description>></li>
<li>Category: <<print $pcarr[_i].category>></li>
<li>Value: <<print $pcarr[_i].value>>g</li>
<li>Number: <<print $pcarr[_i].number>></li>
<<if $pcarr[_i].cansell is true>><<else>>This item cannot be sold.<<endif>>
<</capture>>
<</for>>
<</nobr>>