Skip to content

Latest commit

 

History

History
71 lines (45 loc) · 2.41 KB

File metadata and controls

71 lines (45 loc) · 2.41 KB

Class in Incantium.Events.Send | Assembled in Incantium.ScriptableEvents

Extends ScriptableObject

Description

EventBus

The EventBus is a ScriptableObject that other scripts can subscribe to and listen to invocations from other scripts.

The main use case is to decouple code that needs to be developed and tested separately. In many circumstances, game objects that are depended on other systems (through references) cannot be tested in isolation without their references being set. The EventBus solves this problem by creating a common point to which the dependent system are coupled.

As seen in the image at the top, the EventBus ScriptableObject has a build-in custom inspector where every subscription is visible. It also has an Invoke button to invoke every subscriber from the inspector for testing purposes, alongside a Reset button to remove all subscriptions.

Example

using Incantium.Events.Send;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    [SerializeField]
    private EventBus<string> eventBus;

    // Subscribe to the event anywhere you want.
    private void Start() => eventBus.onReceive += Receive;

    // Don't forget to unsubscribe from the event at some point.
    private void OnDestroy() => eventBus.onReceive -= Receive;

    // Listen to the event.
    private void Receive(string message) => Debug.Log(message);
    
    // Invoke the event when needed.
    public void OnClick() => eventBus.Send("Hello World");
}

Notes

Info: This package includes basic event bus implementations for default/void, boolean, integer, float, double, string, Vector2, Vector3, and GameObject.

Variables

📗 Action onReceive;

Event, when subscribed to, will be notified when something else send a message.

📗 int count

Amount of event handlers connected to this event.

Methods

📗 void Send()

Method to call all methods subscribed to onReceive to deliver a message.

📗 void Reset()

Method to remove all methods from this event.