[Twine2/Sugarcube2] Help Making a 'Vending machine' mechanic.

+1 vote
asked Jan 23 by Loliconman (490 points)

<<silently>>
<<set $VMachinePrize1 to random(3)>>
<<if $VMachinePrize1 is 0>><</if>>
<<if $VMachinePrize1 is 1>><</if>>
<<if $VMachinePrize1 is 2>><</if>>
<<if $VMachinePrize1 is 3>><</if>>
<</silently>>

The code I am using so far. I want it to be set up as if it was a vending machine. The game is set up so once per day you can pay a fee and depending on the number you get I item. I am trying to find out a way where if you get the item once before(or the chance has been activated before.) instead of going to what the variable is registered to do it displays a different passage.

Example:

 

If you just got the variable for the first time:

<<if $VMachinePrize1 is 1>>

You got a prize!

<<set $Prize to 1>>

<</if>>

but, I want it so if you have the prize, or if '$prize is 1', it displays this

 

<<if $VMachinePrize1 is 1>>

<<if $Prize is 0>>

You win a prize!

<<set $Prize to 1>>

<</if>>

<<if $Prize is 1>>

The Machine beeps at you.

"This Item is sold out!"

<</if>><</if>>

I am sure this would work, but I want your opinions or alternate solutions.

1 Answer

0 votes
answered Jan 23 by idling (10,470 points)
edited Jan 23 by idling

I'm not 100% sure how exactly you want the machine to work. I'd probably put something like this into StoryInit:

<<set $prizes = ["Hat", "Bag of Candy", "Golden Ticket", "Pack of Tissues"]>>
<<set $inventory = []>>
<<set $VMachinePrize = 0>>

And then design the machine something like this:

Throw in a coin for your chance to win a prize.

<<nobr>>
<span id="prize">
<<set $VMachinePrize = random(3)>>
<<for _i to 0; _i lt $prizes.length; _i++>>
	<<capture _i>>
	<<if $inventory.includes($prizes[_i])>>
		<<link "Slot _i">>
			<<replace "#prize">>The Machine beeps at you.<br>"This Item is sold out!"<</replace>>
		<</link>>
	<<elseif $VMachinePrize != _i>>
		<<link "Slot _i">>
			<<replace "#prize">>Too bad. Try again tomorrow.<</replace>>
		<</link>>
	<<else>>
		<<link "Slot _i">>
			<<replace "#prize">>You win a $prizes[_i].<</replace>>
			<<set $inventory.push($prizes[_i])>>
		<</link>>
	<</if>>
	<</capture>>
	<br>
<</for>>
</span>
<</nobr>>

Or if the player should be able to repick his choice if the item is sold out:

Throw in a coin for your chance to win a prize.

<<nobr>>
<span id="prize">
<<set $VMachinePrize = random(3)>>
<<for _i to 0; _i lt $prizes.length; _i++>>
	<<capture _i>>
	<<if $inventory.includes($prizes[_i])>>
		<<link "Slot _i">>
			<<replace "#nothing">>The Machine beeps at you.<br>"This Item is sold out!"<</replace>>
		<</link>>
	<<elseif $VMachinePrize != _i>>
		<<link "Slot _i">>
			<<replace "#prize">>Too bad. Try again tomorrow.<</replace>>
			<<replace "#nothing">><</replace>>
		<</link>>
	<<else>>
		<<link "Slot _i">>
			<<replace "#prize">>You win a $prizes[_i].<</replace>>
			<<replace "#nothing">><</replace>>
			<<set $inventory.push($prizes[_i])>>
		<</link>>
	<</if>>
	<</capture>>
	<br>
<</for>>
</span>
<</nobr>>
<span id = "nothing"></span>

 

commented Jan 24 by Loliconman (490 points)
edited Jan 24 by Loliconman

Works great. just need it setup to when you when a prize( or if $prizes[_i] is Golden ticket, Tissues or bag of candy) it goto's a passage where I can include a little illustration of you getting the item and able to <<set $GoldenTicket to 1>>.

 

if $prizes[_i] is Golden ticket

<<goto [[VMachinePrize1]]>>

if $prizes[_i] is Bag of candy

<<goto [[VMachinePrize2]>>

commented Jan 24 by idling (10,470 points)
edited Jan 24 by idling

I'd create a passage called for example "Picture":

<<nobr>>

<<if _i == 0>>
	<img src= "the URL of your image" width="500" height="300">
<<elseif _i == 1>>
	<img src= "the URL of your image" width="500" height="300">
<<elseif _i == 2>>
	<img src= "the URL of your image" width="500" height="300">
<<elseif _i == 3>>
	<img src= "the URL of your image" width="500" height="300">
<</if>>

<</nobr>>

You'll have to put the proper url in there and adjust height/width as needed. Then you can just do something like this:

Throw in a coin for your chance to win a prize.
<span id="picture"></span>
<<nobr>>
<span id="prize">
<<set $VMachinePrize = random(3)>>
<<for _i to 0; _i lt $prizes.length; _i++>>
	<<capture _i>>
	<<if $inventory.includes($prizes[_i])>>
		<<link "Slot _i">>
			<<replace "#nothing">>The Machine beeps at you.<br>"This Item is sold out!"<</replace>>
		<</link>>
	<<elseif $VMachinePrize != _i>>
		<<link "Slot _i">>
			<<replace "#prize">>Too bad. Try again tomorrow.<</replace>>
			<<replace "#nothing">><</replace>>
		<</link>>
	<<else>>
		<<link "Slot _i">>
			<<replace "#prize">>You win a $prizes[_i].<</replace>>
			<<replace "#nothing">><</replace>>
			<<set $inventory.push($prizes[_i])>>
			<<replace "#picture">><<display "Picture">><</replace>>
		<</link>>
	<</if>>
	<</capture>>
	<br>
<</for>>
</span>
<</nobr>>
<span id = "nothing"></span>

You can add any <<set>> macros you want to be executed between the approbriate <<link "Slot _i">><</link>>. As for setting $GoldenTicket to 1 - you don't really need that in this setup. If you want to check, whether the player has the golden ticket, you can just do something like:

<<if $inventory.includes("Golden Ticket")>>You have a golden ticket.<</if>>

If you really want to go to another Passage as soon as the player wins, you could instead do it like this:

Throw in a coin for your chance to win a prize.
<span id="picture"></span>
<<nobr>>
<span id="prize">
<<set $VMachinePrize = random(3)>>
<<for _i to 0; _i lt $prizes.length; _i++>>
	<<capture _i>>
	<<if $inventory.includes($prizes[_i])>>
		<<link "Slot">>
			<<replace "#nothing">>The Machine beeps at you.<br>"This Item is sold out!"<</replace>>
		<</link>>
	<<elseif $VMachinePrize != _i>>
		<<link "Slot">>
			<<replace "#prize">>Too bad. Try again tomorrow.<</replace>>
			<<replace "#nothing">><</replace>>
		<</link>>
	<<else>>
		[[Slot|$prizes[_i]][$inventory.push($prizes[_i])]]
	<</if>>
	@@color: CornflowerBlue; _i@@
	<</capture>>
	<br>
<</for>>
</span>
<</nobr>>
<span id = "nothing"></span>

This way the Player will ne linked to a passage that has the same name as the item - Winning the Golden Ticket would send the player to a passage called "Golden Ticket".

commented Jan 25 by Loliconman (490 points)

Thanks a ton! I will try it out later today!

 

thanks again for your help, idling!

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.
...