0 votes
by (1.1k points)

Hello everybody!

Using SugarCube 2.21. What I want is to have things you can click on in a passage which add 1 to a variable, and when that variable reaches a certain number they are sent to another passage. I tried something like this:

<<link test>><<if $test < 6>><<set $test += 1>><</if>><<if $test is 6>><<goto "yay">><</if>><</link>>

I even tried nested <<linkreplace>> macros, but however many times I clicked the link the <<goto>> macro never worked. It worked if I just set $test to 6, but nothing seemed to work with counting up.

Any ideas would be appreciated!

Thank you all!

2 Answers

0 votes
by (23.6k points)
selected by
Best answer

I copied your code and it works for me without a problem. Did you properly set $test beforehand? Also it would be better to use a single if statement like this:

<<set $test to 0>>
<<link test>><<if $test < 6>><<set $test += 1>><<else>><<goto "yay">><</if>><</link>


by (1.1k points)
Ha ha! A world of possibilities unfoldeth! Thank you a lot.

If you are curious, I am using this to make a phone you can dial numbers on.
by (1.1k points)
Well, actually the phone ended up working a different way, but I used the counter for streaks in a mechanical game you find in the story.
+1 vote
by (154k points)

As explained by @idling you need to initialise a numerical variable before you can modify its value like you are doing within the body of your <<link>> macro call, generally the best place to do that initialising is within the StoryInit special passage like so:

<<set $test to 0>>


You should also wrap the linkText parameter of your <<link> macro within quotes, because it is a String value.

<<link "test">>


by (1.1k points)
Yes, I found out I needed the quotes for using <<linkreplace>>, but I did not know before because <<link>> worked without them, though I thought that was strange at the time.
Welcome to Twine Q&A, where you can ask questions and receive answers from other members of the community.

You can also find hints and information on Twine on the official wiki and the old forums archive.

See a spam question? Flag it instead of downvoting. A question flagged enough times will automatically be hidden while moderators review it.