Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Making a complex dice input. Results problem.

Discussion in 'Scripting' started by Kirby26, Sep 2, 2019.

  1. Kirby26

    Kirby26

    Joined:
    Mar 3, 2019
    Posts:
    13
    Hi. Im making a complex dice roller for unity who get imputs like: !roll 1d20 +30 or !roll 1d40 +3.

    The code its almost done. But i dont know how to do maths with all of the random.range results. the number of them its the input of !roll "1"d20. the 1 in this case, but another person can put !roll "20"d10.

    Code (CSharp):
    1. public void LeerdadosYchat(InputField textoRecibido)
    2.     {
    3.         string[] SeparacionTexto = textoRecibido.text.Split(new string[] { " " }, System.StringSplitOptions.None);
    4.  
    5.         string PrimeraPalabra = SeparacionTexto[0];
    6.  
    7.        
    8.  
    9.         if (PrimeraPalabra=="!roll")
    10.         {
    11.             print("Estoy tirando un dado!");
    12.             string NumeroDeDadosYDados = SeparacionTexto[1];
    13.  
    14.             string[] SeparacionTexto2 = textoRecibido.text.Split(new string[] { "d" }, System.StringSplitOptions.None);
    15.  
    16.             int NumeroDeDados = int.Parse(SeparacionTexto2[0]);
    17.             int Dado = int.Parse(SeparacionTexto2[1]);
    18.  
    19.             int Sumas = int.Parse(SeparacionTexto[2]);
    20.  
    21.  
    22.  
    23.             for (int i = 0; i < NumeroDeDados; i++)
    24.             {
    25.                 Resultados[i] = Random.Range(1, Dado + 1);
    26.             }
    27.  
    28.  
    29.             //print();
    30.  
    31.         }
    32.  
    33.     }
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    For XdY, loop X times and sum the results of Random.Range(1, Y + 1).
     
  3. Kirby26

    Kirby26

    Joined:
    Mar 3, 2019
    Posts:
    13
    How i show the results of that array of results? how i can show them individually and the sum?

    like: your results are 1,5,7,33,7,56=sum
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    If you need to show all of the individual rolls, you can build the string inside the loop as you go or store the results in a List and iterate through that any time you need to refer back to the roll.