Endings that can happen at any point in the game

Twine Version: 2.3.16
Story Format: Sugarcube

So I don’t know how to phrase this very well but does any one know any way to have an ending that can be activated at any point in the game? For instance, if there was a “health” variable that starts at 100 but decreases over the course of the game decreases to 0 and the player dies. I understand how to make this happen but not at any point in the game because in my game the player’s health can hit 0 from countless combinations of choices so I want one that can happen automatically.

The way I would approach this:

  1. Create the PassageFooter special passage and put the following code in it:
<<if $hp <= 0 && passage() != "You died.">>
[[You died.]]

The contents of PassageFooter are appended to any passage in your story, so if the $hp variable ever reaches 0, a link “You died.” leading to the passage with the same name will appear.

  1. Put all the other links in your story that forward to new passages inside if-statements:
<<if $hp > 0>>
[[Choice A|Next passage]]

[[Choice B|Some other pasasge]]

(those will be displayed in the passage you place them in unless $hp equals 0, in which case they will be replaced with the “You died” link)

(EDIT: Modified the example so that it a) works when $hp is less than 0 and b) doesn’t display the “You died.” link inside the “You died.” passage)


If one of the ways to get to a Game Over screen is the player’s health hitting 0, you’ll need to check that variable any time health changes or decreases. You can either put the code for this inline, or call the mechanism by “including” it from another passage.

The Ogre strikes you!
<<set $health = $health - 3>>
<<if $health lte 0>><<goto 'gameover'>><</if>>

If you have a combat system, you may want this is separate passages so you can include it from anywhere.

: ogrestrike
The ogre slaps you with its sword causing damage!
<<include 'damageplayer'>>
You rise to your feet, shaken but resolved to continue. Your damage is now $health ...

: damageplayer
<<set $health = $health - 1>> <<include 'checkhealth'>>

: checkhealth
<<if $health lte 0>><<goto 'gameover'>><</if>>

: gameover
You have died! Would you like to <<restart>>?

That’s how I would do it with my limited knowledge, but there are likely better and more efficient ways. There’s a “widget” function that I haven’t learned about yet that you could use.

[EDIT] @agat above calls out the use of the PassageFooter which is also a great way to check a variable on every single page. [/me makes notes for my own story]

You might be able to employ the GOTO ‘passage’ function in the footer which would preclude needing to put forwarding links inside if statements since it will yank the player instead to your “game over” screen instead of showing the rest of the page whenever health hits 0. That’s a stylistic thing depending on how you want to present your prose and story.

The proper way to do this is to create a Config.navigation.override function which redirects passage navigation based on variables in the game. So, you might put something like this in your JavaScript section:

// Force the player to the "You Died" passage if they let $health get too low.
Config.navigation.override = function (dest) {
	var sv = State.variables;

	// If $health is less-than-or-equal to 0, go to the "You Died" passage instead.
	if (sv.health <= 0) {
		return "You Died";

That example is straight out of the SugarCube documentation.

Hope that helps! :slight_smile: