2. Quick Start

When working with EventForge, there are usually 3 steps to establish a connection between different classes.

1) Creating a new Event

EventForge uses structs at its core to send data from one class to another. These structs can contain data but it is not required.

Example

public struct MyEvent
{
    private static ExampleStringEvent _event;
    public string valueToShare { get; private set; }

    public static void Trigger(string value)
    {
        _event.valueToShare = value;
        EventManager.TriggerEvent(_event);
    }
}

2) Triggering an event

From anywhere within your code you can now call the Trigger method on your newly created Struct.

Example:

// Trigger an event
MyEvent.Trigger("My cool string to share");

3) Receiving an event

Listeners are not required by EventForge, events don't have to be consumed. Any number of listeners can listen to an event (0-infinite).

Example:

// Implement the Listener interface on the class that should receive the event.
public class MyEventReceiver: MonoBehaviour, IEventListener<MyEvent>
{
        // Method that gets triggered when the event occurs. There is an override per event type.
        public void OnEvent(MyEvent eventType)
        {
            // Do something with your event
        }

        private void OnEnable()
        {
            // Add the event listener, usually done in the OnEnable lifecycle method.
            EventManager.AddListener(this);
            // If you are listening to multiple events in this class, you have to use typing like shown below.
            EventManager.AddListener<MyEvent>(this);
        }

        private void OnDisable()
        {
            // Remove the event listener, usually done in the OnDisable lifecycle method.
            EventManager.RemoveListener(this);
            // If you are listening to multiple events in this class, you have to use typing like shown below.
            EventManager.RemoveListener<MyEvent>(this);
        }
}

A full example can be found in the ExampleScene.

This page was last edited on 2024-03-03 17:31

Powered by Wiki|Docs

This page was last edited on 2024-03-03 17:31

BiteMe Games

Powered by Wiki|Docs