Help with variables for tracking visited rooms

Hello everyone! Many apologies if this issue has been raised before, but I wasn’t able to find any answers that might solve it. I should also mention that I’m a complete newbie to coding and Twine and I’m mainly working through trial and error.

I’m trying to create a way system to unlock and display a secret room after visiting all rooms in the game.

So far I’ve set up my game like so:

  • Each passage contains the following code:
    <<if visited(passage()) is 1>><<set $visited to ($visited + 1)>><><<set $passageX to true>><>
    This allows for a counter at the top each page ($visited rooms / total rooms).

  • The homepage contains the following code:
    <<if $passageX eq true and $passageY eq true and $passageZ eq true>>
    [[SecretRoom]]
    <><>
    What I mean here is that if the passages are not true, nothing happens.

My problem is that each passage seems only to become true on the second visit, while I’d like it to be counted on the first visit.
I suspect that part of the problem might be that I need to set $visited to 0 before it can start counting. However, part of the game consists in retracing your own steps and even visiting the homepage multiple times, so that setting $visited to 0 on the homepage would also require a line of code that sets the value only on the first visit, so that the counter isn’t reset every time.

This may seem rather basic stuff, but has anyone encountered a similar scenario?
Any suggestions (or words of comfort) are much appreciated!

Thank you!
seedy_cake

You’re over complicating this. You don’t need to duplicate the visit state with variables, just use the state as-is.

Specifically. Just use the hasVisited() function in the passage with the secret room portal.

As a basic example:

<<if hasVisited("Bar", "Café", "Pizza Joint")>>…secret room link…<</if>>

Oh I see! Thank you very much, TheMadExile, that does sound less of a hassle.
I tried using the hasVisited() function but I’m getting the following error:

assignment operator found within <<if>> clause (perhaps you meant to use an equality operator: ==, ===, eq, is)

My code is:

`<<if hasVisited(“Five Fathoms Deep”, “Totentanz”, “Pieter Bruegel the Elder”, “Darkness Visible”, “Paradise Lost”, “Dido’s Lament”, “Corpus Christi Carol”, “Henry Purcell”, “Posthumous Publishing”, “It is ill to go against the flow”, “Titus Groan”, “Writer Artists”, “War Requiem”, “War Poets”, “Cantus in Memoriam”, “Benjamin Britten”, “William Blake”, “Ode on the Death of a Favourite Cat”, “Sinfonia Concertante”, “Ariel Songs”, “Nixon in China”, “Minimalism”, “Drowning by Numbers”, “Prospero’s Books”, “Bravura in the face of grief”, “Oliver James”, “The Lake of Innisfree”)>>
<div class="drydeath">[[A Dry Death]]</div>
<<else>><</if>>`


Is the div element clashing with the variable by any chance?

Thank you for your help so far!

Your code is using typographical (i.e., curly) quotes. I’m assuming from copy-pasting from a word processor. They’re not legal for use in code. Change them to regular quotes and you should be fine.

I.e., “Five Fathoms Deep” vs. "Five Fathoms Deep".

PS: You don’t need an empty <<else>>. If you’re not planning on using it, you may remove it.

1 Like

I would have never thought inverted commas would effect code that way! Very interesting stuff.
TheMadExile you’ve been a great, thank you very much for your help!
All the best
seedy_cake

Hello again! I just wanted to share the finished project with everyone, and run through further changes I’ve made along the way.
Death by Drowning.zip (161.2 KB)

I added plenty of css lines to make navigation more intuitive, including signalling clickable links and previously visited pages. This was particularly useful as it hinted towards unblocking the secret room addressed in this thread.
I’m particulalry proud of my hero images, as it took me a very long time to merge images, links, and a hover effect.
I still have a problem with Sugarcube’s default loading screen, as I’d rather have an entirely white screen (the 2-3 second wait doesn’t seem to justify the need for a loading screen, though I appreciate its purpose for heavier files). I understand this could be solved by editing the javascript: I’d greatly appreciate help from anyone who’s faced this issue before, I’m quite surprised I found so little material on the matter, at least for Sugarcube.

I’m not sure when or if I’ll be using Twine again but it has certainly been a very formative experience, and I’m very thankful for the help I received from this community.
Hope to hear from you soon,
all the best,
seedy_cake

1 Like

FYI - You can modify the loading screen with a little JavaScript code in your JavaScript section like this:

/* Replace default loading screen with a custom one. */
$("#init-loading").css({ animation: "none", border: "none", width: "100%", height: "100%" });
$("#init-loading").empty().append('<div style="text-align: center; text-indent: inherit;">Loading...</div>');

The first line adds some CSS properties to the loading screen, and the second line empties out what was on the loading screen and then replaces it with its own HTML content. See the jQuery .css(), .empty(), and .append() methods for details on how those work.

Simply modify that code until it gives you what you want.

Enjoy! :slight_smile: