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
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;
}
}
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.
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”.