Help with different types of random events

I noticed that some of your questions have to do with introducing random events, so I’ll try to help you out with that question.

Because you’re working with SugarCube, you can use a Config.navigation.override function to redirect passage navigation for these random events. I’ll give you an example of how you can use this function for random events.

To make things simple for this example, let’s say you have “normal” passages, “travel” passages, and “event” passages. When going to “normal” or “event” passages, there’s no chance of random events, but for “travel” passages there’s a 30% chance of ending up at an “event” passage instead, unless you’re already coming from an “event” passage, in which case you’ll always make it to the “travel” passage. That will prevent triggering multiple events in a row. To be able to recognize all of the “travel” and “event” passages, they’ll all need to be marked with a “travel” or “event” tag, respectively. (I’d also recommend picking colors for those tags, to make those passages easier to spot.) Any passages without either of those tags will be a “normal” passage.

Now that we have that structure figured out, you can implement that by putting this in your JavaScript section:

// This function is triggered just before each passage navigation event.
Config.navigation.override = function (dest) {
	if (tags(dest).includes("travel") && !tags().includes("event")) {
		// If the destination is a "travel" tagged passage and
		// the current passage is NOT an "event" tagged passage, then...
		if (random(1, 10) <= 3) {  // ...there's a 30% chance of:
			// Storing the original destination in the $continue variable.
			State.variables.continue = dest;
			// Changing to a random "event" tagged passage.
			dest = Story.lookup("tags", "event").random().title;
		} // ...otherwise continue to the original destination passage.
	} else {  // For non-"travel" tagged destination passages:
		// Clear the value in $continue since it shouldn't be needed anymore.
		delete State.variables.continue;
	}
	// Go to the passage named in the "dest" variable.
	return dest;
};

Note that the code which looks at the tags is case sensitive, so tagging a passage with “Travel” instead of “travel” will not work.

That code uses the SugarCube tags() function to get an array of tags in a passage, the .includes() method to determine if a value is in an array, the random() function to get a random number, the State.variables object to access SugarCube story variables (since you can’t directly use SugarCube story or temporary variable names within JavaScript code), the Story.lookup() method to get an array of passage objects with a particular tag, the .random() method to get a random element from an array, and the .title property to get the title of a passage from a passage object. It also uses the JavaScript delete operator to remove the $continue story variable from the State.variables object (it’s the JavaScript equivalent of the SugarCube <<unset>> macro).

Once you have added that to your code and added “travel” and “event” tags to the appropriate passages, it will then automatically have a 30% chance of redirecting any movement to a “travel” tagged passage to an “event” tagged passage, unless you were already in an “event” tagged passage.

If you want to have a link in an “event” passage that sends you back to the original “travel” passage that you were heading to, you can use the $continue variable in the “event” passage to know which passage to go to. So the code you can use to exit your “event” passage may look like this:

<<link "Continue" $continue>><</link>>

That will display a link that says “Continue”, and clicking on it will send you to the original destination “travel” passage that you were attempting to go to when you were interrupted with the event.

Note: As the code is currently, the $continue variable will be deleted once you exit any “event” passage. This is to prevent unnecessary “bloat” in the game history, which can happen if you store a bunch of unneeded story variables, since that extra data will slow down saves, loads, and passage transitions.

Now, to be clear, there is no reason why you would have to do things exactly as I’ve shown. That’s merely a simple example of one way that it could be done. There are millions of different ways you could do that, including having different odds, not automatically deleting the $continue variable, having different events possible depending on things such as player level or destination passage, etc… It’s entirely up to your imagination and coding skills. Feel free to modify it into whatever you want.

Hopefully, though, this should give you a basic foundation for setting up random events that can trigger in your game. Please let me know if you have any questions on how any of the above works.

Enjoy! :slight_smile:

1 Like

@HiEv Oh my god :scream: THIS WILL HELP ME A LOT, THANK YOU SO MUCH!! :heart_eyes::heart_eyes::heart_eyes:

@StJohnLimbo thank you so much!! :heart_eyes: