Setting and changing initial variables in Sugarcube [Storyinit passage]

If you are requesting technical assistance with Twine, please specify:
Twine Version:2.3.16
Story Format: Sugarcube
Hello everyone, I am having trouble with a basic set command. Everything around this works, but this one in particular, the description of clothing, doesn’t appear. It just prints out $itemdesc, instead of the actual variable I have set for $itemdesc . Here is the part of the code in the storyinit passage

<<if $playeroutfit === 'Work Clothes'>>
	<<set $outfitdesc to "A light blue string crop top with jean shorts.">>

I have the other options under elseif’s, and a closing /if tag at the end.
Here is the passage under the equipment stats, that the player can click on and look through.

Equipment.
<<print 'Currently Equipped Weapon: $playerweapon.'>>
<<print 'Currently Equipped outfit: $playeroutfit.'>> 
<<print '$outfitdesc'>>

So the $playerweapon and $playeroutfit variables both work as they should, but for some reason the outfitdesc just literally prints out ‘$outfitdesc’.
I did the quick find to make sure there was no other $outfitdesc in any of the other passages that could be overwriting the storyinit, and it only appears in the equipment passage and storyinit.
I have no idea what I did/am doing wrong. Any help is greatly appreciated.

Does it work if you use “eq”?
<<if $playeroutfit eq 'Work Clothes'>> […]

Triple equals should work, but it’s worth a try.

Another thing is to try the variable naked instead of in quotes - it might think you’re literally trying to print “$outfitdesc”.

Equipment.
Currently Equipped Weapon: $playerweapon
Currently Equipped outfit: $playeroutfit
$outfitdesc

Thanks for the response,
I tried the eq, but that didn’t work.
I did remove the single parentheses, but now it’s just saying [undefined].
Does this mean that there’s something wrong with what I have in the StoryInit passage?

What’s in the Storyinit passage? Since it says “undefined” that means the variable isn’t getting set - either in storyinit, or in your <<if $playeroutfit ===[…]>> passage. Make sure you’re setting the variable in Storyinit as a string $playeroutfit = "nothing" or $playeroutfit = "" and not just $playeroutfit = false - if the variable type doesn’t match (string vs. boolean) I don’t believe it can set to different type.

I’m not sure about whether single and double quotes are completely synonymous, as far as I know they are… but I’ve noticed if I use single quotes in something like <<print either (‘one’, ‘two’, ‘three’)>> I can’t use a contraction unless I start with double quotes <<print either(“You can’t.”, “You won’t.”)>>

if/else can be tricky sometimes, especially when there’s complicated nesting.

I’d recommend hitting Play in the IDE and track your $playeroutfit and $outfitdesc variables using the “bug” pane on the lower right as you go through the relevant passages. You can at least track if the variable isn’t getting set in the Storyinit somehow, or if it’s missing in the “if===” passage.

This is the full storyInit passage. The $playeroutfit set works perfectly, so I may just give up and bundle the description right after the outfit. (mildly adult content warning)
//outfits// <<if $playeroutfit === 'Work Clothes'>> <<set $outfitDescription to "A light blue string crop top with jean shorts.">> <<elseif $playeroutfit === 'Evening Dress'>> <<set $outfitDescription to "A nice jade-green number that goes down just barely past the mid thighs. It's folded front leaves a lot of $name's thighs exposed.">> <<elseif $playeroutfit === 'Maid Outfit'>> <<set $outfitDescription to "An outfit based on old school french maid wear. The amount of frills and exposed skin do a lot for some people. Not recommended for actual cleaning.">> <<elseif $playeroutfit === 'Leather Armour'>> <<set $outfitDescription to "Basic leather armour thick enough to deflect some rather weak blows, but only (barely) covers the vital organs. Looks more like an outfit from some questionable anime series your smelly cousin loves.">> <<elseif $playeroutfit === 'Chainmail'>> <<set $outfitDescription to "Don't kid yourself. This does literally nothing. Its barely a bikini. If it somehow manages to block anything I will be very surprised. Made from metal so it won't stain, so it has that going for it. Which is nice.">> <</if>>

