0 votes
by (510 points)

Okay so I'm trying to reduce my workload by making a single game-over screen that shows up as soon as the player loses all of their Vitality (HP basically). To do this I'm trying to make an if statement inside the PassageReady passage that uses the "goto" macro to send them to a game over passage. This way they'll be sent there before the next passage loads (in theory). However, whenm I test it, all that happens when the player's Vitality reaches 0 is that the game sort of "soft" crashes. There's no error message, but there's just a blank passage that doesn't even have a PassageHeader and the history controls don't work. This is the code I'm using:

<<if $vital < 1>><<set $vital to 0>><<goto "GameOver">><</if>>

Note: I get the same result if I do not include the quotation marks around the passage name.

1 Answer

0 votes
by (63.1k points)
selected by
 
Best answer

Here's what your code is doing: 

Before rendering —> check vitality 
If vitality < 1  —> goto "GameOver"
Before game over —> check vitality 
Vitality is 0    —> goto "GameOver"
Before game over —> check vitality 
Etc... 

In fact, the only reason your browser doesn't crash is because SugarCube is trying to prevent that. Every time it tries to render the passage, though, it's told to render the passage again and it can never actually get there. This would also lock the history controls and prevent anything, including your header, from rendering. 

To prevent this, you should probably use Config.navigation.override, which can be found here. You could also test the name of the rendering passage and make sure it isn't "GameOver" before sending the player there. 

by (510 points)

Oh I see, so I am inadvertently trapping it in an infinite loop then. So would a possible solution be to attach a variable to it? something like this:

<<if $vital < 1 && !$gameover>><<set $vital to 0>><<set $gameover to true>><<goto "GameOver">><</if>>

This way it won't activate any more than once.

by (63.1k points)

Or something like this:

Config.navigation.override = function (d) {
    var s = State.variables;
    if (s.vital < 1) {
        s.vital = 0;
        return 'GameOver';
    }
    return false;
};

And then just get rid of the whole <<if>><<goto>><</if>> thing.  This will check the vitality value on every passage transition and override the navigation if $vital is less than 1.

by (510 points)
Ah I see, thanks for the help, that does seem less bulky. I really need to learn JavaScript someday.
...