-1
\$\begingroup\$

I'm currently trying to write a base player class, and I'm not exactly sure what I should make private, and what I should make public. I know that as a programmer, I need to keep as many variables as private as possible, but I don't know which setters I need to keep private, or just leave public. Is it bad to leave those setters public?

Here's the code for reference:

using UnityEngine;
using System.Collections;

/// <summary>
/// Base Class for the Player
/// </summary>
public class Player {

public PlayerSpecies species;

private float currentHealth;
private float currentXP;
private int currentMana;

private int playerLevel;

// variables that depend on player level
private float requiredXP;
private float maxHealth;
private int maxMana;
private float strengthFactor;
private float speedFactor;

// currency variables
private int chipCount;
private int solusCount;
private int orbCount;
private int xpCoinCount; 

public Player()
{
    maxMana = 20;
}

/// <summary>
/// The player's species.
/// </summary>
public PlayerSpecies Species
{
    get { return species; }
    set { species = value; }
}

/// <summary>
/// The player's name.
/// </summary>
public string PlayerName { get; private set; }

/// <summary>
/// The player's current health.
/// </summary>
public float CurrentHealth
{
    get { return currentHealth; }
    set { currentHealth = value; }
}

/// <summary>
/// The player's current experience count.
/// </summary>
public float CurrentXP
{
    get { return currentXP; }
    set { currentXP = value; }
}

/// <summary>
/// The player's current mana count.
/// </summary>
public int CurrentMana
{
    get { return currentMana; }
    set { currentMana = value; }
}

/// <summary>
/// The player's current level.
/// </summary>
public int PlayerLevel
{
    get { return playerLevel; }
    set { playerLevel = value; }
}

/// <summary>
/// Required XP for next level. The value is dependent on player's level.
/// </summary>
public virtual float RequiredXP
{
    get { return requiredXP; }
    set { requiredXP = value; }
}

/// <summary>
/// The maximum health the player can have. The value is dependent on player's level.
/// </summary>
public virtual float MaxHealth{
    get { return maxHealth; }
    set { maxHealth = value; }
}

/// <summary>
/// The maximum mana the player can have. The value is dependent on player's level.
/// </summary>
public int MaxMana
{
    // Calculates the maximum mana the player can have.
    get
    {
        maxMana = playerLevel * 20;
        return maxMana;
    }
    private set { maxMana = value; }
}

/// <summary>
/// The strength factor added to the player. The value is dependent on the player's level.
/// </summary>
public virtual float StrengthFactor
{
    get { return strengthFactor; }
    set { strengthFactor = value; }
}

/// <summary>
/// The speed factor added to the player. The value is dependent on the player's level.
/// </summary>
public virtual float SpeedFactor
{
    get { return speedFactor; } 
    set { speedFactor = value; }
}
}

Thanks in advance!

\$\endgroup\$

closed as primarily opinion-based by Alexandre Vaillancourt, Philipp, MichaelHouse Jul 17 '16 at 20:41

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

-1
\$\begingroup\$

By definition, all variables SHOULD be private. Every function or variable is private by default in Unity, but there's no problem writing the 'private' reserved word. If you want to get or set one of them from another class, you have to create a getter/setter function, but these functions must be public, otherwise they will be useless.

There is no problem having public setters.

I hope I helped you!

\$\endgroup\$
  • 5
    \$\begingroup\$ "All variables should be private" is an overgeneralization. \$\endgroup\$ – Almo Jul 17 '16 at 20:50
  • 1
    \$\begingroup\$ "All variables should be private" branches from a hardcore fixation on encapsulation, you should only practice "best practices" if it fits within how you develop a game. Extreme OOP has its consequences just the same as any other paradigm. \$\endgroup\$ – user5665 Jul 17 '16 at 23:18
  • \$\begingroup\$ By definition, all variables should be a variable. Otherwise you've gone far beyond the definition of "definition". \$\endgroup\$ – Jesse Williams Jul 18 '16 at 15:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.