0 votes
by (170 points)
This might be really noob stuff, but I'm having some weird problem with my IF closing tags. Using Sugarcube 2.21.0. Here is the code I'm using and it seems to be right for me, but everytime I run Play, I get error:

error: Cannot find a closing tag for macro <<if>>.

Here is my code:

<<if $vaara is "vaara">> Chest opens. [[task3]]
<<if $vaara is "VAARA">> Chest opens. [[task3]]
<<else>> Chest doesn't open [[try something else->returntask2]]?
<</if>>
<<set $visitedluokka to true>>

I get the same error also from this one:
<<if $visitedluokka is true>> Random flashback to your childhood.
<</if>>

2 Answers

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

For what you originally intended to write, you'd want to write it like this:

<<if ($vaara is "vaara") or ($vaara is "VAARA")>>Chest opens. [[Next task|task3]].
<<else>>Chest doesn't open. [[Try something else|returntask2]]?
<</if>>
<<set $visitedluokka to true>>

However, you can simplify that by using the .toLowerCase() method like this:

<<if $vaara.toLowerCase() is "vaara">>Chest opens. [[Next task|task3]].
<<else>>Chest doesn't open. [[Try something else|returntask2]]?
<</if>>
<<set $visitedluokka to true>>

The .toLowerCase() method makes that variable act as though it's all in lowercase, though it doesn't actually change the value of that variable.  This means it would match "Vaara", "VAARA", "vAaRa", etc...  See the link above for more details on that method.

This code:

<<if $visitedluokka is true>> Random flashback to your childhood.
<</if>>

should work fine.  Though, you could shorten that to just this:

<<if $visitedluokka>> Random flashback to your childhood.
<</if>>

If there is any "truthy" value in $visitedluokka, then the code inside the <<if>> there will be executed.

A "truthy" value is any value which is not false, 0 (zero), "" (an empty string), null, undefined, or NaN (Not a Number), which are all of the "falsy" values.

If you were getting an error on that part, then it might be caused by another problem or error earlier in the code there or by a problem within the area of the code where you have "Random flashback to your childhood." inside that <<if>>.  If you still have problems with that, then you may need to post more of the code around those lines before we can figure out what went wrong.

Hope that helps!  :-)

by (170 points)

Thank you so much! I managed to get the statement work with 

<<if ($vaara is "vaara") or ($vaara is "VAARA")>>

but for some reason the .toLowerCase didn't work, which is a bummer, because that was something I had wanted to use. I just received this error:

Error: <<if>>: bad conditional expression in <<if>> clause: Unexpected end of input

I'll have to try that one more time though, there might have been my own spelling error.

The reason for the error in my other chapter also cleared: I had used also there two <<if>> with only one closing tag. I have two boolean statements inside the string, but since I thought it would be irrelevand to this problem, I removed the other one from my example.

by (44.7k points)

You need the () at the end of .toLowerCase(), and the capitalization has to be exactly the same.

I copied the code I gave you above and it worked fine.

Anyways, glad that worked.  :-)

+1 vote
by (880 points)

The error message is generated because you have two <<if>> statements but only one <</if>>. For it to work the way I think you intend, you can change the 2nd <<if>> to be <<elseif>>. You can have multiple <<elseif>> statements within an <<if>> <</if>> block. See https://www.motoslave.net/sugarcube/2/docs/#macros-control

<<if $vaara is "vaara">> Chest opens. [[task3]]
<<elseif $vaara is "VAARA">> Chest opens. [[task3]]
<<else>> Chest doesn't open [[try something else->returntask2]]?
<</if>>
<<set $visitedluokka to true>>

Alternatively, you might consider using <<switch>> when there are multiple possibilities to be tested. See https://www.motoslave.net/sugarcube/2/docs/#macros-macro-switch

Also, there should be a way (although I haven't found it yet) to change a string to be all upper or lower case before you do the comparison. That way any mixed case input would be allowed and only one comparison would be needed.

 

by (170 points)
Thank you! I had made <<if>> strings before with multiple answer options, but apparently I had done it a bit different since my option didn't work properly. So from now one, only one <<if>> and the rest with some other if forms.
...