0 votes
by (140 points)
I am new to twine. I am trying to put a random variable in a link.

This is my StoryInit passage

<<set $dice to 0>>
<<set $dice = "[6][$dice+=6]", "[5][$dice+=5]">>
<<set $randomDice = $dice.random()>>

In the start passage

Choose a roll of the dice

[["roll " + $randomDice|next]]
[["roll " + $randomDice|next]]

When I go to try it, it provides a link, but it says roll undefined. Eventually I want to put roll 1-6, but I only put two to try it out.

2 Answers

0 votes
by (590 points)

If you want to go to a random passage out of a predefined list (with similar names) you can do something like this:

<<<link "Roll" `"Roll " + random(1,6)`>><</link>>

This code creates a link with the text "Roll" and then randomly takes the user to a passage named Roll 1 through Roll 6 (you have to create them first though). The backticks cause that code to be evaluated before it is used. I prefer using <<link>> for tasks like this because it avoids creating dummy passages that you have to delete to make it work.

The nice thing about this version is that the value is random every time, and you don't have to store it anywhere. It also means you can have multiple links like this on the same page without them all linking to the same place.

by (156k points)

Information about using the back-ticks can be found in the Marcro Arguments documentation.

by (140 points)
How would this look in SugarCube 2-28-2 and can it go to the same link, but just increase your variable? Thanks for your help.
0 votes
by (41.9k points)
edited by

Part of the problem is that the StoryInit code is wrong.  In order for $dice.random() to work, $dice needs to be an array, like this:

<<set $dice = ["[6][$dice+=6]", "[5][$dice+=5]"]>>
<<set $randomDice = $dice.random()>>

However, in your code above, $dice was a actually string ("[6][$dice+=6]", because the rest after the comma would be ignored due to it not being within square brackets). And since that .random() wouldn't have worked, that was why $randomDice was set to undefined.

That said, you can't use that "corrected" code anyways, because if you did that you'd only get one value ever from $randomDice, since that value would only get set once.  Doing it that way doesn't keep giving you different random values.

For what I think you're trying to get, which is N rolls of six sided dice, you'd want to use a widget by creating a new passage with "widget" and "nobr" tags, and put this in it:

<<widget "rolls">>
<<capture _rollval>>
	<<for _rollno = 0; _rollno < $args[0]; _rollno++>>
		<<set _rollval = random(1, 6)>>
		<<link `"roll " + _rollval` $args[1]>><<set $dice = _rollval>><</link>><br>
	<</for>>
<</capture>>
<</widget>>

Then in your passage you could just do:

<<rolls 3 "next">>

and that would display three random rolls the player could pick from, and clicking any of those links would take them to the "next" passage and add the value of that roll to the $dice variable.  You could change that number and the string in quotes to change the number of rolls and the passage that they link to.  (Note that because the numbers are random, it's possible that some or all of the rolls could randomly be the same value.)

This works propertly because the <<capture>> macro "captures" the value of _rollval at the time the link is created.  Without "capturing" it, _rollval would only have the last value it was set to, so clicking any link would act as though the last link was the one clicked every time.

Also, the "backticks" (the ` on the ~ key) within the <<link>> macro causes what what's inside those backticks to be evaluated.  So, in the above code, the <<link>> macro sees `"roll " + _rollval` as a single parameter (e.g. something like "roll 5").

Hope that helps!  :-)

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.
...