It feels weird to post again but I have encountered another one of those problems that a few hours of desperate testing and research couldn’t solve…
I’m creating a randomized book system. The way it works is there will be a prompt for the player to enter the library and search the bookshelves. A random book title will then be pulled from an array, and the result will be put through a switch case to determine the description and player reaction. This part works as intended.
<<set $book_titles = ["book 1", "book 2", ...]>> (example only, too many actual books to actually quote)
<<set _book_titles = $book_titles.random()>>
<<switch _book_titles>>
<<case "book 1">>
...
<<case "book 2">>
...
I wanted to have a series of books that are similar to each other so that the player will remember it (and finding all 3 of them unlocks an achievement, but it’s another topic). In StoryInit, I have set the appropriate variables which would be set to true after encountering the respective book.
<<set $potato1 to false>>
<<set $potato2 to false>>
<<set $potato3 to false>>
Back to the problem part of the switch case.
<<case "Potato The First">>
"Potato The First"<<set $potato1 to true>>
The cover has regular boring text and letters on a blue background. <<if $potato2 is true or $potato3 is true>>I feel like I have seen one of these before...<</if>>
I'm not touching that thing.
<<case "Potato The Second">>
"Potato The Second"<<set $potato2 to true>>
The cover has regular boring text and letters on a dark blue background. <<if $potato1 is true or $potato3 is true>>I feel like I have seen one of these before...<</if>>
I'm not touching that thing.
<<case "Potato The Third">>
"Potato The Third"<<set $potato3 to true>>
The cover has regular boring text and letters on a beige background. <<if $potato1 is true or $potato2 is true>>I feel like I have seen one of these before...<</if>>
I'm not touching that thing.
But even as I have encountered all three variants of the potato book series, the conditional sentence did not appear, which tells me that the variable was not changed at all. If I fix the condition to match the variable that was set then it works, but otherwise no. I read somewhere about the variables in switch cases only works in the case and will not be changed globally, but it is from C++ or something and I’m not sure if it applies to Twine. I’m finding nothing to help me with this. Does anyone know what’s wrong?
<<set $book_titles = ["Potato The First", "Potato The Second", "Potato The Third"]>>
<<set _book_titles = $book_titles.random()>>
<<switch _book_titles>>
<<case "Potato The First">>
"Potato The First"<<set $potato1 to true>>
The cover has regular boring text and letters on a blue background. <<if $potato2 is true or $potato3 is true>>I feel like I have seen one of these before...<</if>>
I'm not touching that thing.
<<case "Potato The Second">>
"Potato The Second"<<set $potato2 to true>>
The cover has regular boring text and letters on a dark blue background. <<if $potato1 is true or $potato3 is true>>I feel like I have seen one of these before...<</if>>
I'm not touching that thing.
<<case "Potato The Third">>
"Potato The Third"<<set $potato3 to true>>
The cover has regular boring text and letters on a beige background. <<if $potato1 is true or $potato2 is true>>I feel like I have seen one of these before...<</if>>
I'm not touching that thing.
<</switch>>
produces the expected results, setting the variables to true and displaying the conditional text when appropriate. If it’s not working on your end, there’s some other code somewhere else that’s interfering.
I may have found the problem. I used the run engine backward prompt on exit so it would reset the variables…
<<link "Back">><<run Engine.backward()>><</link>>
…But in that case I can’t think of any other way to return the player to the previous passage. Using <<goto $return>> wielded weird results (empty page). This particular library passage will be called on by all future library interactions, so I can’t just link a constant passage name in the return link. I thought of a way to manually save the entrance passage’s name in a variable, then paste it to the return macro to go back.
Bookshelves lined up in front of me.
[[Read a book][$current_library to passage()]]
(At the end of "Read a book" passage)
<<return [["Return"|$current_library]]>>
And now the problem is how to get the engine to read $current_library as the passage name… In my case it was a string. Or find a different way to go back to the previous passage without using undo…
This makes the macro use the value of the variable as an argument rather than the string "$current_library".
Meanwhile, if you want to use the [["Return"|$current_library]] syntax with a variable, you can just delete the passage named $current_library that Twine automatically creates and it will work in the game. It will also leave an ugly red cross in the Twine UI because it think it’s linking to a non-existent passage, so I don’t recommend doing that, but it’s an option.