-
Notifications
You must be signed in to change notification settings - Fork 1
IMPORTANT!! Game Events with scriptable objects
Event systems are something that is a pain to deal with in any unity project. With ScriptableObjects, we can mitigate some of those pain points.
The main thing ScriptableObjects provide us with is an object that does not need to live in the scene but can still be referenced by scripts as if it were a game object.
In the "ScriptableObjects" folder you will find a folder called "Events" and the base class "GameEvent" lives here.

To understand how we will be using events in this project you need to understand how ScriptableObjects work. You can think about ScriptableObjects as an OOP class that you can create instances of that live permanently in the assets. To create an instance of a ScriptableObject you DO NOT need to write any extra code, all of it is done through the "create" menu option when you right-click in the asset folder. This will create an asset that essentially behaves like an instance of a class, any of your scripts can reference this object and it is serialized in the inspector view if you make it public.
The most common way to do events is with a Publisher/Subscriber pattern. You should already be familiar with this through your OOP courses. But essentially, if an object called a "subscriber" wants to know of some event that happens it can add itself to a list of "listeners" and when the event happens, the publisher can just loop through the list of listeners and call a method on those listeners.
This project uses a custom solution by using ScriptableObjects as our event system.
Go into the asset menu, right-click, and highlight create, at the very top there should be a ScriptableObjects menu option where you can create a GameEvent, click this. If you are familiar with OOP this is the same thing as creating an "instance" of a class by calling its constructor, this instance will have all of its variables and methods of the class GameEvents that can be invoked like any other object.

This should create an instance of the class GameEvent in the current directory and you should name it something appropriate, in this case, I'll call it ExampleGameEvent. When you create a GameEvent it should be SINGULAR in use, for example, if I want an event for when the power turns on and then back off I should create 2 GameEvents from the menu, one called "PowerOnEvent" and "PowerOffEvent".

In your Monobehaviour that wants to listen to the event, add a new field of type GameEvent and make it public, for the event that you want to trigger, make sure you call .AddListener() and .RemoveListener() in OnEnable and OnDisable respectively.

Now in the inspector, you can drag your GameEvent into the slot, or you can look it up in your asset folders by clicking the radio button on the left of the inspector field.

That's it! You are now listening to a GameEvent, of course nothing will happen yet because nothing is triggering the event. However, for debugging purposes, our GameEvents use a custom editor that allows the GameEvent to be triggered manually with the press of a button.

Similarly to how you Listen to a GameEvent, you can also Invoke a GameEvent anywhere in code. In your Monobehaviour script all you need to do is add a reference to a GameEvent and then call .TriggerEvent() when the event should be triggered for all listeners. You must drag and drop the ScriptableObject from your assets like you did when creating the listener.

