# I haven't been able to figure out how to do a dice roll in Sugarcube

+2 votes
I've been looking for a way to roll dice in Sugarcube. I ran across one in harlow, but I don't know how to make that a working Sugarcube format. The sugarcube ones I found were only partial code, not a full example that I could copy and change.

I am not a programmer. All I can really do is copy and paste and change some of the words around to fit my story.

I am just looking for a way for a person to roll 1 die, have a random outcome, then have something happen based on the outcome of the die.

Any help is appreciated.

Thanks

## 1 Answer

+1 vote
by (63.1k points)

You can use the provided random() or either() functions.

``````<<set \$roll to random(1, 6)>>

<<set _roll to either(1, 2, 3, 4, 5, 6)>>

<<if \$roll gte 4>>
do something
<</if>>

<<if _roll is 6>>
do something
<<else>>
do another thing
<</if>>

Etc. ``````

All things being equal, random() is easier to use in most cases, but I've heard either() is better if you only have a few values.

Note that variables starting with an _ are temporary, and get cleared on passage transition, but are better for your story when you only need a value for a bit.

by (170 points)
So then I need to make a button and point it to do "\$roll" ?
by (63.1k points)

You can do that, sure.

``````::some passage
<<button 'Roll a die.'>>
<<set \$roll to random(1, 6)>>
<<goto 'next passage'>>
<</button>>

::next passage
You rolled a \$roll.

<<if \$roll is 6>>
You're pretty lucky.
<</if>>``````

by (170 points)
``````Roll a 4 or better

<<button 'Roll a die.'>>
<<set \$roll to random(1, 6)>>
<<if \$roll lt 4>>
Try again
<<else>>
[Well Done]
<</if>>
<</button>>``````

What am I doing wrong here? Thanks.

by (63.1k points)

There's no place for your output to go, which is why my example looks the way it does.  Code inside a <<button>> or <<link>> macro is silent, which means it generates no visible output to the screen regardless of the code that it runs.

You need to either:

1. Create an element and replace it, like we did with the textbox code the other day, or
2. Set the variable in the button and forward the user to a new passage to evaluate the code and generate output, which is what my above example does.
by (170 points)

This is what I put together after revisiting the 'replace" commands from yesterday. This seems to be working.

``````Roll a 4 or better
<span id='dice-outcome'></span>

<<button 'Roll a die.'>>
<<set \$roll to random(1, 6)>>
<<if \$roll gte 4>>
<<replace '#dice-outcome'>>\
You rolled a \$roll. [[Well Done]]
<</replace>>
<<else>>
<<replace '#dice-outcome'>>\
You rolled a \$roll. Try Again
<</replace>>
<</if>>
<</button>>

``````

But what can I do if when they get the correct roll, I want to make the button go away without leaving the current passage?  Thanks.

by (63.1k points)
edited by

You can do it the same way, using <span>s and a <<replace>>:

``````Roll a 4 or better
<span id='dice-outcome'></span>

<span id='dice-button'>\
<<button 'Roll a die.'>>
<<set \$roll to random(1, 6)>>
<<if \$roll gte 4>>
<<replace '#dice-outcome'>>\
You rolled a \$roll. [[Well Done]]\
<</replace>>
<<replace '#dice-button'>><</replace>> /% remove button %/
<<else>>
<<replace '#dice-outcome'>>\
You rolled a \$roll. Try Again\
<</replace>>
<</if>>
<</button>>\
</span>``````

You could also hide or remove the element with jQuery, but this is simpler.

by (159k points)

@forktie I would suggest you try an be more consistent with the indentation and formatting of your code, it will help make it more readable (thus understandable) to both and anyone else that reads it.

eg. Your original example

``````Roll a 4 or better
<span id='dice-outcome'></span>

<<button 'Roll a die.'>>
<<set \$roll to random(1, 6)>>
<<if \$roll gte 4>>
<<replace '#dice-outcome'>>\
You rolled a \$roll. [[Well Done]]
<</replace>>
<<else>>
<<replace '#dice-outcome'>>\
You rolled a \$roll. Try Again
<</replace>>
<</if>>
<</button>>
``````

... and the same example reformatted. (and using TAB characters for indentation)

``````Roll a 4 or better
<span id='dice-outcome'></span>

<<button 'Roll a die.'>>
<<set \$roll to random(1, 6)>>
<<if \$roll gte 4>>
<<replace '#dice-outcome'>>\
You rolled a \$roll. [[Well Done]]
<</replace>>
<<else>>
<<replace '#dice-outcome'>>\
You rolled a \$roll. Try Again
<</replace>>
<</if>>
<</button>>``````

by (170 points)
@Chapel Works perfectly. Hopefully I will get the hang of this. Thanks a ton.
by (170 points)
@greyelf excellent advice, thank you