Skip to content

Event system

amel-unity edited this page Nov 17, 2020 · 6 revisions

Event System Diagram

The ScriptableObject event system is a way to avoid the use of the Singleton pattern. We want to avoid using the latter for multiple reasons such as the fact that it allows rigid connections between the systems since it’s not modular. In addition, the pattern does not allow the systems to exist separately and they will always depend on each other. This is of course hard to maintain and reuse.

Event system allows you to raise an event from a trigger such as when you enter a collider set to isTrigger or by performing an action like pressing a button. Once the event is raised we have what we call an event listener which will be basically waiting for when we have that event raised. On the graph above, we can visualize this system better. Both the script that fires the event and the event listener are Monobehaviours. Since we are using the Scriptable object Event as an anchor between the 2 systems, those Monobehaviour scripts can live in 2 different scenes in a completely independent way. As a reminder, the event will live in the assets folder since it is a ScriptableObject.

Table of Contents

Events examples in the project

Events can be raised with or without parameters. Here is a few examples of events we have in the project:

  • Void Events are events that are raised with no parameters. A good example of this is when we raise an event for game exit.
  • Int Events are events that are raised with an int parameter. We can use them when we unlock an achievement for instance.
  • Load Events are events that are raised with 2 parameters, an array of GameScene which are the scenes we want to load and a bool parameter to specify if we want to display a loading screen.

Create an Event Scriptable Object

To create an event, right click in the assets folder, under Game Event choose the type of event that fits best to your case.

Create an Event listener

Select the GameObject to which you want to attach the listener. Add a new script component to it and look for the listener script corresponding to the event you have created.

The Monobehaviour that listens to the event could be an independent script or included in another script. In the project, we are doing so for the scene loading event listener. In this case, the listener is included in the location loader script, meaning that the script acts as container of the scene loading methods but also as a listener. This is particularly useful if we are not using a specific listener more than once in the project.

Add a new type of Event - Example

You can add a new type of event depending on how many parameters you want to use on the raise method. This is an example of the Int event:

using UnityEngine.Events;
using UnityEngine;

[CreateAssetMenu(fileName = "IntGameEvent", menuName = "Game Event/Int")]
public class IntGameEvent : ScriptableObject
{
	public UnityAction<int> eventRaised;
	public void Raise(int value)
	{
		eventRaised.Invoke(value);
	}
}

Replace the int variable with the type you need. You can also add more than one parameter.

Add a new type of Listener - Example

You can add a new type of Listener depending on how many parameters were used to raise the event. This is an example of the Int Event listener:

using UnityEngine;
using UnityEngine.Events;

[System.Serializable]
public class IntEvent : UnityEvent<int>
{
}

public class IntEventListener : MonoBehaviour
{
	public IntGameEvent intGameEvent;
	public IntEvent OnEventRaised;

	private void OnEnable()
	{
		//Check if the event exists to avoid errors
		if (intGameEvent == null)
		{
			return;
		}
		intGameEvent.eventRaised += Respond;
	}

	private void OnDisable()
	{
		if (intGameEvent == null)
		{
			return;
		}
		intGameEvent.eventRaised -= Respond;
	}

	public void Respond(int value)
	{
		if (OnEventRaised == null)
		{
			return;
		}
		OnEventRaised.Invoke(value);
	}
}

More info

  • If you would like to know more about multiple arguments version of UnityEvent, you can see examples in the documentation using up to 4 parameters.

Home
Video materials

Basics

World building and Graphics

Game architecture

The game systems explained, with API examples. For programmers.

Game design

How-tos for designers to expand the game's gameplay.

  • Adding quests
  • Adding items
  • Creating dialogues
  • Making a cutscene
Clone this wiki locally