I am learning C# for purpose of making silly little games in Unity, possibly something more elaborate once I have a better foundation. Going pretty well so far, I have this little prototype thing I've been slowly putting together to test how well I understand the stuff I'm learning in videos. So this is more of an academic question, I ran into something I couldn't figure out and wanted to see if it's doable, and in the meantime I just used if else to make it happen and wait to see if a solution presents itself as I get better.
The prototype thing I'm doing is a little pseudo-adventure game. There's a player character with six stats, which are determined by his character class. Originally this was hardcoded, but I've set up a new thing so the game stores the class (RPG term, lowercase) definitions inside dictionaries in a separate Class (programming term, uppercase). The player Class then has a dictionary for its personal stats. It selects a random class and populates the dictionary from the global dictionary (using this separate Class setup because at some point I want to have multiple instances of the player character that can have different classes).
If that all makes sense, the code is below. There's three jobs (going to say job instead of class to avoid confusion), each stores its stats in a dictionary, and the three dictionaries are then in a dictionary of dictionaries, which was mainly to facilitate the thing I failed to do. The dictionary of dictionaries stores the jobs with integer keys. My plan was to use this to pick a random integer and then pass it as an argument for the GetPlayerClass function, but that didn't work. GetPlayerClass(GameControl.playerClasses[
x]) would only take a direct reference to the key, I couldn't get it to take an integer variable and recognize what it's supposed to be. So instead I used the if else thing at the end of the sequence, just a different function call for each class. My question is, is that possible? To use a single function call that can pass a randomly selected dictionary as a reference?
Maybe more importantly, is it worth doing that over just doing if else?
public class GameControl : MonoBehaviour
{
//define class dictionaries and dictionary of class dictionaries lol
public static Dictionary<int, Dictionary<string, int>> playerClasses = new Dictionary<int, Dictionary<string, int>>();
public static Dictionary<string, int> fighterStats = new Dictionary<string, int>();
public static Dictionary<string, int> rogueStats = new Dictionary<string, int>();
public static Dictionary<string, int> mageStats = new Dictionary<string, int>();
// Start is called before the first frame update
public void Start()
{
//add class dictionaries
playerClasses.Add(0, fighterStats);
playerClasses.Add(1, rogueStats);
playerClasses.Add(2, mageStats);
//add stats
fighterStats.Add("Combat", 5);
fighterStats.Add("Athletics", 3);
fighterStats.Add("Subtlety", 2);
fighterStats.Add("Magic", 2);
fighterStats.Add("Investigation", 3);
fighterStats.Add("Persuasion", 3);
rogueStats.Add("Combat", 3);
rogueStats.Add("Athletics", 3);
rogueStats.Add("Subtlety", 5);
rogueStats.Add("Magic", 2);
rogueStats.Add("Investigation", 3);
rogueStats.Add("Persuasion", 3);
mageStats.Add("Combat", 2);
mageStats.Add("Athletics", 3);
mageStats.Add("Subtlety", 3);
mageStats.Add("Magic", 5);
mageStats.Add("Investigation", 4);
mageStats.Add("Persuasion", 3);
}
public class Player : MonoBehaviour
{
//player stats
public Dictionary<string, int> playerStats = new Dictionary<string, int>();
public int Dice1 = 0;
public int Dice2 = 0;
public int DiceTotal = 0;
public int PlayerClass;
string StatCheck(int DieMax, int StatUsed)
{
Dice1 = Random.Range(1, DieMax);
Dice2 = Random.Range(1, DieMax);
DiceTotal = Dice1 + Dice2 + StatUsed;
return "Dice Roll: " + Dice1 + ", " + Dice2 + ", " + StatUsed + ", Total " + DiceTotal;
}
//assign stats to player based on class
public void GetPlayerClass(Dictionary<string, int> playerClass)
{
playerStats.Add("Combat", playerClass["Combat"]);
playerStats.Add("Athletics", playerClass["Athletics"]);
playerStats.Add("Subtlety", playerClass["Subtlety"]);
playerStats.Add("Magic", playerClass["Magic"]);
playerStats.Add("Investigation", playerClass["Investigation"]);
playerStats.Add("Persuasion", playerClass["Persuasion"]);
Debug.Log("it works bitch.");
}
// Start is called before the first frame update
void Start()
{
//fuck it
PlayerClass = Random.Range(0, 2);
if (PlayerClass == 0)
{
GetPlayerClass(GameControl.playerClasses[0]);
}
else
{
if (PlayerClass == 1)
{
GetPlayerClass(GameControl.playerClasses[1]);
}
else
{
GetPlayerClass(GameControl.playerClasses[2]);
}
}
}