Al Heck

Sign in

Often, these are declared with an “I” at the front and a “able” at the end. It’s written like a class minus the parameters.

You cannot have fields. public int health will error out. But you can set it with a property.

Allows us to force inheritance for easy coding! For this example, we want to create an employee salary system. We want to force every employee to calculate monthly salary so we set the function to abstract and since we did that, we have to force our class to be abstract as well.

Next, we will create a new class that is part-time that will inherit employee. Since we grabbed a class that also uses monobehavior, we inherited the Employee class and the Monobehavior class. Because it’s pulling from an abstract class, we need to do an override function for calculateMonthlySalary(). We will also add the variables for hours worked and hour rate.

Let’s loop through a item dictionary. This is useful for a loop vs a simple list or an array because sometimes, the count can be huge. 10 is not large. To cycle through 60,000 or 6,000,000 can be time consuming. Let’s see if we can use dictionaries to find the item based on the key id. To start, let’s create a dictionary item DB.

All of these items have been added via script into our database. Now we can sort through this list using library keys.

Let’s do a foreach loop.

A dictionary allows me to associate a key with an item in a list. They are written similarly to a list but add an in to the parameter.

We can then add items similarly, just passing an int in the dictionary.

Sometimes, you can’t pass a string. Enums still come in similar to arrays, so how do we get the int value of an enum?

To cast it, we need to add a (int) in front of the variable amount.

This allows us to grab the numerical value of the enum over the string value. This is great for a level selection system.

Let’s say we wanted to create an item class and we wanted to browse through our items using an enum dropdown list. Here’s how we setup the class.

Now we need a database to connect to.

Let’s make a FSM — finite state machine using an Enum (usually with switch statements). Let’s start by creating our enemy state enum.

Let’s begin and have our enemy start the game in a patrolling state.

Create readable selections based off of integer values! For a difficulty selection screen, instead of doing them as int values (0 for easy, 1 for medium, etc), we can use an enum as a variable. Be sure the last value has no comma.

Let’s create a typical inventory system using lists using C#. To start, let’s create a simple item class.

Let’s create a gameobject and attach an item database script to it.

Let’s begin by creating a list using the string variable type since names are strings.

Al Heck

Unity Developer and Software Engineer who loves making games with GameDevHQ

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store