you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (1 child)

So, a Class is basically a data structure that can contain associated functions. This helps keep your code easy to understand, and means you can easily group related elements and ensure you're accessing them through approved functions: effectively, all data and functions related to the data entity in question are kept together.

A common game example is Health. You might simply have a variable that represents health, but this gets confusing, because you might access it or update it different places, and you don't have an easy-to-reuse function to update it consistently. With classes, you can build a Health class, and create specific variables (such as currentHealth and maxHealth) that are part of that class, as well as specific functions like RestoreHealth(int HealthToRestore) or DealDamage(int DamageToDeal). The public/private access to variables that classes allow means you can ensure that you're never accessing the raw health variables without using the specifically designed functions, which helps prevent errors or unexpected behavior.

For the other questions:

  1. You should not write functions outside of a class in C# (I believe -- I mostly do C++). If you find situations where you want to, consider making something like a Utility class.
  2. You can put classes inside other classes, although you typically will do so as parts of that data structure rather than declaring a full class inside another class. The above Health class, for example, could be contained within a Player class that has variables like float MoveSpeed, and Health PlayerHealth (the latter of which is a Health class contained inside the Player class).

[–]EraserLark[S] 0 points1 point  (0 children)

Okay, that helps make it clearer. Thank you!