22 - Events
In this tutorial we will learn how events build upon the functionality of delegates that we learned about in the last tutorial. Events are actions such as user-clicks, keyboard input, and internal message systems. Events follow a publisher-subscriber model. A publisher-subscriber model is a framework which allows entities called subscribers the ability to subscribe to events created by an entity called the publisher. The goal of events is to perform a set of actions when some event happens. Multiple actions may happen when an event happens, and can dynamically change as the game progresses. In this tutorial we will set-up a list of events to happen when our player falls of the map. For this tutorial we will set our scene up like shown below. You can re-use the simple player movement found in previous tutorials to move the character. Also, create a script called EventManager which will be attached to the DeathArea GameObject and a EventTutorial script which will be attached to our player.
Creating Event Manager Class
The first step to using events is defining a delegate. This delegate is going to shape how all of the methods that are being called by our event are shaped. If you want different types of methods to be called by an event, you can create multiple delegates within the EventManager class. For this tutorial we would like to reset our player's position and subtract a life when our player falls off the map. We can do both of these with void functions if we attach the method script to the player. This means that we will only require one delegate for the event. I labeled mine DeathDel.
The next step is to create the event. This will be done in the event manager class and we will make it static so that it can be called anywhere. We'll actually create two events for this tutorial. One will be used to subtract the player's lives and reset their position. The other will be used to display game over text once the player has run out of lives.
The final step for creating the event is creating the code that will be the calling factor on the event. In this tutorial, our event is going to occur when the player falls off the map, and when the player has no more lives respectively. We accomplish this by utilizing an invisible gameobject with a box collider attached to it. This box collider will be used to detect a trigger from the player colliding with it. Upon colliding, we would like to reset the player's position and subtract a life. We would also like to call the GameOverEvent when the players current lives are at zero. We are able to accomplish this with the same trigger being when the player collides with the death area.
Using the Event
Now that we have our event created, we need to actually use the event. The first step to utilizing events is to have methods that the event will call when it occurs. We will create two seperate methods for this tutorial. A void method which changes the position of the player back to 0,0,0. We will also create another void method which subtracts a life from the player. We will also create a method to display the gameover text. You'll see we unsubscribe to the resetposition once we have 1 life left and then we unsubscribe from the subtract life event when we have 0 lives. This will be more clear in the next portion.
The next step to utilizing events in Unity is to subscribe to the event. Generally, this is accomplished through the enable and disable methods within the class that the events methods are in. We use the "+=" operator to subscribe to events and teh "-=" operator to unsubscribe to events. We put them in the OnEnable and OnDisable methods. Notice the format of how we subscribe to events. We are subscribing to the specific event that we defined in the Event Manager class. This links the ResetPosition, SubtractLives, and OnGameover methods to the event of falling off the map.
Make sure that you have all of the fields assigned in the inspector and then try out the script by jumping off the cliff three times. You should get a result similar to what is below. Notice that by unsubscribing to the ResetPosition event we don't reset our position after the last life is gone.
In this tutorial we learned how to create a simple event system where a player loses a life and reset's their position when they fall off the map. This simple example should explain the process of using events in Unity and you should be able to extend this concept to your own code. What is the benefit of using events? We can dynamically change how this event behaviour acts at runtime. If we create a level where the player might fall of the map, but we would not want them to lose a life, we can unsubscribe the SubtractLives method and only call the ResetPosition method when the player falls off the map. It is this real time behaviour that makes events so powerful. It allows us to customize our game flow as we go.