How do you emulate rolling dice values in a script?

Long story short, I’m trying to “roll” varying numbers of “dice,” with three tiers of dice of varying face values.

The question I have is how would I have the script “roll” multiple dice and get the sum value?

I already have the probability generators hooked up to some random ranges, but I need to call that function, get a value, and repeat that for however many “dice” the player is meant to roll. Last, I need to add those values together.

I’m sure this is a simple process that I simply can’t think of at the moment, but I would really appreciate any suggestions.

Obviously the simplest is just Random.Range().

int oneD6 = Random.Range( 1, 7);  // rolls 1 to 6

If you want something more useful, you could implement a string parser so you can specify a string in a dice notation such as "2d6+2" and have it plucked apart in real time and a resulting value returned. There’s actually dice parser code online you can start from, or roll your own.

Beyond that I suppose you would build up “useful” methods for whatever kind of game you are making.

Well, like I said, I already have a system that will get a random value, I’m asking how I get multiple values from that same system, then add them together.

For example: Say one player gets to roll 2d6 and another gets to roll 5d4, my system is more complicated than that, but none the less. Right now I can get one six-sided die and one four-sided die, but no more than one each. I need my system to do three things:

  1. Roll the same die multiple times.

  2. Allow me to change how many times it rolls that die.

  3. Add the results together.

I imagine something like a For Loop would work, but I’m still learning and I can’t seem to find an answer as to how I would get a value out of that.

Is there a way I can get a value out of each iteration and get the sum of those values?

Absolutely! You just add them.

Let me get you started so you can reason about what is involved.

Here’s a super-simple way to process something like 2d6+3:

So remember these variables: idj+k, where i, j and k are the 2, 6, and 3 above:

// I made k optional, defaults to zero if you don't supply it
int RollMe( int i, int j, int k = 0)
{
  int total = 0;
  while( i > 0)
  {
    // make each individual roll
    int roll = Random.Range( 1, j + 1);
    // add it up
    total += roll;
    // count down
    i--;
  }
  // add the adjustment:
  total += k;

  // back to the caller
  return total;
}

And you can paste as many of those together as you want, just keep summing the result back to a single integer.

// this is 2d6 + 5d4
int result = RollMe( 2, 6) + RollMe( 5, 4);

As I noted above, if you need a general purpose parser that can accept any string you throw at it, there’s PLENTY of examples out there to start from. Be sure to select one in C#.

Yes, perfect, this was pretty much exactly what I was trying to figure out how to write, but I didn’t know what to even google for an answer. I may still have some questions down the line, but I really appreciate this. Do you mind if I send you follow up questions as they come up?

Public posts only please, but certainly. Always best to start a fresh thread for each new question.

I had to do some trouble shooting, but in case someone comes across this later I noticed only one issue with the code you gave me: Because “int total = 0;” is within RollMe(), it always returned a 0 for the total at the end.

All this meant is I needed to have the line “int total = 0;” placed within whatever function was calling RollMe(). It will reset it to zero any time you call that function, like intended, but will give you a new total after the function was run.

This is essentially what it became:

private int total;

public void RollDice()
{
  total = 0;
  Rollme(int i, int j, int k = 0)
}

// I made k optional, defaults to zero if you don't supply it
int RollMe( int i, int j, int k = 0)
{
  while( i > 0)
  {
    // make each individual roll
    int roll = Random.Range( 1, j + 1);
    // add it up
    total += roll;
    // count down
    i--;
  }
  // add the adjustment:
  total += k;

  // back to the caller
  return total;
}

If you made a class level variable called total (not shown above), beware that line 4 is NOT setting it back to zero. Line 4 is creating a brand new local variable called total and setting that to zero.

If you doubt me, run RollDice() 100 times in a row and you’ll be amazed at how big total gets.

Ah, right, I see what you’re referring to, that was a translation issue on my end when posting. The code I shared wasn’t what I originally wrote, so I just missed that when trying to build an example.

None the less, I did test what you wrote, and the total was always 0 until I made that change.

My code works perfectly as advertised. You most likely have another variable named total you are reading, which means you didn’t call my code the way I showed you in the second snippet beginning with int result =.

Code never lives in a vacuum. It has to be used the way it was intended.

All together now, two blocks of code exactly copied from above, drop this MonoBehavior on an empty GameObject and press PLAY:

using UnityEngine;

// @kurtdekker
public class ItWorksJustFine : MonoBehaviour
{
    // I made k optional, defaults to zero if you don't supply it
    int RollMe(int i, int j, int k = 0)
    {
        int total = 0;
        while (i > 0)
        {
            // make each individual roll
            int roll = Random.Range(1, j + 1);
            // add it up
            total += roll;
            // count down
            i--;
        }
        // add the adjustment:
        total += k;

        // back to the caller
        return total;
    }

    void Update()
    {
        // this is 2d6 + 5d4
        int result = RollMe(2, 6) + RollMe(5, 4);

        Debug.Log(result);
    }
}

I feel like there’s some hostility and if that feeling is correct, I don’t really understand why. I was just pointing out something that didn’t work for me and made a fix. Some people may be using more complicated code in the future and would appreciate the explanation. I’m sure there are multiple solutions and multiple ways to use the system to gave me. Like I said, I appreciate it, but if I ran into this issue, someone else might too, just wanna future-proof.