Triggering one of three endings

Hi there!

I’m writing my first interactive story in Twine and I need a little help.

I have three possible endings to my story, and each one could be triggered if the player chooses two or more conversation paths along the way. In the story one ending sees the protagonist’s mom coming to their rescue, the second their friend coming to their rescue, and the third is that no one does. If the player chooses two or more of conversation paths with their mom, that will trigger the Mom ending, same with the friend. But I’m not quite sure how to program this. I’m thinking this may involve a boolean code, but I don’t really know how to tag the passages and to make sure that each ending fits the requirement of two or more triggering passages.

Am I making sense?

Thanks in advance for any help for a total noob! :slight_smile:

Twine Version: 2
Story Format: SugarCube 2.31.1

At the start of your story add:

<<set $timesTalkedToMom = 0>>
<<set $timesTalkedToFriend = 0>>

You can then add 1 every time they are talked to using:

<<set $timesTalkedToMom += 1>>


<<set $timesTalkedToFriend += 1>>

In the passage before the ending:

<<if $timesTalkedToMom === 2>>


<<elseif $timesTalkedToFriend === 2>>





This assuming you cannot talk to both the mom and the friend twice.

Hope this helps

For “two or more” it should be “>= 2” instead of “=== 2”.

Also, instead of using the <<set>> macro to increment the variables, you could do that within the link to those passages. For example:

[[Talk to friend|Friend passage][$timesTalkedToFriend += 1]]
[[Talk to Mom|Mom passage][$timesTalkedToMom += 1]]

Other than that, Gulumige’s solution seems good.

EDIT: Fixed typo.

Thank you!!! :smiley:

So if the times talked to friend and times talked to mom are equal and I want that condition to lead to RescuedbyMom, could I use something like this in the final sequence (along with the friend, mom and no rescue conditions):

<<elseif $timesTalkedToFriend >== 2 and $timestalkedtomom >== 2 >>


So in total it would look like this:

<<if $timesTalkedToMom >== 2>>


<<elseif $timesTalkedToFriend >== 2>>


<<elseif $timesTalkedToFriend >== 2 and $timesTalkedToMom >== 2 >>





*Note for some reason the else and if at the end are not showing up properly, but they are in there!

The greater-than-or-equal relational operators are: >= (JavaScript) and gte (TwineScript). >== is simply a syntax error.

So in Twine it would be:

<<if $timesTalkedToMom gte 2>>



SugarCube, you mean. And yes, either gte or >=. E.g.,

<<if $timesTalkedToMom gte 2>>

<<if $timesTalkedToMom >= 2>>

Whichever floats your boat.

1 Like

Sorry about the “>==” thing instead of “>=”. For some reason I make that mistake when writing posts, but not when writing code. :stuck_out_tongue:

Also, you should be aware that the <<if>> macro only triggers the first chunk of code which has a conditional with a “truthy” value. (The values null, undefined, 0, NaN (Not a Number), "" (an empty string), and false are all “falsy” values, all other values are “truthy” values.)

This means that you won’t need the:

<<elseif $timesTalkedToFriend >= 2 and $timesTalkedToMom >= 2 >>

part, because the

<<if $timesTalkedToMom >= 2>>

part would’ve already triggered in that case, since it comes first.

Hope that helps! :smiley:

P.S. If you’re going to post code in this forum you should wrap it within a “preformatted text” block (use the </> button), otherwise the forum has a habit of eating macros (see in your code where the <<else>> and <</if>> look like “<>”).

1 Like

Oh that’s great, thank you!!! :slight_smile:

Thanks so much! :smiley: