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.

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).

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.

Al Heck

Al Heck

Unity Developer and Software Engineer who loves making games with GameDevHQ