Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Creeper_Math · Jan 13, 2017 at 07:31 PM · c#beginnerclassclasses

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Creeper_Math · Jan 13, 2017 at 08:04 PM 0
Share

Thanks! I kinda thought and fiddled around and did pretty much the same thing! Thanks a lot anyways!

avatar image Bunny83 · Jan 13, 2017 at 11:04 PM 0
Share

@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.

avatar image Creeper_Math Bunny83 · Jan 14, 2017 at 02:09 PM 0
Share

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

avatar image RobAnthem · Jan 14, 2017 at 05:38 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

276 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# Find specific object by getting one of its variables 0 Answers

Custom class, Null Reference Exception 4 Answers

How to get the component in a new class 0 Answers

Use a class like a function 1 Answer

(SOLVED) Accessing (string) arrays by enumerations from another class 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges