Character Generator

i got it to roll 4d6 i have tried to add the dice together but that’s not going to well and im not sure how to put them in an array

Ok, good start on the rolling of the four dice.

The example.cs script GitHub gives you the following;

        // Usage example - multiple dice
        int[] results = Dice.Roll(Dice.DieType.D4, 5, Dice.SortOrder.Descending);

        for (int i = 0; i < results.Length; i++)
        {
            Debug.Log(results[i].ToString());
        }

The Roll method of the Dice class is going to return an array of ints for you, so all you need is a container to put those in.

In the above example from that script this is done where we declare (and populate) results;

int[] results

Our variable is called results and it is an array of type int.

The for code that following just iterates through it, arrays have an index for the elements they contain, so, if you have asked this method to roll 4 dice, then your index values would be; 0, 1, 2, 3, as the arrays are zero based, e.g. the first index is 0 not 1.

You could use that loop, but change how many iterations you want, instead of saying, "keep looping through whilst i is less than the length of the array, you could say, whilst i is lower than 3.

I’m not a fan of hard coded values like that, but for the sake of having this work for you, lets try that initially.

So, you roll 4 dice, it returns the results in an int array, in descending order, and then you loop through that array one less time than the number of dice you roll, thus, the lowest number is ignored.

Final thing to do is add them up… so, outside of that for loop, create a variable of type int, and then within the for loop, add the value from the array at position i to it;

int sumOfDiceRolls = 0;

for (int i = 0; i < 3; i++)
{
    sumOfDiceRolls += results[i];
}

Debug.Log("Total was : " + sumOfDiceRolls);

Let me know if any of the above doesn’t make sense :slight_smile:

so the code is saying take 0,1,2 and drop 3 so if i had the code for the dice ascending it would drop the highest ones right?

That’s correct yes, because we are ignoring the last value of the array that is returned.

I don’t know the rules of the game you wish to build regarding the dice numbers, but rather than hard coding in that 3, what would be better to do is say, add up the dice totals for one less dice than we rolled, for example… so;

diceRolled - 1


its under standard

I see.

So, what you want to do is keep the dice class pretty much as is perhaps for now. It gives you the basic functionality of dice rolling.

Then, as your progress with your game you will invariably find other rules which require specific numbers/types of dice to be rolled, and some special rules around them, for example ignoring the lowest value.

When you have a good set of those rules that are implemented in a couple of others places, like our example.cs script at the moment, you may want to consider consolidating them into that dice.cs script, that way, all of the rules for the dice rolling would be in one place.

If you started to do this now though, at the beginning, you would run the risk of creating a lot of duplication, so better to create what you need as you go along, and then refactor back to reduce repetition, at least for now.

Challenge for you :slight_smile:

In this scene that you have created, pop a text element in front of the button and put a characteristic name in the text box.

Repeat so that you have all of the characterists and a button for each.


like so?

1 Like

Cool.

Now, how about adding another text box, after the characteristic label, push the buttons a bit more to the right, so it can fit in between… we can use that for outputting the sum of the rolls to the relevant box.

1 Like

Great.

So, we will need to extend that example.cs script a bit further now in order to deal with the different characteristics.

I would start by renaming it, it is no longer really an example, it has started to become your character creation class.

You can delete the DisplayMessage method now, we no longer need that. I would keep the simple method we created for the dice rolling, just for now.

Next, I would probably create a method stub for each of those characteristics. By stub I just mean an empty method, but with an appropriate name, but it won’t do anything yet. So for example;

public void RollStrength()
{
    // intentionally left empty
}

Name these as you wish, I would include the characteristic in the name though, and typically you would name a class / method based on what it is/does.

After you have created these 6 methods, for each of the buttons next to the characteristics, update the function in the drop down menu for the appropriate method that it will call, e.g. the strength button calls the RollStrength method etc.

On a related note, it would be advisable to name your GameObjects appropriately also.

So, before we go further, tidy up the scene with all of the text UI GameObjects named appropriately, the buttons and so on.

When you’ve done the above, post the full script you have at the time into a reply and we’ll do the next bit for outputting the sum of each roll to the relevant text box. :slight_smile:

i have a script called Character Creation with all of the classes in it already should i just add the dice roller to that one

If you want to, but I was only trying to give you some example bits and pieces and to get you started, so I don’t want to erase anything you have already been working on. Entirely you choice.

when i deleted the Display Message method everything has a red unerline

You may have taken another curly bracket out by accident.

Can you post the code amd pop the code formatting characters before and after it.


See also;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RollStats : MonoBehaviour {

    
    {
        int[] results = Dice.Roll(Dice.DieType.D6, 4, Dice.SortOrder.Descending);
        


        for (int i = 0; i < results.Length; i++)

        {

            Debug.Log(results[i].ToString());

            
        }
        int sumOfDiceRolls = 0;

        for (int i = 0; i < 3; i++)
        {
            sumOfDiceRolls += results[i];
        }

        Debug.Log("Total was : " + sumOfDiceRolls);

    }
    public void RollStrength()
    {
        // intentionally left empty
    }
    public void RollDexterity()
    {
        // intentionally left empty
    }
    public void RollConsititution()
    {
        // intentionally left empty
    }
    public void RollIntelligence()
    {
        // intentionally left empty
    }
    public void RollWisdom()
    {
        // intentionally left empty
    }
    public void RollCharisma()
    {
        // intentionally left empty
    }
}

The curly brace above RollStrength is the one for closing the class. Each opening bracket/brace needs a corresponding closing one. In this case all your new methods are outside of the class definition.

Infact, now I have added the code formatting, the code you have copied from the example.cs file on GitHub isnt actually within a method definition anymore.

Perhaps just comment that out for now.

that just adding // before public void DisplayMessage() right?

On each line you want to comment out…

i just changed the name DisplayMessage() to Dice Roller and it fixed it lol

Privacy & Terms