These if-statements won’t work in the Storyinit passage. It runs before anything else in the game. Hence, your first if-statement does nothing since $playeroutfit hasn’t been set yet.

Storyinit should set what the variables are supposed to start at.

<<set $playeroutfit to 'Work Clothes'>>
<<set $outfitDescription to "A light blue string crop top with jean shorts.">>

The contents you have here will work in a passage after the player selects an outfit.

1 Like

Just to add on to what Hanon was saying, please be aware that <<if>> macros and the like don’t normally hang around and do things later on if the conditional part changes. They only do something at the single moment they’re first executed. You’ll need to run them again at any point where you might need them to do whatever it is that you want them to do.

Hope that helps clear things up, if that’s what you were thinking. :slight_smile:

1 Like

Bro, you guys are so smart.
I took that whole passage out of storyinit, and now it’s working.
I am now going to try and make a listbox in the equipment passage that lets the player put on the various armors and clothes they pick up. If you guys have any suggestions, I would love to hear it!
Thanks again!

Awesome!

Since if-statements are checked only when a passage runs there are several possibilities.

You can build a passage out of what was in Storyinit and either flow the player through it after they make a choice to change their clothing, or you can call that passage using <<include>>.

<<include "checkclothes">>
(where “checkclothes” is your passage with all of the if-statements checking variables and setting them correctly).

Including a passage runs everything in it, but you can have passages with no text at all that just tweak variables.

I think this works? …there are probably several different better ways to do this but I’m learning how to construct links…so every problem I try to solve is link-shaped… :laughing:

You're wearing: $playeroutfit .

<<link "Put on the work unform." "ThisSamePassage">><<set $playeroutfit to 'Work Clothes">><<include "checkclothes">><</link>>

Rather than using an <<include>> macro like that, it would probably be better to make it into a custom SugarCube widget instead, since then you could have it set everything appropriately based on the value passed to the widget.

To make a widget like that, create a non-special passage and give it “widget” and “nobr” tags (the latter so that it will ignore line breaks). Then put something like this in the passage:

<<widget "checkclothes">>
	<<set $playeroutfit = $args[0]>>
	<<switch $playeroutfit>>
		<<case "Work Clothes">>
			<<set $outfitDescription = "A light blue string crop top with jean shorts.">>
		<<case "Evening Dress">>
			<<set $outfitDescription = "A nice jade-green number that goes down just barely past the mid thighs. It's folded front leaves a lot of $name's thighs exposed.">>
		...etc...
	<</switch>>
<</widget>>

Once you’ve filled in the “...etc...” part above with actual code, you’d then only need to do something like <<checkclothes "Work Clothes">> in your passages in order to set everything appropriately for the given outfit. (See also the <<switch>> macro documentation for details on how it works.)

Another thing you might do, instead of looping back to the same passage just to update the clothes description, you could instead update the displayed information directly when the link is clicked by tweaking Hanon’s code like this:

<span id="clothes">''$playeroutfit''
$outfitDescription</span>

<<link "Put on the work uniform.">>
	<<checkclothes "Work Clothes">>
	<<replace "#clothes">>\
		''$playeroutfit''
		$outfitDescription\
	<</replace>>
<</link>>

The <<replace>> macro updates the content of the “clothes” <span> due to the ID passed to it ("#clothes" means “the element with an ID of ‘clothes’”).

Note that the two single-quotes around $playeroutfit in the above code is just markup to make it so that the text within will be displayed in bold, and the two backslashes (\) above are there to prevent unwanted line breaks (see here) while still keeping the code readable.

Try it out. Hopefully you’ll like the effect.

Please let me know if you have any questions about how any of that works. :slight_smile:

1 Like

I have just started playing around with <span> so this is interesting to read!

I’m really glad I read this thread. What @HanonO said about the init passage being run before everything else so there were no other variables set yet was one of those

“Oh… yeah! Obvious now you’ve said it.” moments.

Would it be worth changing the title of this thread to something like “If macro not working in init passage” so that others will be able to search and find it later?

1 Like

Yeah, that’s a great idea. Looks like someone already changed it.

2 Likes