RPG rolling for stats

I’m trying to implement a system where you have your strength, intelligence and stamina and you click a button that randomly determines what your stats are gonna be. Doing that isn’t too hard, but the catch is that I want the numbers that were randomly generated to add up to equal to thirty-five.

For example: str: 9 int: 14 stam: 12 added up equals to 35
another dice roll: str:13 int: 14 int: 8 added up equals to 35

P.S The stats can’t go below 5

distribute 5 points to each stat, subtract all those from your total… so if 4 stats, subtract 20 from 35, leaving you with 15.

Loop this left over amount of times (15).

Apply 1 point to a random stat each loop.

Thanks for the reply, but if you don’t mind, can you show me the script to this. I have very little experience with C#.

Bump, I need help.

It’d be a fairly basic bit of code. You’d have a for loop iterate through the number of stat points you’d like to distribute. Pick a random value and then use a case switch to increment your stats. Try going through some of the tutorials, you’re going to run into some harder coding pretty quick.

This is a bit of pseudo code on what it might look like, I don’t actually know what your code looks like but i’m assuming you have each stat as it’s own variable.

Edit: Holy cow it butchered the formatting. Let me make that readable.

for(int x = 0; x < statPointsToAllocate; x++)
{
     int statroll = Random.Range(1,numberOfStats);
    switch (statroll)
           {
           case 3:
                str++;
               break;
           case 2:
               agi++;
               break;
           case 1:
               int++;
               break;
           default:
               Debug.LogError("Something went wrong, probbably with my Random.Range bounds!");
                x--;
           break;
       }

}

Sorry, but I don’t know what most of this code means. Thanks for the help anyways :slight_smile:

Show us what code you have currently…

I don’t have any code for this subject in particular, i don’t know how to do this other than the random.range

OK… so then what about Avo’s code are you not understanding?

Do you see how they loop the number of points to allocate (15 in my post).
Do you see how they then select a random number from 1 to the number of stats… int, stam, str would be 3.
Then they have a switch statement to associate 1,2,3 with one of the stats, and increment it one point.

Just like I described in my post.

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

public class statroll : MonoBehaviour {


    for(int x = 0; x < 20; x++)
    {
        int str, agi, inte;
        int statroll = Random.Range(1,3);
        switch (statroll)
        {
        case 3:
            str++;
            break;
        case 2:
            agi++;
            break;
        case 1:
            inte++;
            break;
        default:
            Debug.LogError("Something went wrong, probbably with my Random.Range bounds!");
            x--;
            break;
        }

    }
   

}

I don’t understand what I’m still doing wrong. I did everything you said and it’s giving me an error on a curly brackets and “for”.

this code needs to be inside a function… like maybe the Start function.

Also you need the stat variables defined somewhere that they are usable in the future, like as fields of this class.

You appear to be a very novice programmer… you might want to start with the basics of writing code before moving onto this.

Yes, thank you, and you’re right, I definitely need to be more familiar with programming before making games.