Events

“Something has happened and I would like you (script) to respond now”

What Are Events For?

Events are useful for alerting other classes / game objects that something has happened. With scripting you can configure some classes to raise events while others respond to events being raised. Here are some example applications:

  • Displaying Player Health: Your game could have a UI element that displays a player’s health bar. Instead of updating the health bar every frame, the UI element is configured to update whenever a OnPlayerHealthChanged event is raised.

  • Pausing The Game: When the player pauses the game, your game may require that all game objects must stop moving, player input must be disabled, and many more systems must change. Instead of a single game manager calling functions in scripts across the scene to pause the game, it could raise a OnGamePaused event and the other scripts listen for that event and respond accordingly.

Event Architecture: Action Events

../_images/action_event_system_diagram.svg

Example

Click to download ActionEventSystemExample.unitypackage.

../_images/action_event_system_example_diagram.svg

Event Architecture: Unity Events

../_images/unity_event_system_diagram.svg

Example

Click to download UnityEventSystemExample.unitypackage.

../_images/unity_event_system_example_diagram.svg

Event Architecture: Scriptable Objects

Note

This section includes the use of Unity Events. See Event Architecture: Unity Events for more information.

../_images/scriptable_object_event_system_diagram.svg

Example

Click to download ScriptableObjectEventSystemExample.unitypackage.

../_images/scriptable_object_event_system_example_diagram.svg

Event Architecture: Scriptable Objects V2

We can refactor the above implementation for scriptable objects events so that:

  • Listeners are able to recieve any type of information

  • The game event listener component we add to game objects can listen in on multiple events

Because the data passed is an object, it is the responsibility of the scripts receiving the data to cast the information to the correct type.

You can find the updated code and example below:

Example

Click to download ScriptableObjectEventSystemV2Example.unitypackage.

Event Architecture: Scriptable Objects V3

This is mainly a refactoring of the V2 implementation for scriptable object events (Event Architecture: Scriptable Objects V2). In summary this was done to make the link between events and the functions they called more explicit in the editor and code. See below for an in depth explanation:

  • In V2 game event listeners existed in their own separate monobehaviors. This made following links between game events and functions listening to those game events difficult. For example: On a player prefab you could have a PlayerController component and a Listener component. You can have the listener component contain a OnPlayerHurt game event and have it call PlayerController’s TakeDamage() function when that hurt event was raised. This setup works just fine. However if you wanted to create a new prefab for a different kind of player object, you may start by adding a PlayerController component to it and then forget that you should also add a listener for a hurt event that calls the component’s TakeDamage() function since there is no specific reference to a listener in the PlayerController component.

  • In V3 we have done away with the Game Event Listener component to discourage users from making links between events and functions that can be easy to miss. Scripts must now take game events in as arguments and register them directly with the methods they want to have called. The only downside of this approach is that scripts that to subscribe to these game events must be mindful of how they register and unregister function to events. Failure to register results in no functions called, and failure to unregister so that they aren’t accidentally registered and called when they do not expect them to be. (Previously the listener component automatically registerd and unregistered on the OnEnable OnDisable event functions.)

  • Game events no longer send data in a general object form. In V2 this was used in order to have the listener component accept any time of game event with any single argument input. Now that we have done away with the listener component, We thought it was better to keep the type of data passed into the raise method explicit to avoid confusion

  • Since we want to directly establish a link between the script functions and game events, we have decided to use UnityEvents and UnityActions since they allow us to pass functions as arguments and can directly register with events using the UnityEvent’s AddListener() method.

You can find the updated code and example below:

Example

Click to download ScriptableObjectEventSystemV3Example.unitypackage.