Having an issue with comparing variables

Twine Version: 2.3.9
Story Format: SugarCube 2.31.1

I’m using code to create a “correct answer” in a textbox. The game will only let you progress if you’ve inputted the right text. This is what I’m using:

Answer: <<textbox "$code" "" autofocus>> 
<span id="textbox-error" style="color: red"></span>
<<button "Confirm">>
	<<set $code to $code.trim()>>
	<<if $code is "name">> <!--This line here is the source of my trouble-->
		<<goto "Passage 2">>
	<<else>>
		<<replace "#textbox-error">>Incorrect Answer<</replace>>
	<</if>>
<</button>>

However, I am trying to make my answers slightly variable. I have code elsewhere that sets the variable $name to a random selection of first and last names. (Like Steven Harris or Walter Green) I want the correct answer in the textbox to change depending on what the name is. I have tried many different things to get this to work:

<<if $code is $name>>
<<if $code is "$name">>
<<if $code eq "$name">>
<<if $code == $name>>

And so on. Everything I’ve tried brings up an error message, has no correct answer, has no wrong answer, or the correct answer is $name. I feel like this code should’ve worked at some point, but I’m at a loss.

Thanks in advance.

The first and last ones should have worked, so I’m guessing that you’re somehow setting $name incorrectly.

I’d recommend, as a test, displaying the value of $name on the page, to make sure it is what you think it is. Temporarily add something like this to the passage:

Name: "$name"

Note that you might need to put that code inside the <<replace>> to make sure it accurately reports the value of $name at the time the user clicks the button.

That snippet of code should display something like:

Name: "Anne"

However, if it shows something like this:

Name: "Anne "

then you know the problem is that a space is getting included. Tests like that are a good way to verify that your code is giving you what you expect it to.

Also note that the comparison you’re using requires that the capitalization match as well. So if the answer is “Bob”, and the user types in “bob”, then that won’t count as a match. If you want to ignore capitalization, then you could do something like this:

<<if $code.toLowerCase() === $name.toLowerCase()>>

(Note:===” is the same as “is”, and “==” is the same as “eq”. See the <<if>> macro documentation for details.)

Hope that helps! :grinning:

I have done that already, but I thought I would try it again, exactly as you suggested. It still doesn’t work.

The only thing that I can maybe see causing a problem is that $name is decided based on 2 other variables, first and last. These each have 10 different first/last names that get picked randomly, and then I do this:

<<set $name = "$first $last">>

I don’t know how this could be an issue, because when I print the name with your
Name: “$name” idea it prints out the name the right way, something like Joe Harris, or Walter Thomas.

I don’t think that would help at all but that’s the only thing I’m doing differently than I usually do.

Edit: I figured it out. Typing this whole thing out made me realize that I had put $first $last in quotes. I changed it to:

<<set $name = $first + " " + $last>>

And it worked.

For what it’s worth, I added a <<raw>> macro in my <<print>> Macro Differences section, which you can use if you need to debug variables like that in the future. (Click “Jump to Start” in the UI bar to see other sample code there.)

Enjoy! :grinning: