Need help condensing macros into a single variable

If you are requesting technical assistance with Twine, please specify:
Twine Version: 2.3.16
Story Format: SugarCube 2.36.1

I’m trying to get this code to work. I’m new to SugarCube so it’s probably a rookie mistake.

<<set $timeflavor to 

"<<if $daylightcyle is \"day\">> The sun peeks through the trees. You feel safe. 

<<set $paranoiachance to \"false\">>

<</if>>

<<if $daylightcycle is \"night\">> The stars are out. <</if>>

<<set $paranoiachance to \"true\">>

<<if $paranoiachance is \"true\">>

<<set $paranoia to random(1, 4)>> 

<<if $paranoia is 1>> You think you saw something move in the shadows.

<</if>>"

>>

Worked fine when it was in Harlowe, but when I ported my project to SugarCube I couldn’t get it to work. The idea is to make a shortcut for repeatable flavor text based on a couple variables, so all I would need to do is put <<print $timeflavor>> where I want the text to appear, just to make it cleaner and easier.

Forgot to include the error message!

Error: <>: bad evaluation: Unexpected EOF

I have no idea what that means :sunglasses:

I’ve never used Twine, but my programming experience suggests you’re missing a

<</if>>

after testing whether paranoiachance is true. This would cause an unexpected EOF (end of file).

Edited to get code to display.

You can’t have an <<if>> inside a <<set>>. You need to do it the other way around: Conditional statement first, then setting the variable.
Edit: you are also missing a closing <</if>> for the one before last <<if>>

The previous commenters are right that you’re missing some <</if>> tags, but another thing you should keep in mind is that if you want to repeat code in Sugarcube, there are easier and more efficient ways than using a variable. Since variables are saved to the game history, having a lot of variables like this in a big game can impact performance. And, if at some point after releasing the game you decide to change or fix something in the timeflavor text, any player that has a previous save wouldn’t see the change since the old $timeflavor from their save will be loaded and replace the fixed version. Also, it’s just plain unwieldy.

A better way would be to use a widget. Create a passage and tag it widget, then paste this in it:

<<widget timeflavor>>

<<if $daylightcyle is "day">> The sun peeks through the trees. You feel safe. 

<<set $paranoiachance to "false">>

<</if>>

<<if $daylightcycle is "night">> The stars are out. <</if>>

<<set $paranoiachance to "true">>

<<if $paranoiachance is "true">>

<<set $paranoia to random(1, 4)>> <</if>>

<<if $paranoia is 1>> You think you saw something move in the shadows.

<</if>>

<</widget>>

Then, whenever you want the text to appear, just write <<timeflavor>>.

2 Likes

If the $paranoiachance variable is representing a Yes/No or On/Off or True/False two state situation then I suggest using a Boolean value instead of a String value.

eg.

<<set $paranoiachance to true>>

<<if $paranoiachance>>
    Variable currently equals true, or a Truthy value.
<<else>>
    Variable currently equals false, or a Falsy value.
<</if>>

<<set $paranoiachance to false>>

<<if not $paranoiachance>>
    Variable currently equals false, or a Falsy value.
<<else>>
    Variable currently equals true, or a Truthy value.
<</if>>
1 Like

Thank you, it works great! I had no idea widgets existed, I’ll definitely be using them going forward.