Accessing Variables inside classes inside other classes?
Ok.. I have this "character" class for an RPG style game I'm building... What I want to be able to do is to get the players abilities by using Character.Ability.Scores.Str
. This is what I've somehow stumbled upon, as this is my first class attempt, and I don't know IF it's possible and how I would do it if it is...
Here's my script so far... "StatDataBase.GetBaseStats" is just getting an array of ability scores from the class and race
public class Character {
// Get a brand new level 1 character
public Character(string Race, string Class) {
int[] NewScores = StatDataBase.GetBaseStats(Race, Class);
Ability.Scores.Str = NewScores[0];
Ability.Scores.Dex = NewScores[1];
Ability.Scores.Con = NewScores[2];
Ability.Scores.Wis = NewScores[3];
Ability.Scores.Int = NewScores[4];
Ability.Scores.Cha = NewScores[5];
}
// Get a character with ability modifications
public Character(string Race, string Class, int[] Modifiers) {
Stats = StatDataBase.GetBaseStats(Race, Class);
int Selection = 0;
foreach (int item in Modifiers) {
Stats[Selection] += Modifiers[Selection];
Selection++;
}
}
// can't access the variables in here when using 'Character.Ability.Scores.Str"
public class Ability {
public class Scores {
static public int Str;
static public int Dex;
static public int Con;
static public int Wis;
static public int Int;
static public int Cha;
}
}
}
Please try to explain any and all help as I know nothing about creating Classes!
Answer by RobAnthem · Jan 13, 2017 at 07:46 PM
You aren't too far off base, you basically just didn't give the character an actual ability stat, and to you would do that like this.
public class Character {
public Ability abilities;
public string race;
public string charClass;
// Get a brand new level 1 character
public Character(string Race, string Class) {
race = Race;
charCLass = Class;
int[] NewScores = StatDataBase.GetBaseStats(Race, Class);
abilities = new Ability();
abilities.scores.Str = NewScores[0];
abilities.scores.Dex = NewScores[1];
abilities.scores.Con = NewScores[2];
abilities.scores.Wis = NewScores[3];
abilities.scores.Int = NewScores[4];
abilities.scores.Cha = NewScores[5];
}
public class Ability
{
public Scores scores = new Scores();
public class Scores
{
public int Str;
public int Dex;
public int Con;
public int Wis;
public int Int;
public int Cha;
}
}
My suggestion to you though, would be to create another class file for character classes, so you can have seperate info for each class and aren't stuck trying to fudge some fake class out of a string name.
EDIT: Have you ever worked with Skript? I'm only asking because your code reflects a non-OOP influence, and I did Skript for minecraft servers for a while, and I gotta say it taught me some bad habits in coding.
Thanks! I kinda thought and fiddled around and did pretty much the same thing! Thanks a lot anyways!
@RobAnthem It seems the OP has already solved his problem, however i want to add that your code won't work ^^. "Scores" is just a nested class inside the Ability class. The Ability class itself doesn't have any data in it so creating an instance of it wouldn't make any sense at the current point.
Furthermore the nested Scores class only contains static elements. So the way the OP accessed those static members was actually correct.
If you want to have instances of the Ability class as well as the Scores class, the Ability class would need a variable of type "Scores" and the variables inside Scores must not be static.
public class Ability
{
public Scores scores = new Scores();
public class Scores
{
public int Str;
public int Dex;
public int Con;
public int Wis;
public int Int;
public int Cha;
}
}
And in that case you would access the variables like this:
abilities = new Ability();
abilities.scores.Str = ...
ps: why did you name the variable "abilities"? It's a single instance. The plural should only be used for arrays / lists.
The whole class design should be worked out in a way that makes sense. In the code of the question the Ability class has nothing in it and nothing that would define an "ability". The "Scores" seem to be more like a "Stats" class. I can imagine that a character has one or multiple abilities and a set of stats. The way it's currently designed it seems that an ability actually has stats and not the character.
What I have now is working completely, and this is following "Dungeons and Draogons" in which "abilities" is a "sub-category" of your stats, which also include Armor Class and for each Ability you have a "score" and a "modifier", in which the score is what you "rolled" for creating your character
Well Bunny was pointing out that my solution was only half the fix, because I did not realize you had done your stats as static. I should of read it better, because I look like a fool :P Anyway the reason I didn't see them as static is because it makes no sense to separate your stats from your character. Especially if you have any intention of a D&D style game, the stats should be able to be applied to any entity. Bunny83's solution would be the appropriate and most efficient way of doing things. As a side note, remember that static data should never be "real" data, because it cannot contain "instances", so as a good practice, even if you have a "static" abilities variable, then I'd still suggest making it a constructed object and use the static class for references. Still not reason to since your character should be an easy place to access.