hasVisited() function not working

I am using Twine 2.3 and Sugarcube 2.29
I want to show the player some kind of introduction when he first visit a passage. So i used hasVisited() function, here is my code

<div data-theme-scrollable="true"><<if hasVisited("dungeon")>>
   Content
<else>
Introduction
</if>
</div> 

but somehow the introduction part never gets executed i don’t know what i am missing .I even used not hasVisited(), <<if lastVisited(“dungeon”) is 0>> nothing worked for me.Can anyone point out what i am missing.Thank you.

2 Likes

I don’t know why my code did not displayed there.Here is my code

<<if hasVisited("dungeon")>>
content
<<else>>
introduction
<</if>>
1 Like

FYI - You have to use the preformatted text icon </> to wrap around your code if you want it to be visible. For multiple lines of code you can do three backticks (on the ~ key) on a line by itself, then the code, then three more backticks on a line by itself to end the code section.

1 Like

Not a Twine expert, but shouldn’t there be something inside <>?
Like:

<<if hasVisited(“dungeon”)>>
content
<else>
introduction
<endif>
1 Like

There probably was when Prasanth originally wrote that. However, if you type in <<xyz>>, without putting it inside of preformatted text markers, this forum displays it like this: <>

Regarding your code, it should be done like this instead:

<<if hasVisited("Passage Name")>>
	Visited content.
<<else>>
	Unvisited content.
<</if>>

The <<endif>> format has been depreciated in favor of the <</if>> format, instead of “fancy quotes” standard double or single quote markers should be used to indicate the content is a string, and you should use double angle brackets around macro code. Single angle brackets should only be used for HTML code.

Anyways, Prasanth will need to repost his code inside of preformatted text markers before we can figure out what’s going on there.

Yeah…someone who moderates the forum probably should have noticed that or something…

I edited his message to include the back ticks. I didn’t even think to open the message and look at the raw text in there.

Thanks for correcting me. I work in AXMA Story Maker which is based on the original and simplified version of Twine which is why my psuedo-code was mainly deprecated forms.

1 Like

OK, now that I can see your code, I think I understand the problem.

If you’re using your above code within the “dungeon” passage, then hasVisited("dungeon") will be true, because you’re currently in that passage, thus it has been visited.

If you want some text to only show the first time you enter a passage, you can do this:

<<if visited() == 1>>
	First visit text.
<<else>>
	Text shown in subsequent visits.
<</if>>

In the above, the visited() function checks to see how many times you’ve visited the current passage (including this visit), and if it’s 1, then it shows the text for the first visit, otherwise it shows the alternate text.

Hope that helps! :slight_smile:

1 Like