0 votes
by (240 points)
closed by

Twine 2.2.1, Harlowe 2.1.0.

I'm trying to attach two variables to a link for a login, but when I put in the right info, no link comes up. I also have a couple parts that show if the username or password is right or not, and I've also tried startup. With that, I've gotten things to work like putting in the command to get TO the login passage, but I can't get login to work. Currently, this is what I have.

(if: $command is $login)[Please enter your username and username.
(link: "Username")[(set: $username to (prompt: "Please enter your username","Username"))(print: $username)]
(link: "Password")[(set: $password to (prompt: "Please enter your password","12345"))(print: $password)]]

(if: $username is $correctusername)[Username correct! (set: $username_correct to true)]
(if: $password is $correctpassword)[Password correct! (set: $password_correct to true)]
(if: $username_correct and $password_correct is true)[ [[Login Success->Jim Benolo's Account]] ]


closed with the note: The question was solved.

1 Answer

+1 vote
by (159k points)
selected by
Best answer

note: You don't state this in your example but I am going to assume you have pre-initialised all your story variables prior to showing your 'login' passage, ideally this initialisation was done in your project's startup tagged special passage. I will be using the following default values for my testing.

(set: $login to "login")
(set: $command to $login)
(set: $username = "Username")
(set: $password = "12345")
(set: $correctusername to "abcde")
(set: $correctpassword to "abcde")
(set: $username_correct to false)
(set: $password_correct to false)

Your example contains the following issues:

1. The Target Passage name of your Markup based link contains a single quote.

It is generally recomended not to include punctuation characters within a Passage Name because those characters can have special mean to either the story format engine or to the web-browser. I suggest change the passage name in the link to the following, remember to also change the name of the Passage itself.

[[Login Success->Jim Benolos Account]]

2. The conditional expression of your last (if:) macro is invalid, it should look as follows.

(if: $username_correct and $password_correct)[ [[Login Success->Jim Benolos Account]]]

3. The last three (if:) macro are processed at the wrong point-in-time.

Except for the code contained within the associated hooks of interactive/time-based macros like (link:) and (live:) all the contents of a Passage is processed directly before it is displayed to the Reader, and by default none of that content is re-processed until that Passage is re-displayed to the Reader again.

This means that the last three (if:) macros in your exampe are only processed before the Passage is displayed, and at the time of that processing all three of their conditional expressions would of evaluated to false so their assoicated text would not of been displayed. It also means that any changes to the values of the story variables referenced in their conditional expressions would not cause those expressions to be re-evaluated.

You can get around this behaviour by placing the three (if:) macros within a child Passage (mine is named Check Username and Password), and using a named hook & (replace:) macro & (display:) macro combination to dynamically evaluate those three (if:) macros as needed.

a. Change the contents of your 'login' Passage to the following.

(if: $command is $login)[\
	Please enter your username and username.
	(link: "Username")[{
		(set: $username to (prompt: "Please enter your username", "Username"))
		(print: $username)
		(replace: ?results)[(display: "Check Username and Password")]
	(link: "Password")[{
		(set: $password to (prompt: "Please enter your password","12345"))
		(print: $password)
		(replace: ?results)[(display: "Check Username and Password")]

|results>[(display: "Check Username and Password")]

b. Create a new passage named Check Username and Password and add the following to it.

(if: $username is $correctusername)[Username correct! (set: $username_correct to true)]
(if: $password is $correctpassword)[Password correct! (set: $password_correct to true)]
(if: $username_correct and $password_correct)[ [[Login Success->Jim Benolos Account]]]

note: you can change the name of the above new Passage to whatever makes sense to your project, if you do change it then just remember to also change the name used within all of the (display:) macro calls in example A.

by (240 points)
Thanks. This fixed the story, and now I can continue the next part (and the rest of the part I wanted to add). If I ever do this login thing again, I'll try to do the same thing you did to get it to work. Again, thanks!