Programming Documentation

logo

Programming Documentation

Search
Light Mode
Contact Us

Contact us

No results for your search.
Sorry, an unexpected error occurred


General programming practice

// Do this
private const _thisVariablesDoesThis = 5; //Unmodifiable

private void Function()
{
    // Here we know what both variables are.
    thisVariableDoesSomething += thisVariablesDoesThis; 
}
// Don't do this
private void Function()
{
    thisVariableDoesSomething += 5; // Why are we increasing it by 5?
}







// Do this
if()
{
    //Do stuff
}

while()
{
    //Do stuff
}

//Don't do this
if()
    //Do stuff
    
While() //Do stuff

//Also Don't do this
if(){
    //Do Stuff
} 







Naming Conventions

The importance of a naming convention makes the code easier to read.


Variables

// Do this
private int _healthPoints;
private bool _hasItem;

//Don't do this:
private int PlayerChar;
private bool A;
private string PSText;







Functions

private void OnDoorOpened()
{
    //Do stuff
}






private bool IsPlayerDead()
{
    return _isPlayerDead;
}







Privacy/Access Modifiers

Access modifiers are the public, protected, and private keywords used before a class and any members of that class like variables, methods, enums, etc. Leaving off the access modifier it is automatically considered private.

That way we have better control of when and how scripts alter them. There is no harm in accessing a variable from another class. Be more concerned about changing the value from another class.

HERE is a webpage explaining the importance of privacy and access modifiers in further detail

// Do this
private int _variable; // safest approach

[Serialized Field, Tooltip("Can be altered in the inspector")] 
private int _variable;

//working with properties (getters and setters)
private int _variable;
public int Variable 
{
    get {return _variable;} // can be read from anywhere
    private set {_variable=value;} // set from this class only
}

[field:SerializeField, Tooltip("same as above, but Unity creates a backupfield so you can set it in the inspector")] 
public float MyFloat3 { get; private set; }







SerializeField and Tooltip

πŸ“˜
Checkout the attributes section to learn more about them


XML Tags


///<summary>
/// This is where you explain what this class is doing. Make sure they are using '///' three backslashes instead of '//' two like a normal comment.
///</summary>
public class AClass:Monobehaviour
{
  ///<summary>
  ///This is where you explain what this method does. If it has parameters follow use the param tag like below, if it has a return type use the return tag to explain what the return is for.
  ///</summary>
  ///<param name=a>This is where you explain what this parameter is for</param>
  ///<param name=b>You can do this for as many parameters there are for the function</param>
  ///<returns>Explain what is going to be returned in this case its an integer</return>
  private int AMethod(int a, int b)
  {
    // Doing cool method things
  }
}







Comments

If it is necessary to write a piece of code that is not ordinary and it was the only way.

If the task you are working on still has something left to do that goes farther than your task card intended.


Ordering of Classes

The functions within classes should be arranged in a specific order. When we write classes , there is a specific order to keep in mind. Keeping a consistent order promotes readibility.

public class ClassName : MonoBehaviour
{
    //Variable Declarations
    private int _intVariable;
    private float _floatVariable;

    public string stringObject;
    public bool isABool;

    //Enum Declarations
    
    //Unity Events
    - OnValidate()
    - OnEnable()
    - Awake()
    - Start()
    - Update()
    
    //Private Functions
    
    //Public Functions
    //- Custom functions
    //- Get/Set Methods
    
}







Lambda Expressions and Delegates

Lambda expressions in C# are used like anonymous functions, with the difference that in Lambda expressions you don’t need to specify the type of the value that you input thus making it more flexible to use.

When you see β€œ=>” in C#, you see a lambda expression. It is a way to shorthand a function call to return a value to a variable in the middle of your code.

For example:

public float CurrentHealth => (energy * baseHealth)-damage;






Acts like a function with body:

{
  return (energy * baseHealth)-damage;
}






But you can call it without brackets though, like:

if(CurrentHealth > 0)










On This Page