Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

proper way to initialize classes in unity?

Discussion in 'Scripting' started by simonlvschal, Feb 28, 2017.

  1. simonlvschal

    simonlvschal

    Joined:
    Nov 17, 2015
    Posts:
    266
    so i got into a little problem with initialize in unity i properly never got it fully. but when i try to initialize a class and i got some information going through it doesn't initialize the values?

    Code (CSharp):
    1.  private PlayerStatLogic playerstat;
    2.  
    3.     // Use this for initialization
    4.     void Start () {
    5.         BasicClass NewClass = new BasicClass().Beginner();
    6.         playerstat = GetComponent<PlayerStatLogic>();
    7.         playerstat = new PlayerStatLogic();
    8.      
    9.         playerstat.PlayerHealth();
    10.         playerstat.PlayerMana();
    11.      
    12.  
    13.  
    14.     }
    and this is the class i have my values being set and stuff.

    Code (CSharp):
    1. public class PlayerStatLogic : PlayerStats {
    2.  
    3.     public Slider healthbar;
    4.     public Slider manabar;
    5.     public Text playerlevel;
    6.     public Text playername;
    7.  
    8.     public void PlayerHealth()
    9.     {
    10.         SetPlayerMaxHealth = GetBaseClassHealth;
    11.         SetPlayerHealth = GetBaseClassHealth;
    12.         SetPlayerCurrentHealth = GetPlayerHealth;
    13.  
    14.         Debug.Log("Max Health : " + GetPlayerMaxHealth);
    15.         Debug.Log("Current Health : " + GetPlayerCurrentHealth);
    16.         Debug.Log("Current Mana : " + GetPlayerCurrentMana);
    17.  
    18.    
    19.     }
    20.  
    21.     public void PlayerMana()
    22.     {
    23.         SetPlayerMaxMana = GetBaseClassMana;
    24.         SetPlayerMana = GetBaseClassMana;
    25.         SetPlayerCurrentMana = GetPlayerMana;
    26.  
    27.         Debug.Log("Max Health : " + GetPlayerMaxHealth);
    28.         Debug.Log("Current Health : " + GetPlayerCurrentHealth);
    29.         Debug.Log("Current Mana : " + GetPlayerCurrentMana);
    30.  
    31.      
    32.     }
    33.  
    34.     public float CalculateHealth()
    35.     {
    36.         return GetPlayerCurrentHealth / GetPlayerMaxHealth;
    37.     }
    38.  
    39.     public float CalculateMana()
    40.     {
    41.         return GetPlayerCurrentMana / GetPlayerMaxMana;
    42.     }
    43.  
    44.     public void DealDamage(float damagevalue)
    45.     {
    46.         SetPlayerCurrentHealth = GetPlayerCurrentHealth - damagevalue;
    47.         SetPlayerCurrentMana = GetPlayerCurrentMana - damagevalue;
    48.         Debug.Log(healthbar.value = CalculateHealth());
    49.         Debug.Log(manabar.value = CalculateMana());
    50.         Debug.Log("Current Health : " + GetPlayerCurrentHealth);
    51.         Debug.Log("Current Mana : " + GetPlayerCurrentMana);
    52.     }
    53.  
    54.     // Use this for initialization
    55.     void Start () {
    56.      
    57.         SetPlayerLevel = 1;
    58.         playerlevel.text = "Level : " + GetPlayerLevel.ToString();
    59.         SetPlayerName = "Reaper";
    60.         playername.text = GetPlayerName.ToString();
    61.         Debug.Log("ClassHealth : " + GetBaseClassHealth.ToString());
    62.         Debug.Log("ClassMana : " + GetBaseClassMana.ToString());
    63.         Debug.Log("ClassType : " + GetClassType.ToString());
    64.  
    65.         Debug.Log(healthbar.value = CalculateHealth());
    66.         Debug.Log(manabar.value = CalculateMana());
    67.     }
    68.  
    69.     // Update is called once per frame
    70.     void Update () {
    71.         if (Input.GetKeyDown(KeyCode.X))
    72.         {
    73.             DealDamage(6);
    74.         }
    75.     }
    76. }
    77.  
    but whenever i try to initialize the player class it wont set the values i have in playerstatlogic.
    if someone can help me better understand how to initialize classes in unity i would be greatful.

    oh and the last classes i got is purely used as a way ot storing values and creating ingame classes etc.

    playerstats is just a bunch of get/set where as ClassStats is also a get/set for classes, i got a ton of inheritences

    Code (CSharp):
    1. public class PlayerStats : ClassStats {
    2.  
    3.     private string _PlayerName;
    4.     public string GetPlayerName
    5.     {
    6.         get { return _PlayerName; }
    7.     }
    8.     public string SetPlayerName
    9.     {
    10.         set { _PlayerName = value; }
    11.     }
    12.  
    13.     private float _Health;
    14.     public float GetPlayerHealth
    15.     {
    16.         get { return _Health; }
    17.     }
    18.     public float SetPlayerHealth
    19.     {
    20.         set { _Health = value; }
    21.     }
    22.  
    23.     private float _MaxHealth;
    24.     public float GetPlayerMaxHealth
    25.     {
    26.         get { return _MaxHealth; }
    27.     }
    28.     public float SetPlayerMaxHealth
    29.     {
    30.         set { _MaxHealth = value; }
    31.     }
    32.  
    33.     private float _CurrentHealth;
    34.     public float GetPlayerCurrentHealth
    35.     {
    36.         get { return _CurrentHealth; }
    37.        
    38.     }
    39.     public float SetPlayerCurrentHealth
    40.     {  
    41.         set { _CurrentHealth = value; }
    42.     }
    43.  
    44.     private float _Mana;
    45.     public float GetPlayerMana
    46.     {
    47.         get { return _Mana; }
    48.     }
    49.     public float SetPlayerMana
    50.     {
    51.         set { _Mana = value; }
    52.     }
    53.  
    54.     private float _MaxMana;
    55.     public float GetPlayerMaxMana
    56.     {
    57.         get { return _MaxMana; }
    58.     }
    59.     public float SetPlayerMaxMana
    60.     {
    61.         set { _MaxMana = value; }
    62.     }
    63.  
    64.     private float _CurrentMana;
    65.     public float GetPlayerCurrentMana
    66.     {
    67.         get { return _CurrentMana; }
    68.  
    69.     }
    70.     public float SetPlayerCurrentMana
    71.     {
    72.         set { _CurrentMana = value; }
    73.     }
    74.  
    75.     private float _Exp;
    76.     public float GetPlayerExp
    77.     {
    78.         get { return _Exp; }
    79.     }
    80.     public float SetPlayerExp
    81.     {
    82.         set { _Exp = value; }
    83.     }
    84.  
    85.     private float _Level;
    86.     public float GetPlayerLevel
    87.     {
    88.         get { return _Level; }
    89.     }
    90.     public float SetPlayerLevel
    91.     {
    92.         set { _Level = value; }
    93.     }
    94.  
    95.     private BasicClassTypes _Class;
    96.     public BasicClassTypes GetClassType
    97.     {
    98.         get { return _Class; }
    99.     }
    100.     public BasicClassTypes SetClassType
    101.     {
    102.         set { _Class = value; }
    103.     }
    104.  
    105.  
    106.  
    107. }
     
    Last edited: Feb 28, 2017
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,715
    What is PlayerStats? By which I really mean: is PlayerStats a class that derives from MonoBehaviour?

    There are two kinds of classes in Unity: MonoBehaviours and everything else. "Everything else" should be initialized as you would in any other dev environment, e.g. with a constructor.

    If a class ultimately inherits from MonoBehaviour, though, it's not gonna use constructors, nor should you use the new keyword to create it. Instead, it is created either in the editor or with .AddComponent, and any initialization code you need, should be run in methods like Awake() [the first thing that gets run], OnEnable() [executed each time the object is activated], and Start() [best for initialization that depends on other objects, since the other objects' Awake will have already been run].
     
    Suddoha likes this.
  3. simonlvschal

    simonlvschal

    Joined:
    Nov 17, 2015
    Posts:
    266
    playerstats,classtats are both get/setter classes.
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,715
    What do you mean by "get/setter class"? That's not a thing. Do you mean a wrapper class?

    And the more important question was, do they inherit from MonoBehaviour?
     
  5. simonlvschal

    simonlvschal

    Joined:
    Nov 17, 2015
    Posts:
    266
    what i ment by get/setter class is just as u see above just a bunch of get/set prop, and yes Player inherit from Monobehaviour. should i get rid of it>? and do something else
     
  6. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    This still doesn't answer the question ,which seems really straightforward. Does PlayerStats which inherits from ClassStats ultimately inherit from Monobehaviour? Your Player class clearly does since you posted a snippet of it with a Start method.

    The reason being is MonoBehaviour classes are created with the new keyword like any other class. But Unity does it for you behind the scenes. If you then try to create another PlayerStats using the new keyword your making an entirely new instance of this class that is NOT the one in your scene.

    I am assuming somewhere down the chain you are in fact inheriting Monobehaviour for PlayerStatLogic because of this line:
    Code (CSharp):
    1.  playerstat = GetComponent<PlayerStatLogic>();
    \

    For that to work I assume your PlayerStatLogic script is attached to your GameObject
    so that code right there correctly gets the PlayerStatLogic instance you need. However you are immediately throwing it away and overwriting it with this:
    Code (CSharp):
    1.  playerstat = new PlayerStatLogic();
    2.      
    Now all you code that calls playerstat.Whatever() is operating on an instance of PlayerStatLogic.. but this instance isn't in your scene at all. It is definitely not the PlayerStatLogic on your Player. Just take out that line and use the first GetComponentCall.

    As soon as you attached PlayerStatLogic script to your Player GameObject Unity did all the "new PlayerStatLogic()" calls for you.
     
  7. simonlvschal

    simonlvschal

    Joined:
    Nov 17, 2015
    Posts:
    266
    ultimately no nothing but player has access to monobehaviour or inherit it
     
  8. simonlvschal

    simonlvschal

    Joined:
    Nov 17, 2015
    Posts:
    266
    okay so far i got the correct things loading i believe however i am still unable to get the values i set out. atm all values are just 0
     
  9. simonlvschal

    simonlvschal

    Joined:
    Nov 17, 2015
    Posts:
    266
    welp i solved all problems apart from Argumentexpception getcomponent requires that the requested component 'Player' derives from monobehaviour or component or is an inferfact and player does not derive from mono or any other componenets
     
  10. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    That makes this line odd then:
    Code (CSharp):
    1. playerstat = GetComponent<PlayerStatLogic>();
    Is PlayerStatLogic a script attached to the GameObject in your scene. If it is its required that it inherits from MonoBehaviour. Otherwise the editor wouldn't let you add it to the scene. Heh and i just noticed it does have a Start and Update. So clearly something down the inheritance chain is inheriting from Monobehaviour

    So this code here is the part that is getting the PlayerStatLogic Component and setting the values
    Code (CSharp):
    1.  playerstat = GetComponent<PlayerStatLogic>();
    2. playerstat.PlayerHealth();
    3. playerstat.PlayerMana();
    And PlayerHealth Looks like this:
    Code (CSharp):
    1. public void PlayerHealth()
    2.     {
    3.         SetPlayerMaxHealth = GetBaseClassHealth;
    4.         SetPlayerHealth = GetBaseClassHealth;
    5.         SetPlayerCurrentHealth = GetPlayerHealth;
    6.  
    Is GetBaseClassHealth a property in the ClassStats class? You didn't post it in any of your code. Is it actually being set to anyting?
     
  11. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Your code is very confusing especially in snippets. Could you post the code for Player and PlayerStatLogic that includes the class declaration. Your initial code snippet cut that off. Does anything you have inherit MonoBehaviour? I don't see how you using Unity or making GameObjects without it.
     
  12. simonlvschal

    simonlvschal

    Joined:
    Nov 17, 2015
    Posts:
    266
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Player : PlayerStatLogic
    4. {
    5.  
    6.  
    7.     public Player()
    8.     {
    9.         startplayer();
    10.     }
    11.  
    12.     public void startplayer()
    13.     {
    14.         BasicClass newclass = new BasicClass();
    15.         newclass.Beginner();
    16.         PlayerHealth();
    17.         PlayerMana();
    18.         PlayerLevel();
    19.         PlayerName();
    20.      
    21.     }
    22. }
    23.  
    24.    
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6.  
    7. public class PlayerStatLogic : PlayerStats {
    8.  
    9.     public Slider healthbar;
    10.     public Slider manabar;
    11.     public Text playerlevel;
    12.     public Text playername;
    13.  
    14.  
    15.  
    16.     public void PlayerHealth()
    17.     {
    18.         SetPlayerMaxHealth = GetBaseClassHealth;
    19.         SetPlayerHealth = GetBaseClassHealth;
    20.         SetPlayerCurrentHealth = GetPlayerHealth;
    21.  
    22.         Debug.Log("Max Health : " + GetPlayerMaxHealth);
    23.         Debug.Log("Current Health : " + GetPlayerCurrentHealth);
    24.         Debug.Log("Current Mana : " + GetPlayerCurrentMana);
    25.  
    26.      
    27.     }
    28.  
    29.     public void PlayerMana()
    30.     {
    31.         SetPlayerMaxMana = GetBaseClassMana;
    32.         SetPlayerMana = GetBaseClassMana;
    33.         SetPlayerCurrentMana = GetPlayerMana;
    34.  
    35.         Debug.Log("Max Health : " + GetPlayerMaxHealth);
    36.         Debug.Log("Current Health : " + GetPlayerCurrentHealth);
    37.         Debug.Log("Current Mana : " + GetPlayerCurrentMana);
    38.  
    39.      
    40.     }
    41.  
    42.     public void PlayerLevel()
    43.     {
    44.         SetPlayerLevel = 1;
    45.         playerlevel.text = "Level : " + GetPlayerLevel.ToString();
    46.     }
    47.  
    48.     public void PlayerName()
    49.     {
    50.         SetPlayerName = "Reaper";
    51.         playername.text = GetPlayerName.ToString();
    52.     }
    53.  
    54.     public float CalculateHealth()
    55.     {
    56.         return GetPlayerCurrentHealth / GetPlayerMaxHealth;
    57.     }
    58.  
    59.     public float CalculateMana()
    60.     {
    61.         return GetPlayerCurrentMana / GetPlayerMaxMana;
    62.     }
    63.  
    64.     public void DealDamage(float damagevalue)
    65.     {
    66.         SetPlayerCurrentHealth = GetPlayerCurrentHealth - damagevalue;
    67.         SetPlayerCurrentMana = GetPlayerCurrentMana - damagevalue;
    68.         Debug.Log(healthbar.value = CalculateHealth());
    69.         Debug.Log(manabar.value = CalculateMana());
    70.         Debug.Log("Current Health : " + GetPlayerCurrentHealth);
    71.         Debug.Log("Current Mana : " + GetPlayerCurrentMana);
    72.     }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class World : MonoBehaviour {
    6.  
    7.    
    8.  
    9.     public void GameWorld()
    10.     {
    11.         Player newplayer = GetComponent<Player>();
    12.         newplayer.startplayer();
    13.        
    14.  
    15.     }
    16.  
    17.     public World()
    18.     {
    19.        
    20.     }
    21.    
    22.  
    23.     // Use this for initialization
    24.     void Start () {
    25.        
    26.     }
    27.    
    28.     // Update is called once per frame
    29.     void Update () {
    30.        
    31.     }
    32. }
    i reworked it and it should work but i get that argument failur.

    and note i completly forgot to say that i am using a World class to hold all things that is going to be in a world.
     
    Last edited: Feb 28, 2017
  13. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    GetComponent is only used for MonoBehaviours. Since a Monobehaviour is attached to a gameObject Unity creates the instance for you ( runs the new keyword for you behind the scenes). Since it does this you need a way to get a handle on that object. Thats what GetCompnent is for.

    However, your Player Class is just a data object sittting in your project its not attached to any GameObject (It can't be since it does not inherit from MonoBehaviour). So you just need to create it the normal way using the new keyword.

    Additionally Since MonoBehaviours are created for you by Unity you should NOT have public constructor. Just use Awake and Start. Change your GameWorld script to this:
    Code (CSharp):
    1. public class World : MonoBehaviour {
    2.     // Use Awake to initialize things about ourself
    3.     void Awake()
    4.     {
    5.         Player newplayer = new Player();
    6.         newplayer.startplayer();
    7.     }
    8.  
    9.    // Use Start to initialize things that require other scripts
    10.     void Start () {
    11.      
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update () {
    16.      
    17.     }
    18. }
    So quick overview:
    If a class inherits from Monobehaviour
    • You attach to game Objects in your scene
    • Unity creates the instance for you. Don't use new to create a copy.
    • Use GetComponent<Class>() to get a handle to the object that Unity created
    • Do NOT have public constructors .. public Class()
    • Use Awake() and Start() to initialize things
    If a class doesn't inherit from MonoBehaviour
    • This is a "normal" C# class that just sits in your project
    • You are not allowed to add it to GameObjects
    • You must use Class myClass = new Class() to make a new copy of it
    • Use public Constructors public Class() to initialize it.
     
  14. simonlvschal

    simonlvschal

    Joined:
    Nov 17, 2015
    Posts:
    266
    oright thanks for your help. gonna test it in few
     
  15. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    The following is rather a little off-topic, but still worth to be mentioned:

    I'd like to kindly recommend to follow some naming conventions.
    Yo do this almost everywhere:

    Code (CSharp):
    1. private int _someField;
    2.  
    3. public int GetSomeField
    4. {
    5.     get { return _someField; }
    6. }
    7.  
    8. public int SetSomeField
    9. {
    10.     set { _someField = value; }
    11. }
    Don't get me wrong, this works flawlessly but it's a really unconventional way to name properties.
    Usually, you have a common name when using C# properties, as they have field-like syntax, i.e. when you write some code, it looks as if they were just some fields. (In the end methods will be generated anways, but that's not the point.)

    Additionally, it shortens your code and defines the accessors in one block, which is nice since they're always defined in the same place using the same name.

    Code (CSharp):
    1. private int _someField;
    2.  
    3. public int SomeField
    4. {
    5.     get { return _someField; }
    6.     set { _someField = value; }
    7. }
    That's how it's usually done.

    In contrast, if it says GetXXX and SetXXX, one usually expects these accessors to be implemented as methods:

    Code (CSharp):
    1. private int _someField;
    2.  
    3. public int GetSomeField()
    4. {
    5.     return _someField;
    6. }
    7.  
    8. public void SetSomeField(int value)
    9. {
    10.     _someField = value;
    11. }
    In the end it's the result will be the same, and it's really up to you whether you ignore this or find this helpful.
     
    simonlvschal and StarManta like this.
  16. p1zzaman

    p1zzaman

    Joined:
    Jan 1, 2017
    Posts:
    64
    Does PlayerStats extend anything. Also you really should look into using composition pattern rather than inheritance here. A Player is not a type of PlayerStats ultimately.

    In your game world in you need to call the constructor of Player

    Code (csharp):
    1. Player newplayer = new Player();
     
  17. p1zzaman

    p1zzaman

    Joined:
    Jan 1, 2017
    Posts:
    64
    First break the inheritance, then do the following to Player

    Code (CSharp):
    1. public class Player{
    2.  
    3.   private PlayerStats playerStats;
    4.   private PlayerStatLogic playerStatLogic;
    5.  
    6.   public Player(PlayerStats playerStats, PlayerStatLogic playerStatLogic) {
    7. this.playerStats = playerStats;
    8. this.playerStatLogic = playerStatLogic
    9. }
    10.  
    11. }
    This way the logic of playerStats and PlayerStatLogic is decoupled from and you can make alterations to the code in those classes without affecting Player. They are now seen as dependencies instead.

    Think of composition as "has-a" rather than "is-a"

    Player has a playerStat

    If you need multiple implementations of the class playerStats and playerStatLogic, you can make an interface for both of them.
     
  18. simonlvschal

    simonlvschal

    Joined:
    Nov 17, 2015
    Posts:
    266
    ye i know naming and stuff :D however it isnt my concern atm
     
  19. simonlvschal

    simonlvschal

    Joined:
    Nov 17, 2015
    Posts:
    266
    ye i was just trying some stuff.
    however player is the parent of them and i have trouble getting the values instantiated they are just returning null
     
  20. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    And where do you set them? In the inspector?
    We don't know that unless you either post the pieces of code that set values programatically or tell us that you've set them in the inspector.
     
  21. simonlvschal

    simonlvschal

    Joined:
    Nov 17, 2015
    Posts:
    266
    sorry i ment Player was Parent of objects

    for example i have a class here that will hold information about Basic types of classes.

    Code (CSharp):
    1. public class BasicClass : PlayerStats {
    2.  
    3.     public BasicClass()
    4.     {
    5.      
    6.     }
    7.  
    8.     public BasicClass Beginner()
    9.     {
    10.         SetClassType = BasicClassTypes.Beginner;
    11.         SetBaseClassHealth = 50f;
    12.         SetBaseClassHealthModifier = 1.2f;
    13.         SetBaseClassMana = 50f;
    14.         SetBaseClassManaModifier = 1.2f;
    15.         return this;
    16.     }
    17.    
    18.     public BasicClass Warrior()
    19.     {
    20.      
    21.         return this;
    22.        
    23.     }
    24.  
    25.     public BasicClass Mage()
    26.     {
    27.      
    28.         return this;
    29.     }
    30.  
    31.     public BasicClass Thief()
    32.     {
    33.      
    34.         return this;
    35.     }
    36.  
    37.     public BasicClass Bowman()
    38.     {
    39.      
    40.         return this;
    41.     }
    42.  
    43.  
    44. }
    and i am trying to create a class that is being used for Player stats logic, aka strength, health and so on

    Code (CSharp):
    1. public class PlayerStatLogic : PlayerStats {
    2.  
    3.     public Slider healthbar;
    4.     public Slider manabar;
    5.     public Text playerlevel;
    6.     public Text playername;
    7.  
    8.     public PlayerStatLogic()
    9.     {
    10.        
    11.  
    12.     }
    13.  
    14.    
    15.  
    16.     public void PlayerHealth()
    17.     {
    18.         SetPlayerMaxHealth = GetBaseClassHealth;
    19.         SetPlayerHealth = GetBaseClassHealth;
    20.         SetPlayerCurrentHealth = GetPlayerHealth;
    21.  
    22.         Debug.Log("Max Health : " + GetPlayerMaxHealth);
    23.         Debug.Log("Current Health : " + GetPlayerCurrentHealth);
    24.         Debug.Log("Current Mana : " + GetPlayerCurrentMana);
    25.  
    26.      
    27.     }
    28.  
    29.     public void PlayerMana()
    30.     {
    31.         SetPlayerMaxMana = GetBaseClassMana;
    32.         SetPlayerMana = GetBaseClassMana;
    33.         SetPlayerCurrentMana = GetPlayerMana;
    34.  
    35.         Debug.Log("Max Health : " + GetPlayerMaxHealth);
    36.         Debug.Log("Current Health : " + GetPlayerCurrentHealth);
    37.         Debug.Log("Current Mana : " + GetPlayerCurrentMana);
    38.  
    39.        
    40.     }
    41.  
    42.     public void PlayerLevel()
    43.     {
    44.         SetPlayerLevel = 1;
    45.         playerlevel.text = "Level : " + GetPlayerLevel.ToString();
    46.     }
    47.  
    48.     public void PlayerName()
    49.     {
    50.         SetPlayerName = "Reaper";
    51.         playername.text = GetPlayerName.ToString();
    52.     }
    53.  
    54.     public float CalculateHealth()
    55.     {
    56.         return GetPlayerCurrentHealth / GetPlayerMaxHealth;
    57.     }
    58.  
    59.     public float CalculateMana()
    60.     {
    61.         return GetPlayerCurrentMana / GetPlayerMaxMana;
    62.     }
    63.  
    64.     public void DealDamage(float damagevalue)
    65.     {
    66.         SetPlayerCurrentHealth = GetPlayerCurrentHealth - damagevalue;
    67.         SetPlayerCurrentMana = GetPlayerCurrentMana - damagevalue;
    68.         Debug.Log(healthbar.value = CalculateHealth());
    69.         Debug.Log(manabar.value = CalculateMana());
    70.         Debug.Log("Current Health : " + GetPlayerCurrentHealth);
    71.         Debug.Log("Current Mana : " + GetPlayerCurrentMana);
    72.     }
     
  22. p1zzaman

    p1zzaman

    Joined:
    Jan 1, 2017
    Posts:
    64
    Ok from the code you have presented so far... I see the following inheritance

    PlayerStats -> PlayerStatLogic -> Player
    PlayerStats -> BasicClass

    Ok so you have PlayerStatLogic and BasicClass extending base class PlayerStats

    Are you expecting BasicClass to have impact on the Player? From the class design, that won't happen because Player doesn't extend BasicClass. Player will not inherit the class values of BasicClass

    What is this? What is GetBaseClassHealth?
    Code (csharp):
    1.  
    2. public void PlayerHealth() {
    3.         SetPlayerMaxHealth = GetBaseClassHealth;
    4.         SetPlayerHealth = GetBaseClassHealth;
    5.         SetPlayerCurrentHealth = GetPlayerHealth;
    6.  
    7.         Debug.Log("Max Health : " + GetPlayerMaxHealth);
    8.         Debug.Log("Current Health : " + GetPlayerCurrentHealth);
    9.         Debug.Log("Current Mana : " + GetPlayerCurrentMana);
    10.     }
    11.  
     
  23. p1zzaman

    p1zzaman

    Joined:
    Jan 1, 2017
    Posts:
    64
    It sounds like you want a Player class, and different c# classes to represent the Player Class correct?

    So like BasicClass(), FighterClass(), MageClass()? And depending on what the Player extends, he is of that character class. Am I understanding this correct?

    Are PlayerStats, PlayerStatLogic interfaces or concrete classes?
     
  24. simonlvschal

    simonlvschal

    Joined:
    Nov 17, 2015
    Posts:
    266
    yes that is sorta what i am looking for. like Player has a BasicClass etc, and playerstats,playerstatlogic are going to be seen as concreate classes that holds information about how the logic of the players health,mana,strength etc works.

    after doing some changes, Player is no longer a parent or child of any other classes, But.

    Code (CSharp):
    1.  
    2. public class Player
    3. {
    4.  
    5.  
    6.     public Player()
    7.     {
    8.         BasicClass newclass = new BasicClass();
    9.         newclass.Beginner();
    10.  
    11.         PlayerStatLogic statlogic = new PlayerStatLogic();
    12.    
    13.         statlogic.PlayerHealth();
    14.         statlogic.PlayerMana();
    15.         statlogic.PlayerLevel();
    16.         statlogic.PlayerName();
    17.  
    18.    
    19.     }
    20.  
    21.  
    22. }
    23.  
    24.    
    Code (CSharp):
    1.   private float _BaseClassHealth;
    2.     public float GetBaseClassHealth
    3.     {
    4.         get { return _BaseClassHealth; }
    5.     }
    6.     public float SetBaseClassHealth
    7.     {
    8.         set { _BaseClassHealth = value; }
    9.     }
    10.  
    11.     private float _BaseClassMana;
    12.     public float GetBaseClassMana
    13.     {
    14.         get { return _BaseClassMana; }
    15.     }
    16.     public float SetBaseClassMana
    17.     {
    18.         set { _BaseClassMana = value; }
    19.     }
    20.  
    21.     private float _BaseClassHealthModifier;
    22.     public float GetBaseClassHealthModifier
    23.     {
    24.         get { return _BaseClassHealthModifier; }
    25.     }
    26.     public float SetBaseClassHealthModifier
    27.     {
    28.         set { _BaseClassHealthModifier = value; }
    29.     }
    30.  
    31.     private float _BaseClassManaModifier;
    32.     public float GetBaseClassManaModifier
    33.     {
    34.         get { return _BaseClassManaModifier; }
    35.     }
    36.     public float SetBaseClassManaModifier
    37.     {
    38.         set { _BaseClassManaModifier = value; }
    39.     }
    40.  
    41.     public enum BasicClassTypes
    42.     {
    43.         Beginner,
    44.         Warrior,
    45.         Mage,
    46.         Thief,
    47.         Bowman
    48.  
    49.     }
    this one will be mainloop that holds updating and starting different things,

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class World : MonoBehaviour {
    6.  
    7.  
    8.  
    9.     void Awake()
    10.     {
    11.         Player newplayer = new Player();
    12.      
    13.     }
    14.  
    15.  
    16.     // Use this for initialization
    17.     void Start () {
    18.      
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update () {
    23.      
    24.     }
    and the inherit of classes go as following
    PlayerStatLogic > PlayerStats

    MasterClass > AdvancedClass > BasicClass > PlayerStats > ClassStats

    MasterClass inherit AdvancedClass, Advanced Inherit BasicClass inherit PlayerStats which inheit ClassStats and PlayerStatLogic inherit PlayerStats
     
    Last edited: Mar 1, 2017
    p1zzaman likes this.
  25. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    The reason for all the struggles you're facing is that you're still trying to put most of it into one big inheritence hierarchy.

    'PlayerStats' is not meant to be the base for a 'BaseClass', You're litereally expressing that every class 'is' a set of stats instead of 'has' a set of stats. This doesn't work. Don't try to be a magician.

    Imagine I'd tell you:
    House -> Building -> Rooms
    or
    Car -> Vehicle -> Tires

    Wait what? This ain't gonna work either. Inheritence can be a nice tool, but only if you use it apprropriately.

    The most basic you can do is to follow @p1zzaman's advice which he had posted earlier:

    and so on...
     
    p1zzaman likes this.
  26. simonlvschal

    simonlvschal

    Joined:
    Nov 17, 2015
    Posts:
    266
    so your saying if i only inherit what should be inherited like lets say BasicClass,AdvanceClass,MasterClass. like MasterClass inherit from AdvanceClass inherit from BasicClass? since those 3 will have something to do with eachother
     
  27. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    That would be something which is more reasonable.

    However, you could also consider to use even more composition in your class architecture. It's a good alternative to inheritence and Unity heavily uses a component base design too.
     
  28. simonlvschal

    simonlvschal

    Joined:
    Nov 17, 2015
    Posts:
    266
    how do i go about using composition in unity?
     
  29. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You can find a lot of information on the internet.

    But an example on how to approach something like that has been posted by
    @p1zzaman in post #17 (the post that I also quoted earlier).
    Just read the post and get a little into it with tutorials etc.

    It's generally a different yet a very effective way to model an association / dependency between classes and it's often more flexible in terms of modularity, because it solves various issues that may arise in complex inheritence hierarchies.
     
  30. simonlvschal

    simonlvschal

    Joined:
    Nov 17, 2015
    Posts:
    266
    what still doesnt get me is that the code inside BasicClass Beginner doesnt actually work or even sets the value.

    Code (CSharp):
    1.  
    2. private PlayerStats playerstat = new PlayerStats();
    3.  
    4.     public BasicClass()
    5.     {
    6.      
    7.     }
    8.  
    9.     public BasicClass Beginner()
    10.     {
    11.         playerstat.SetClassType = BasicClassTypes.Beginner;
    12.         SetBaseClassHealth = 50f;
    13.         SetBaseClassHealthModifier = 1.2f;
    14.         SetBaseClassMana = 50f;
    15.         SetBaseClassManaModifier = 1.2f;
    16.      
    17.         return this;
    18.     }
    19.  
    20.     public BasicClass Warrior()
    21.     {
    22.      
    23.         return this;
    24.      
    25.     }
    26.  
    27.     public BasicClass Mage()
    28.     {
    29.      
    30.         return this;
    31.     }
    32.  
    33.     public BasicClass Thief()
    34.     {
    35.      
    36.         return this;
    37.     }
    38.  
    39.     public BasicClass Bowman()
    40.     {
    41.      
    42.         return this;
    43.     }
    calling it in player
    Code (CSharp):
    1.  
    2.  
    3. public Player()
    4.     {
    5.         BasicClass newclass = new BasicClass();
    6.         newclass.Beginner();
    7.  
    8.         PlayerHealth();
    9.         PlayerMana();
    10.         PlayerLevel();
    11.         PlayerName();
    12.     }
     
  31. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    In the future you should really post the tops of each of these script files with the class name. Its impossible to follow the logic when i don't know the names of the classes or the inheritance of them.

    I'm assuming BaseClass still inherits from PlayerStats and PlayerSTatLogic also inherits from PlayerStats.
    Your Player has an instance of a BaseClass and a PlayerStatLogic in it. Just because they both inherit from the same base class they are still different instances of each other. Whats happening is your setting all the variables in BaseClass's PlayerStats.. Then your looking at PlayerStatLogic's PlayerStats and wondering why they aren't set.

    Imagine this scenario. I have a Car Class (your playerStats class) I then make a FordTruck Class (BaseClass) and a ChevyCorvetteClass(your PlayerStatLogic). I then call a function that sets FordTruck paint to red. Then I call ChevyCorvette function to display its color and wonder why its not red. Thats effectively what your doing here:

    Code (CSharp):
    1. public Player()
    2.     {
    3.         BasicClass newclass = new BasicClass();
    4.         newclass.Beginner();
    5.         PlayerStatLogic statlogic = new PlayerStatLogic();
    6.  
    7.         statlogic.PlayerHealth();
    8.         statlogic.PlayerMana();
    9.         statlogic.PlayerLevel();
    10.         statlogic.PlayerName();
    11.  
    12.     }
    I'm not sure this is till the working code because you seem to be making major restruturing and just posting small snippets so its not 100% possible to follow the new logic of whats inheriting what or whats being called.

    Don't by shy about reposting all the relevant code. If you think it makes the post too long to look at try using a spoiler tag around the code like this:
    Code (CSharp):
    1. public class FooledYou
    2. {
    3.        public bool ItsReallyShort = true;
    4. }
     
    Last edited: Mar 1, 2017
    Suddoha likes this.
  32. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    It surely does if you get it running, but not as you'd expect.

    Listen. It's very difficult to grasp the concept if you only supply one script after another. And when you post missing code, you've changed the other parts already so their current state is missing.

    Now, if I take a look at all the scripts and imagine how it's setup, it's just that you create a new instance of a "BaseClass" in your Player's constructor.
    You also call these methods which you named "PlayerHealth" etc. Those access some "BaseStatXXX" properties, at least that's what the other code snippets show.

    So it's a huge flaw in your design, as you work with a subclass that instantiates one of its base class and... you see, that's already irritating.

    Make one post with every script that is related to your whole structure and we'll be able to exactly show you the mistakes and recommen improvements.
     
  33. simonlvschal

    simonlvschal

    Joined:
    Nov 17, 2015
    Posts:
    266
    okay thats obviously on my end. so this is what i changed so far Starting from top.

    World Class it will maintain and update all values,player positions etc. at some point. and sorta be the cream la cream

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class World : MonoBehaviour {
    7.  
    8.  
    9.  
    10.     void Awake()
    11.     {
    12.         Player newplayer = new Player();
    13.      
    14.     }
    15.  
    16.  
    17.     // Use this for initialization
    18.     void Start () {
    19.      
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update () {
    24.      
    25.     }
    26. }
    27.  
    next up is the Player itself. note i only inherit in order to make it eaiser to access the data.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Player : PlayerStatLogic
    4. {
    5.  
    6.  
    7.     public Player()
    8.     {
    9.         BasicClass newclass = new BasicClass();
    10.         newclass.Beginner();
    11.      
    12.         PlayerHealth();
    13.     }
    14.  
    15.  
    16.  
    17.  
    18. }
    PlayerStatLogic

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6.  
    7. public class PlayerStatLogic : PlayerStats {
    8.  
    9.     public Slider healthbar;
    10.     public Slider manabar;
    11.     public Text playerlevel;
    12.     public Text playername;
    13.  
    14.     private ClassStats classstats = new ClassStats();
    15.     public PlayerStatLogic()
    16.     {
    17.      
    18.     }
    19.     public void PlayerHealth()
    20.     {
    21.      
    22.         SetClassType = GetClassType;
    23.         SetPlayerMaxHealth = classstats.GetBaseClassHealth;
    24.         SetPlayerHealth = classstats.GetBaseClassHealth;
    25.         SetPlayerCurrentHealth = GetPlayerHealth;
    26.         Debug.Log("Class : " + GetClassType);
    27.         Debug.Log("Max Health : " + GetPlayerMaxHealth);
    28.         Debug.Log("Current Health : " + GetPlayerCurrentHealth);
    29.  
    30.      
    31.     }
    32.  
    33.     public void PlayerMana()
    34.     {
    35.         SetPlayerMaxMana = classstats.GetBaseClassMana;
    36.         SetPlayerMana = classstats.GetBaseClassMana;
    37.         SetPlayerCurrentMana = GetPlayerMana;
    38.  
    39.         Debug.Log("Max Mana : " + GetPlayerMaxMana);
    40.         Debug.Log("Current Mana : " + GetPlayerCurrentMana);
    41.  
    42.      
    43.     }
    44.  
    45.     public void PlayerLevel()
    46.     {
    47.         SetPlayerLevel = 1;
    48.         playerlevel.text = "Level : " + GetPlayerLevel.ToString();
    49.     }
    50.  
    51.     public void PlayerName()
    52.     {
    53.         SetPlayerName = "Reaper";
    54.         playername.text = GetPlayerName.ToString();
    55.     }
    56.  
    57.     public float CalculateHealth()
    58.     {
    59.         return GetPlayerCurrentHealth / GetPlayerMaxHealth;
    60.     }
    61.  
    62.     public float CalculateMana()
    63.     {
    64.         return GetPlayerCurrentMana / GetPlayerMaxMana;
    65.     }
    66.  
    67.     public void DealDamage(float damagevalue)
    68.     {
    69.         SetPlayerCurrentHealth = GetPlayerCurrentHealth - damagevalue;
    70.         SetPlayerCurrentMana = GetPlayerCurrentMana - damagevalue;
    71.         Debug.Log(healthbar.value = CalculateHealth());
    72.         Debug.Log(manabar.value = CalculateMana());
    73.         Debug.Log("Current Health : " + GetPlayerCurrentHealth);
    74.         Debug.Log("Current Mana : " + GetPlayerCurrentMana);
    75.     }
    PlayerStats

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerStats  {
    6.  
    7.     private string _PlayerName;
    8.     public string GetPlayerName
    9.     {
    10.         get { return _PlayerName; }
    11.     }
    12.     public string SetPlayerName
    13.     {
    14.         set { _PlayerName = value; }
    15.     }
    16.  
    17.     private float _Health;
    18.     public float GetPlayerHealth
    19.     {
    20.         get { return _Health; }
    21.     }
    22.     public float SetPlayerHealth
    23.     {
    24.         set { _Health = value; }
    25.     }
    26.  
    27.     private float _MaxHealth;
    28.     public float GetPlayerMaxHealth
    29.     {
    30.         get { return _MaxHealth; }
    31.     }
    32.     public float SetPlayerMaxHealth
    33.     {
    34.         set { _MaxHealth = value; }
    35.     }
    36.  
    37.     private float _CurrentHealth;
    38.     public float GetPlayerCurrentHealth
    39.     {
    40.         get { return _CurrentHealth; }
    41.      
    42.     }
    43.     public float SetPlayerCurrentHealth
    44.     {  
    45.         set { _CurrentHealth = value; }
    46.     }
    47.  
    48.     private float _Mana;
    49.     public float GetPlayerMana
    50.     {
    51.         get { return _Mana; }
    52.     }
    53.     public float SetPlayerMana
    54.     {
    55.         set { _Mana = value; }
    56.     }
    57.  
    58.     private float _MaxMana;
    59.     public float GetPlayerMaxMana
    60.     {
    61.         get { return _MaxMana; }
    62.     }
    63.     public float SetPlayerMaxMana
    64.     {
    65.         set { _MaxMana = value; }
    66.     }
    67.  
    68.     private float _CurrentMana;
    69.     public float GetPlayerCurrentMana
    70.     {
    71.         get { return _CurrentMana; }
    72.  
    73.     }
    74.     public float SetPlayerCurrentMana
    75.     {
    76.         set { _CurrentMana = value; }
    77.     }
    78.  
    79.     private float _Exp;
    80.     public float GetPlayerExp
    81.     {
    82.         get { return _Exp; }
    83.     }
    84.     public float SetPlayerExp
    85.     {
    86.         set { _Exp = value; }
    87.     }
    88.  
    89.     private float _Level;
    90.     public float GetPlayerLevel
    91.     {
    92.         get { return _Level; }
    93.     }
    94.     public float SetPlayerLevel
    95.     {
    96.         set { _Level = value; }
    97.     }
    98.  
    99.     private ClassStats.BasicClassTypes _Class;
    100.     public ClassStats.BasicClassTypes GetClassType
    101.     {
    102.         get { return _Class; }
    103.     }
    104.     public ClassStats.BasicClassTypes SetClassType
    105.     {
    106.         set { _Class = value; }
    107.     }
    108.  
    109.  
    110.  
    111. }
    112.  
    ClassStats.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ClassStats  {
    6.  
    7.     private float _BaseClassHealth;
    8.     public float GetBaseClassHealth
    9.     {
    10.         get { return _BaseClassHealth; }
    11.     }
    12.     public float SetBaseClassHealth
    13.     {
    14.         set { _BaseClassHealth = value; }
    15.     }
    16.  
    17.     private float _BaseClassMana;
    18.     public float GetBaseClassMana
    19.     {
    20.         get { return _BaseClassMana; }
    21.     }
    22.     public float SetBaseClassMana
    23.     {
    24.         set { _BaseClassMana = value; }
    25.     }
    26.  
    27.     private float _BaseClassHealthModifier;
    28.     public float GetBaseClassHealthModifier
    29.     {
    30.         get { return _BaseClassHealthModifier; }
    31.     }
    32.     public float SetBaseClassHealthModifier
    33.     {
    34.         set { _BaseClassHealthModifier = value; }
    35.     }
    36.  
    37.     private float _BaseClassManaModifier;
    38.     public float GetBaseClassManaModifier
    39.     {
    40.         get { return _BaseClassManaModifier; }
    41.     }
    42.     public float SetBaseClassManaModifier
    43.     {
    44.         set { _BaseClassManaModifier = value; }
    45.     }
    46.  
    47.     public enum BasicClassTypes
    48.     {
    49.         Beginner,
    50.         Warrior,
    51.         Mage,
    52.         Thief,
    53.         Bowman
    54.     }
    55.  
    56.  
    57. }
    58.  
    BasicClass again note it only inherits because i want to make it eaiser to access the classStats

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BasicClass : ClassStats {
    6.  
    7.     private PlayerStatLogic playerstat = new PlayerStatLogic();
    8.  
    9.     public BasicClass()
    10.     {
    11.      
    12.     }
    13.  
    14.     public BasicClass Beginner()
    15.     {
    16.         playerstat.SetClassType = BasicClassTypes.Beginner;
    17.         SetBaseClassHealth = 50f;
    18.         SetBaseClassHealthModifier = 1.2f;
    19.         SetBaseClassMana = 50f;
    20.         SetBaseClassManaModifier = 1.2f;
    21.  
    22.         return this;
    23.     }
    24.  
    25.     public BasicClass Warrior()
    26.     {
    27.         playerstat.SetClassType = BasicClassTypes.Warrior;
    28.         SetBaseClassHealth = 50f;
    29.         SetBaseClassHealthModifier = 1.2f;
    30.         SetBaseClassMana = 50f;
    31.         SetBaseClassManaModifier = 1.2f;
    32.         return this;
    33.      
    34.     }
    35.  
    36.     public BasicClass Mage()
    37.     {
    38.      
    39.         return this;
    40.     }
    41.  
    42.     public BasicClass Thief()
    43.     {
    44.      
    45.         return this;
    46.     }
    47.  
    48.     public BasicClass Bowman()
    49.     {
    50.      
    51.         return this;
    52.     }
    53.  
    54.  
    55. }
    56.  
    and the two last classes is AdvancedClass and MasterClass atm both empty and only inherits from BasicClass.

    honestly sometimes i do wierd mistakes and i think i did one this time. either thinking it should work but it doesn't.

    all help is obviously something i take and use for the future to not make the same mistakes.
     
  34. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Okay, not sure where to start. There are quite some flaws in these scripts.

    Your code executes as follows:

    The world creates a player, the player's constructor will first call the other constructors in the hierarchy, i.e. PlayerStatLogic(), which itself does nothing* and PlayerStats() which is the implicit default constructor that'll be generated as there's no explicit constructor defined.

    * Note that the field initialization also takes place when an object is constructed. I'm referring to this:
    (Instance #1)
    Code (CSharp):
    1. private ClassStats classstats = new ClassStats();
    So let's roll back to the begin of the call chain and imagine we're back in the Player's constructor, which then creates a totally useless instance of 'BasicClass' and initilizes this as 'Beginner'.
    Now this particular yet useless instance is initialized as you'd expect.
    I'll refer to it as (Instance #2).

    Next step: you call 'PlayerHealth', which internally uses Instance #1, not Instance #2. They're completely different instances.
    Also, the 'BasicClass' creates another instance of 'PlayerStatLogic', which again creates yet another instance of 'ClassStats'.
    You're (luckily "only") on the edge of endless recursion thanks to one level of inheritance, if that's not prevented in any way.
    Not to mention that it's still a huge design flaw.

    I'm not even sure how to find the proper wording for this so that you can easily understand everything. If I find more spare time today, I'll post an alternative (but rough) solution. That's no promise though.
     
    p1zzaman likes this.
  35. simonlvschal

    simonlvschal

    Joined:
    Nov 17, 2015
    Posts:
    266
    mhm ye i created a big mess. lol . gotta rethink it again. i just need to find a way to create a player that uses a basicclass. etc
     
  36. p1zzaman

    p1zzaman

    Joined:
    Jan 1, 2017
    Posts:
    64
    Nice! But got flaws. You made it have a hard dependency to BasicClass. Also you have methods that say getFighter getWhatever, that needs to break out too.

    Now looking at your Player constructor you are creating BasicClass. That is a great first step towards composition.

    but I am guessing that not all instances of Player in the future will have BasicClass? What if you want a play with properties of AdvancedClass?

    Let's go one step further.

    Your player constructor can take in parameters.

    Code (csharp):
    1. //constrcutor code
    2. public Player(BasicClass playerClassType)
    However this will only work if you pass in a class of BasicClass, how can we generalize this.

    Let's talk a little about Interfaces.

    An Interface is an api contract, it is like a normal class except it does not include implementation details. It. asically says, I want whatever that implements this interace to do these things. You can create one interface and have many classes that implement it.

    Code (csharp):
    1. public interface IPlayerClassType{
    2.  float playerHealth();
    3.  float playerMana();
    4. }
    Now you can have you BasicClass implement that interface like

    Code (csharp):
    1. public class BasicClass : IPlayerClassType {
    2.   public float playerHealth(){
    3.       return whatever;
    4. }
    5. }
    Now going back to the Player constructor, instead of

    Code (csharp):
    1. public Player(BasicClass type)
    you can go

    Code (csharp):
    1. public Player(IPlayerClassType type)
    in general it is not a good practice to do new class inside a constructor, makes dependency management difficult. By moving the class into the constructor parameter it is eaiser to see what this class depends on.

    In your world script you will need to determine which implementation of the IPlayerClassType you wish to pass in.

    example: If you let the player choose what class they want, and if you have basic and fighter implementing IPlayerClassType, then you create player like

    Code (csharp):
    1.  
    2. if(selection == "basic"){
    3.  IPlayerClassType type = new BasicClass();
    4. player = new Player(type)
    5. }else if (selection == "fighter"){
    6.   IPlayerClassType type = new FighterClass();
    7.  player = Player(type);
    8. }
     
    Last edited: Mar 1, 2017
  37. p1zzaman

    p1zzaman

    Joined:
    Jan 1, 2017
    Posts:
    64
    I think @Suddoha is on the right path in pointing out the flaws in logic. I was focusing on too much on the design patterns.
     
  38. simonlvschal

    simonlvschal

    Joined:
    Nov 17, 2015
    Posts:
    266

    i diff like the way your thinking. but ye i gotta rethink my plan. i don't know why i wanted inherit on so many things.

    Anyhow i will recreate it with no inherit at all that way i can fiqure out things the proper way i believe?

    also if i can't use a new keyword on lets say basicclass and player in what way would be the best to get BasicClass ?

    and well my plan is to have 4-5 Basic Classes you know warrior,mages type of deal. and they can advance to a a stronger one of them which then again they can further up that by going masterclass.
     
  39. p1zzaman

    p1zzaman

    Joined:
    Jan 1, 2017
    Posts:
    64
    You can use the new keyword to instantiate those classes. I'm just saying it's preferred not to do it in a constructor. A constructor is a special method for every class that gets called when you call the new keyword. Inside the constructor, it is advised not to call another new keyword

    Ya go with a common interface for all the classes that have the same API, and implement those with different class files.
     
  40. simonlvschal

    simonlvschal

    Joined:
    Nov 17, 2015
    Posts:
    266
    so a new keyword in case of unity or general? does it copy the existence of the class like values etc? or does it actually sorta delete it all and create a new new?