0 votes
by (190 points)
reopened by

Hi everyone, I've started using Twine a few weeks ago now, and I think I'm getting better, but I am still very much a novice. I have read the Sugarcube documentation and everything, but a lot of it is still quite mysterious to me.

Basically, I've created several "marketplace" passages where you can buy various items, like so:

(Product description here)

/* The following variables and stuff are placeholders. */

<<if $player_money >= 500>>\
Would you like to buy this product?

<<linkreplace Buy "Product in question">>\
<<set $player_money -= 500>>\
Thank you for shopping here!

<<set $productinquestion_count += 1>>\
<<set $inventory_count += 1>>\ /* This is for the inventory system I have, no need to pay attention. */
You receive a "Product in question"
<</linkreplace>>\
<<else>>\
You don't have enough money to purchase the "Product in question".
<<endif>>\

[[Back to main marketplace here]]

The issue is that later on in my game, you can accumulate enough money where buying several of the item can be interesting. This system, while functional, requires the player to click back through menus/passages to get to the page again.

So what I'm looking for is a way to make a link that offers itself as an option if the player has enough money (with a thank you message to boot), or refuses payment if not. My original idea was using the <<link>> macro as opposed to the <<linkreplace>>, but it doesn't stop when the player overspends, and doesn't update the passage at all until you switch passages (unless I'm missing some kind of limit). I also tried using the <<link>> as a link to the passage itself, but I lose the message that way. I also briefly attempted some <<for>> looping:

<<if $player_money >= 100>>
Would you like to buy this product?

<<for $player_money >= 100>>\
\
<<if $player_money < 100>>\
You don't have enough money to purchase the "Product in question".
<<else>>\
\
<<link Buy>>\
<<set $player_money -= 100>>\
Thank you for shopping here!

<<set $productinquestion_count += 1>>\
<<set $inventory_count += 1>>\
You receive a "Product in question".
\
<</link>>\
<<break>>\
\
<<endif>>\
\
<</for>>\

<<else>>\
You don't have enough money to purchase the "Product in question".
<<endif>>

I must confess that I'm really not sure how to use loops, as I'm very new to these basic programming concepts (as you can probably tell from above :P) ; and I have no idea if it can even apply to my case.

 

I don't need a <<for>> loop necessarily, however I would like to know if what I'm looking to achieve is possible, and if so, how. I'd appreciate any help!

1 Answer

0 votes
by (190 points)
 
Best answer

All right, well I've answered my own question, I guess. Just had to keep thinking about it, and not be so impatient. If anyone's curious, this is the way I ended up doing it:

 

<<if $productbought is true>>\ /* I defined this variable in StoryInit as false. */
Thank you for shopping here!
You receive a "Product in question".
<<endif>>\

<<set $productbought to false>>\
\
<<if $player_money >= 500>>\
Would you like to buy this product?

<<link Buy>>\
<<set $player_money -= 500>>\

<<set $productinquestion_count += 1>>\
<<set $inventory_count += 1>>\
<<set $productbought to true>>\
<<script>>state.display(state.active.title, null, "back")<</script>>
<</link>>\
<<else>>\
You don't have enough money to purchase the "Product in question".
<<endif>>\

So basically, an exterior switch variable tracks if the product has been bought, and shows a special message right before deactivating itself, when the passage refreshes (btw, credit to TheMadExile on this forum thread here for giving a simple way to refresh passage).

 

Question is therefore closed! I feel a little stupid now.

...