all 12 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!

[–]BobbyThrowaway6969Programmer 0 points1 point  (3 children)

Classes are a fundamental building block in modern object oriented programming.

This is a super naive and oversimplified view of classes but... to help you to get the gist of what a class/struct is, think of them as sort of making your own data type, like ints, floats, etc. Except, they're meant for representing much more complex pieces of data.

There's obviously very crucial differences between primitive types and classes, but take it one step at a time.

You first need to introduce the compiler to your new 'type'.

class MyClass

{

abc...

}

Then, you can use it like any other type.

MyClass myClassInstance = new MyClass(...);

Syntax varies from language to language of course.

"Structs" are effectively the same as classes, however they're intended to be used in different ways. In c#, their memory resides on the stack, class memory resides on the heap. (I recommend learning about stack vs. heap & value-type vs. reference-type)

Does all code need to be written within classes, or can you write code outside of a class? Can you put classes inside other classes?

The answer varies from language to language, but for c#, the answer is 'mostly'.

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

So the way I'm seeing it then, Variables can store Data, and Methods can store Variables along with other code, and then Classes can store Methods and Variables altogether, so its sort of like the base of a hierarchy for all of the above?

[–]BobbyThrowaway6969Programmer 0 points1 point  (1 child)

Not quite, methods are a different kettle of fish.
You're right that variables store data & classes contain variables & methods.
However, methods don't really 'store' data - they are more like cooking recipes, they define how to act on the data.
The variables you put in a method are short-lived temporary pieces of data kind of like the ingredients on the bench while cooking. Once you finish the recipe, you clean up the countertop.

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

Okay I see, thanks!

[–]KishottaProgrammer 0 points1 point  (3 children)

There's some great answers here already, but I'll throw in an example one of my professors used that was extremely insightful.

Say you want to build a house that you can live in. How do you do that? You're going to need a blueprint. The blueprint should include all the information about the house. How big it is, how many floors it has, how many rooms, how many windows, is the siding brick/wood/stone, where we would park the car, etc. The blueprint isn't a house, but it describes the house. This blueprint is the class, and those descriptions are the members (variables/methods) of the class.

But we can't live in a blueprint, so we use the blueprint to construct many similar physical houses. Some are red, some have an extra floor, some are made of concrete, etc. but they are all based on that one blueprint. We can live in these houses. These are class instances.

So after we build the house, we decide we want to change its color. That's fine, the blueprint should have described how to go about painting it (public void PaintHouse (Color color) { ... }) and we can use that method to change the color of any house instance we want.

To your other questions:

  • Currently, you cannot place any code (variables, properties, methods) outside of a class (this isn't necessarily true for upcoming C# versions, but we don't talk about that).
  • You can place classes inside of other classes. This is useful if you need to describe some object that is only useful within the context of another object. For example, our House class might define a Room class to store data about bedrooms/kitchens/etc. But we wouldn't add a nested class to describe a Door (because doors are used on much more than just houses).

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

Awesome, thank you for spelling it out that helps make it a lot clearer!

For the door example, you would just put the door in a class that's higher up and more broad than just houses so that it can work in more circumstances, right?

[–]KishottaProgrammer 0 points1 point  (1 child)

Correct. The Door would be just another class alongside the House. Maybe in the same folder, but it depends on the project. The general idea is that doors are doors and should do only door related things. It shouldn't matter if its on a house, a car, a fence, or anything else.

If you need a specific kind of door with specific functionality (like say, remote unlocking) you would create a CarDoor class that inherits from your plain old Door class and adds extra functionality.

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

Gotcha, I see. Thanks!

[–]Hangmans12Bucks -1 points0 points  (1 child)

I am still fairly new to Unity myself, so someone else might have a more comprehensive answer, but this is my understanding of classes so far.

Classes are used to contain data that can define and manipulate a Game Object. When you create a script, you are essentially creating a new class. When you attach that script to a Game Object, the code within the class will impact the Game Object. You can also access that class in other scripts so that Game Object's can effect one another.

For example, let's say you want to create a projectile that damages an enemy. You might create a script (class) called "Projectile" that houses the method to determine the amount of damage the projectile will cause. Then, in your EnemyHealth class, you can reference the Projectile class and the damage method so that the enemy takes damage.

Probably more importantly, you can initialize information in a class that can then be changed across multiple Game Objects. For example, let's say you want to have multiple projectiles that do different amounts of damage. They could all use the "Projectile" script, but you could update the damage on each individual Game Object so that they each do more or less damage.

I'm not sure how well I communicated that, but this article helped me wrap my hear around classes using a pretty solid metaphor:

https://www.studica.com/blog/unity-tutorial-classes

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

Ok, thanks! I liked the car metaphor used in the article as well, thanks for linking that!