0 votes
asked by (150 points)
Basically, in my game, when you get an item, it goes into your inventory. You can check your inventory and it'll be there. However, if you check your inventory again in any other time, if the item is still there, you will get a duplicate. For example: At one point, you get a stone. If you check your inventory, it will say: "Your inventory contains a stone." If you check again, it will say: "Your inventory contains a stone, stone." And it continues to multiply. Even if you get another item, it will happen again. It's ridiculous! The inventory quickly becomes filled up with duplicate items and it makes me question if I should even let the player see what they have in their inventory.

Could someone help?

1 Answer

0 votes
answered by (60.4k points)
selected by
Best answer

I imagine you're doing something like this: 

  • In a header/footer, there's a link to an inventory passage. 
  • The inventory passage returns to the last passage, either by going to (history:)'s last or by saving the passage name to a variable. 
  • In a given passage, you add a stone to the player's inventory. 

If this is true, then whatever code is happening in the final point is triggering each time that passage is navigated to by the player, whether they're going there the first time or returning from the inventory, which is intended; harlowe has no way of knowing that one of those passages is a "menu" that the player needs to return from without side effects. 

There are a number of ways to fix this problem. My suggestion is generally to tie certain variable changes to player action rather than just the location. If you're setting a variable that is simply a discreet value, like a chapter number, you can set that anywhere. However, variables changes that should only happen once, like adding to player stats or an inventory, I think should be used as part of the links that transition players. So in this case, you'd do something like 

(link: 'Continue')[
    (set: $inv to it + (a: 'stone'))
    (goto: 'some passage')

and that code would go in the preceding passage, replacing the normal passage link. Unfortunately, you'll lose the passage connections in your story map. To get the arrows back if you want them, you can use a commented out link: 

<!-- [[some passage]] -->

Again, this is just the way I try to avoid this issue. There are other ways, but many of them depend on your specific needs. 

commented by (150 points)
edited by
Thank you so much!! This fixed my problem. Now I don't have any magically duplicating rocks!

And as for the problem with no arrows, since I number my passages (e.g. AxeQuest1, AxeQuest2, etc.) it allows me to keep track of them, even without arrows.
Welcome to Twine Q&A, where you can ask questions and receive answers from other members of the community.

You can also find hints and information on Twine on the official wiki and the old forums archive.

See a spam question? Flag it instead of downvoting. A question flagged enough times will automatically be hidden while moderators review it.