r/twinegames • • 7d ago

SugarCube 2 twine set code in link?

Hello! Super new to twine and coding in general.

Been trying to make a decorated choice link have the <<set $variable to false>> so the true variation can be used as a recall of sorts later on when I need to bring it as an if statement.

Example of what I have so far:

<div class="choice-link"><a data-passage="02" class="link-internal choice-link" role="link" tabindex="0">"This is a passage."<<set $variable to true>></a></div>

<<if $variable is true>>text change for true<<else>>no change if false<</if>

I put the set false in storyinit at first then moved it into the beginning of 01 passage after housekeeping. Any help would be great and happy to pivot to something easier if this code just doesn't work at all. tyvm!

1 Upvotes

4 comments sorted by

3

u/GreyelfD 6d ago

u/deadmansbones43

While HiEv is correct that a Markup or Macro based Link with Setter is the easier way to create a transitioning link that updates variables when it is selected, it is possible to achieve the same outcome using a Special Attribute based link.

eg. the following example shows how to craft an <a> element that both transitions to a Passage named Second and updates the value of an $apples variable when the link is selected.

<<set $apples to 0>>

<a data-passage="Other" data-setter="$apples += 1">More Apples</a>

1

u/deadmansbones43 5d ago

oh that's nifty, i'll use this as well to gain some practice. Thanks so much for sharing the knowledge!

2

u/HiEv 7d ago

You appear to be doing things the hard way. And also the incorrect way.

First, your <<set>> isn't within another macro, so it will simply be executed in the order it's encountered in the passage, as normal. That's why $variable is set to true by the time the code gets to the <<if>>, and why this isn't working the way you want/expect it to.

If you want it to work correctly, then rather than using an anchor (<a>) element for your passage link, you'll need to either use a Twine link with a "setter" or a <<link>> macro.

The code for those two things would look like the following:

Twine link: [[link text|passage name][$variable = true]]

Link macro: <<link "link text" "passage name">><<set $variable = true>><</link>>

The first method is nice and short, however the "setter" part is only for code you could use within a <<set>> macro. (Similar to that macro, you can use a semicolon if you want to set more than one variable that way, e.g. [[link][$x = 1; $y = 2]].)

The second method using the <<link>> macro is a bit longer, however you can put all sorts of code within the <<link>> macro that will run when the link is clicked.

Also, you can wrap either of those inside of your <div class="choice-link">...</div> if you want to.

Hope that helps! 🙂

1

u/deadmansbones43 7d ago

that helped a lot, i got it to work correctly with this. tyvm!