0 votes
by (140 points)
Hey,

I'm very new to Twine/Java only been using it for about a month. I am currently making a everyday life kind of game and I need help making a store. I have tried making separate passages for each product which worked but was super unorganized eg. [[Carrot]] Which would move me to the passage and then add the item "Carrots" to my inventory and subtract the cost from my money.

Ideally I want each item in a list in the store and set up like this.
Carrot $20[[Buy]]
Lettuce $10[[Buy]]

and I want the same buy button for each product so that instead of taking me to a separate passage it just does the subtraction and adds it to my inventory each time I click it.

I use this for my storing how much money I have $player.money and the inventory macros by F2Andy
https://twinery.org/wiki/twine2:add_an_inventory_system

Any help would be appreciated.
Thanks.

2 Answers

+1 vote
by (8.9k points)

Do you know about the <<link>> macro?

You could do something like:

Carrot $20 <<link [[Buy it|Store*]]>>
  <<set $player.money -= 20>>
  <<addToInv carrot>>
<</link>>

*Assuming the passage the store is in is called "Store"

 

+2 votes
by (68.6k points)
edited by

You may use either the <<link>> or <<button>> macros to provide a way for players to perform an action which does not need to navigate passages (though, both macros can do that as well).  For example:

Carrot $20 <<link "Buy">><<set $money -= 20>><<addToInv "carrot">><</link>>
Lettuce $10 <<link "Buy">><<set $money -= 10>><<addToInv "lettuce">><</link>>

 

Alternatively.  If you decided that you needed to navigate back to the same passage for some reason, then I'd suggest the following minor alteration of the above example instead:

Carrot $20 <<link "Buy" `passage()`>><<set $money -= 20>><<addToInv "carrot">><</link>>
Lettuce $10 <<link "Buy" `passage()`>><<set $money -= 10>><<addToInv "lettuce">><</link>>

NOTE: The only change is the addition of the optional passageName argument to the macro.  In this case, a backquote expression containing a call to the passage() function, which will return the name of the current passage.

by (8.9k points)
This is more elegant than my solution &ndash; I didn't realise that <<link>>s didn't need to be directed anywhere.  Thanks MadExile.  :-)
by (140 points)
Second method worked perfectly thanks heaps to both of you :)
...