How can I adapt my passage overrides in Javascript so they don't interfere with each other?

If you are requesting technical assistance with Twine, please specify:
Twine Version: 2.3.14
Story Format: Sugarcube

Hi,

In one part of my story I have a countdown/ticking-clock challenge where the player is taken to a “FAIL” passage after the clock has run down; and in another part of my story, a somewhat similar thing happens when you allow a character to get too close to you. Both of these “fail overrides” are executed by code in my Javascript I’ve managed to find online (see below). The issue I’m having (I think) is the condition for the second fail override ($witchDistance < 3) is governing the first fail override… Hope this makes sense!

Can anyone advise on how to amend the Javascript so that the second fail override doesn’t influence the first? Thanks in advance.

config.history.controls = false

Config.navigation.override = function (d) {
if (State.variables.start) {
if ((Date.now() - State.variables.start) > 180000) {
return ‘Time up!’;
}
}
return false;
};

Config.navigation.override = function (d) {
var s = State.variables;
if (s.witchDistance < 3) {
s.vital = 0;
return ‘Witch reaches you’;
}
return false;
};

I’m not sure if this will make a difference, but have you tried putting them in <<script>><</script>> tags in the relevant passages, as opposed to a dedicated passage tagged with ‘script’?

Preface. I’ll assume that these events take place over multiple passages and navigations, so using simpler methods is unworkable.


You may only have a single navigation override function. Attempting to assign another simply replaces the previous value of the setting.

The simplest thing would be to simply combine the conditions into a single function. For example, something like the following:

Config.navigation.override = function (d) {
	var sv = State.variables;

	/* Timer. */
	if (
		sv.start &&
		(Date.now() - sv.start) > 180000
	) {
		return 'Time up!';
	}

	/* Witch distance. */
	if (
		sv.witchDistance &&
		sv.witchDistance < 3
	) {
		sv.vital = 0;
		return 'Witch reaches you';
	}
};

Just remember to clean up the variables that trigger each event that isn’t terminal—i.e., a bad end or the like—once they’re done, so you aren’t erroneously re-triggering events.

 
PS: config is legacy, it should be Config with a capital C. E. g.,

Config.history.controls = false;
2 Likes

Thanks TheMadExile, that’s perfect :smiley: