Problem with a passage from a random event

Hello everyone! :slight_smile:

I’m using Twine since 3 weeks now. I’m not an expert so I’ve encountered a problem. I’m creating a game in Twine story format 3.1.0. with stats, random events etc. etc. Now:

From the starting page the player has 4 options to travel to different parts of the city:

[[optionA]]
[[optionB]]
[[optionC]]
[[optionD]]

After clicking for example optionA, the player goes to that passage but he has a chance for a random event.

(set: $busevent to (random: 1,5))(if: $busevent is >=4)[(goto: “BusEvent”)]

If the event pops out, the player needs to make one of two choices, which take him to one of two passages. And now the problem: after the player ends the event, I need to him to return to the desired location, which he chose earlier. So if he choose optionA and ended the event, I want him to go to the optionA. If he chose optionB and ended the event, he goes to option B and so on.

Is that possible? Or do I need to make a different scenario? :slight_smile:

Thank you for the help in advance!!

Please use the Preformatted Text option (in the toolbar) when posting code examples, it makes them easier to read. Based on the the version number you mentioned and the syntax of your code example I will assume you are using the Harlowe v3.1.0 story format.

There are a number of methods you can use to automatically track the current location of the Main Character. The following solution uses a known Passage Tag combined with a header tagged passage to automatically update a story variable ($room).

warning: the following code has not been tested, it make contain syntax errors.

1: Initialise the story variable within your project’s startup tagged passage.

(set: $room to "")

2: Create a header tagged passage and add the following code to it. The code checks if the current passage being viewed has been assigned a room tag, and if so it updates the $room variable with the name of the current passage. The code uses the (passage:) macro to obtain the current passage’s tags and name. You can name the passage whatever you like, I named it Track Location

{
	(set: _passage to (passage:))
	(if: _passage's tags contains 'room')[(set: $room to _passage's name)]
}

3: Assign the room passage tag to each of the passages that represent a ‘location’ in your project.
eg. if you have a ‘location’ passage named Library then assign it the room tag.

In your example you are using a (goto:) macro to automatically forward the reader to another passage, there are a number of potential issues with using this method to handle ‘events’:
a. It pads the History with potentially unnecessary entries.
b. The (goto:) executes before the reader has a chance to read the contents of the current ‘location’ passage. eg. they won’t get to read the contents of the Library passage.

4: The following is a corrected variation of your own random event code, in case you want to continue using the (goto:) method of handling events. I have removes the invalid usage of the is keyword when using a mathematical operator (like >=) to compare a variable to a Number.

{
	(set: _roll to (random: 1, 5))
	(if: _roll >= 4)[(goto: "BusEvent")]
}

5: You can use the current value of the $room variable to forward the reader back to the previous ‘location’ passage.

(link-goto: 'Continue', $room)

6: If you prefer to display the (initial) contents of the event within the ‘location’ instead then you could use a combination of a Named Hook, a (replace:) macro, and a (display:) macro to do that. Add in following to the Library passage.

You are standing outside the library...
|event>[{
	(set: _roll to (random: 1, 5))
	(if: _roll >= 4)[
		(replace: ?event)[(display: "BusEvent")]
	]
}]

It worked! :smiley: Thank you!!