Temporary variables between passages?

Twine Version: 2.3.14
Format: Sugarcube2

I’m working on a cassette system where players can load and play different cassettes, both for the information, via a dialog box that pops up, and for activating events if in the right passage with the correct requirements. It’s very simple and everything works fine except for one thing. The current way that I’m trying to detect when a cassette is played is by using a temporary variable, but it obviously gets deleted during the refresh that’s necessary for anything to change.

<<script>> 
	state.display(state.active.title, null, "back") 
<</script>>

<<set _cassette to $loadedcassette>>

This is in the dialog box that pops up when a cassette is played. Very half hearted attempt but I wanted to avoid using a bunch of <<if>> and <<set>> to solve my problems, I’ll never learn that way. Basically I need _cassette to exist in some way after the passage is refreshed.

What’s the reason against making it a global variable?

There is no such thing as a “lasts until the next passage only” variable in SugarCube, it’s either a $story variable that lasts the whole game, or a _temporary variable that is wiped at the end of the passage.

A common pattern to do what you need is just re-use a single story variable, perhaps something like $result or $temp each time you need this, assuming that you remember to clear the value after using it.

1 Like

I was overthinking that way too much. A story variable that gets cleared works perfectly, thanks!

Another thing you can do is unset the variable with <<unset $var>> after you’re done using it.