Sugarcube 2.29 Twine Inventory help

Somehow, the inventory in my game is being erased or reset to the values in the StoryInit every time I go to the following passage:

Fight Enemy
<>
<<set _rn to either (1,2)>>

<<if _rn === 1>>
<<set $enemyName to either(“Bowtruckle”,“Flobberworm”,“Billywig”,“Fox”,“Wolf”, “Hinkypunk”)>>
<<set $enemyHealth to either(10,30)>>
A $enemyName emerges from the woods!
<<include [[Enemy Hits You]]>>

<>
<>
You found _findHerb!
<<if _findHerb == “Dittany”>>
<<include [[Find Dittany]]>>
<<elseif _findHerb == “Bitter Root”>>
<<include [[Find Bitter Root]]>>
<<elseif _findHerb == “Asphodel”>>
<<include [[Find Asphodel]]>>
<<elseif _findHerb == “Wormwood”>>
<<include [[Find Wormwood]]>>
<>
<<include [[Forest]]>>
<>
<>

If someone finds an herb, it takes them to one of two types of passages (I was doing both types to see if they were the source of the problem, but it didn’t make a difference whether I used strings or objects)

Find Asphodel
<<set $inventory.asphodel.quantity += 1>>
<<if def $inventory.asphodel>>
You have $inventory.asphodel.quantity $inventory.asphodel.name.
<>
<<include [[Fight Enemy]] >>

Find Dittany
<<set $dittany = true>>
<<set $dittanyamt to $dittanyamt+1>>
<<if $dittanyamt === 1>>
You now have $dittanyamt Dittany leaf!
<>
You now have $dittanyamt Dittany leaves!
<>

Both of these passages work just fine for adding to the inventory, which is a basic system since I’m a beginning coder. The inventory seems to work just fine if I go to any other passage, it’s just something about the first passage that resets the inventory to the default.

StoryInit
<<set $dittany = false>>
<<set $dittanyamt = 0>>

<<set $inventory.asphodel = {name: “Asphodel”, quantity: 1}>>

1 Like

You’ll need to put your code inside of “preformatted text” markers (select a blank line either with your mouse or by doing SHIFT+DOWNARROW, and then click the </> button), otherwise the forum eats parts of it (notice all of the “<>” bits above).