PassageReady goto events solution

Story Format: sugarcube 2.31.1

Hi all,
first I must outline what I’m trying to achieve to explain my problem. I have a hunger statistic in my game ($hunger) and when it reaches certain level, I want a random event appear from time to time. I’m trying to do it with PassageReady (or PassageDone,doesn’t really matter afaik). The code in PassageReady looks as follows:

 <<if $hunger gte 10>>
 	<<if $timer gte 10>>
 		<<set $timer to 0>>
 		<<goto "YoureSoHungry">>
 	<<else>>
		<<set $timer += 1>>
 	<</if>>
 <</if>>

This works fine, every ten “moves” player does he jumps to “YoureSoHungry” passage. The problem is how to come back - I can’t use <<back>>, because it creates an infinite loop and I can’t use <<return>>, 'cause the previous (random) passage may contain some variable’s change, which would be applied twice, which may cause a lot of problems. Any ideas how to correct the code, or how to achieve similar goal some other way? Thanks a lot in advance. J.

For something like this you’d be better off creating a Config.navigation.override handler. To do that, just add something to your JavaScript section like this:

Config.navigation.override = function (dest) {
	var sv = State.variables;
	if (sv.hunger >= 10) {
		if (sv.timer >= 10) {
			sv.timer = 0;
			return "YoureSoHungry";
		} else {
			sv.timer += 1;
		}
	}
	return dest;
};

Basically, whatever name you return from that function is the passage it will go to next. You can check the value of “dest” if you want to see what passage it was going to go to.

Enjoy! :grinning:

Firstly, thanks a lot for answer. Sadly JavaScript as all Greek to me and I still don’t know how to make this work. I still can’t use neither <<back>> nor <<return>> in the YoureSoHungry passage. I guess what I should do is something like this [[continue|$dest]] instead, but I have absolutely no clue how to do it. Any help?

Change the example to something like the following:

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

	if (sv.hunger >= 10) {
		if (sv.timer >= 10) {
			sv.timer = 0;
			sv.returnTo = dest;
			return "YoureSoHungry";
		}
		else {
			sv.timer += 1;
		}
	}

	return dest;
};

That saves the player’s original destination passage into the story variable $returnTo.

Then, in the hungry passage you can use the following to forward the player to their original destination:

<<link "Return" $returnTo>><<unset $returnTo>><</link>>

This does assume that you aren’t already using a story variable named $returnTo. If you are, simply change all instances above to something else.

Great, that’s exactly what I needed, works perfectly. Thanks a lot to both of you guys!