Game Dev Insights: Variables — The Building Blocks of Software Programming
Objective: This blog post explores variables in game development with Unity and C#. Covering accessibility modifiers, data types, naming conventions, and assigning values, it equips readers with the fundamental knowledge to leverage variables effectively for creating dynamic and interactive games.
What is a variable?
Variables are an essential concept in software programming. They allow developers to store and manipulate data. In game development, for example, a variable can represent the number of lives a player has (an integer) or the height a character can jump (a decimal number).
And that brings us to an important concept in programming: Data Types.
Data Types
Variables in C# have specific data types that determine the kind of data they can store. There are 4 main data types in C#:
- Int: stores whole numbers (e.g. player score, level)
- Float: stores decimal numbers (e.g. movement speed, player health)
- String: stores text data (e.g. player name, dialogue)
- Bool: stores true or false values (e.g. game over state, player input)
public class Player : MonoBehaviour
{
public int score; // Whole number
public float health; // Decimal number
public string playerName; // Text data
public bool isAlive; // Boolean value
// Rest of the code...
}
However, Unity provides additional built-in data types that are suitable for game development such as GameObject, Sprite, etc.
Accessibility Modifiers: Public or Private
In C#, when declaring a variable you would need to determine if your variable will be public or private. In most situations, it’s best practice to keep variables private to prevent them from being exposed to other scripts and systems.
public class Player : MonoBehaviour
{
private int health = 100; // Private variable to store player health
// Rest of the code...
}
For example, it might be preferable to keep the health variable private rather than making it public. You may be asking why it shouldn’t be public, well the answer is other scripts or objects could directly modify it which potentially leads to undesired behavior or improper handling of the player’s health. That’s it better to keep this variable private and you can provide a method within the class to allow controlled access to it.
Naming Conventions
Choosing appropriate variable names is crucial for code readability and maintainability. There are a few tips on naming convention good practice:
- Use CamelCase which will make variables more readable.
public class Player : MonoBehaviour
{
public int playerScore; // using CamelCase
public float playerhealth; // not using CamelCase
// Rest of the code...
}
2. Use meaningful and descriptive names so it’s easier to understand the code when you look back at it maybe one to two months from now.
public class Player : MonoBehaviour
{
public int playerScore; // descriptive name
public float x; // not description name
// Rest of the code...
}
3. Prefix private variables with an underscore (_) so you will know that they are private when reading through your code.
public class Player : MonoBehaviour
{
private int _playerHealth; // private variable
public float playerName; // public variable
// Rest of the code...
}
Assigning Values (optional)
When declaring a variable, there are four elements: accessibility modifier, data type, name, and value. The assignment of a value is optional, meaning that variables can be assigned values during declaration or at a later stage in the code. Here’s an example:
public class Player : MonoBehaviour
{
public int playerScore = 0; // Assigning a value during declaration
public float playerHealth; // Declaration without initial assignment
private void Start()
{
playerHealth = 100.0f; // Assigning a value later in the code
}
// Rest of the code...
}
Conclusions
In game development with Unity and C#, variables play a crucial role in storing and manipulating data. By adhering to best practices, you ensure that your code is easier for fellow developers to understand and work with, leading to more efficient and productive teamwork.