Al Heck

Actions are the same as delegates and events combined — makes the actual writing far simpler.

Let’s pretend I wanted to create a damage system but I wanted my UI manager to subscribe to the event changes on my health system. If I want an action to occur, I need to access the System library and implement the action this way depending on if it passes in a parameter or now.

Now let’s create a typical damage system.

Let’s move the cube to a different position using events. We will use a space bar to move our position to 5, 2, 0. First, we need to properly declare the event.

Then we need to create our space bar functionality. In it, we will pass in our position and then apply that position to our onTeleport event. Keep in mind, ever object that is listening to this event will always move to the same place.

It’s a specialized delegate that all gameobjects listen to. That way, instead of assigning via a for loop, I can call an event and any gameobject that is set up to listen to that particular event will react appropriately. Let’s create a button that turns all cubes red.

We can create a system that scales. We don’t have to hard code everything into our scene. We can assign it a i***able, and it will do whatever that method is regardless of what it is.

Let’s create an iCollectable. The whole purpose is anything using this item uses the collect function (a contract).

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.

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