0 votes
by (140 points)

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

Any help would be appreciated.

2 Answers

+1 vote
by (8.7k 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>>

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


+2 votes
by (63.7k 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.7k 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 :)
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.