Local Event System

LocalEventManager

The central component to the local message passing system is the LocalEventManager. The LocalEventManager is used to trigger events that can be subscribed to by any other component in the scene. This serves the purpose of allowing synchronizing and initilizing of components across a scene without requiring polling or maintaining unnecessary references.

How it Works

The Local Event Manager is essentially an implementation of the observer pattern using a generic delegate and providing static reference to a singleton instance.

The Local Event Manager has one event, LocalEventTriggered, which is associated with a sender object and LocalEventArgs. LocalEventArgs is essentially a wrapper to the LocalEvent Enum, which serves to differentiate events. As a delegate LocalEventTriggered maintains a list of methods that have the same signature (sender & localEventArgs) that will be called when the event is triggered.

Subscribing to an Event

To subscribe to LocalEventTriggered you must have a function with the same signature as the event and then use the addition assignment operator

ASLLocalEventManager.LocalEventTriggered += OnLocalEvent;

To avoid a memory leak, scripts subscribing to the event need to unsubscribe when they are destroyed the subtraction assignment operator.

ASLLocalEventManager.LocalEventTriggered -= OnLocalEvent;

Triggering an Event

To trigger an event you call the event/delegate as function.

LocalEventTriggered(sender, new LocalEventArgs { MyEvent = eventToTrigger });

This has been abstracted with the Trigger function.

Handling an Event

After you have subscribed to the event and it has been triggered then the function at the end of the addition assignment will be called. To proceed with handling the event the LocalEventArgs parameter should be checked to determine what code to execute in response to the event. See the following code segement for an example of handling a local event.

protected override void OnLocalEvent(object sender, ASLLocalEventManager.LocalEventArgs args)
{
        
   Debug.Log("Portal Instantiator has received an event: " + args.MyEvent.ToString());
   switch (args.MyEvent)
   {
      case ASLLocalEventManager.LocalEvents.PortalManagerPlayerSet:
         {
            PlayerSetEventHandler();
            break;
         }
         case ASLLocalEventManager.LocalEvents.TriggerPortalCreation:
         {
            PortalCreationHandler();
            break;
         }
         default:
         {
         break;
         }
   }
}                 

The switch statement allows for specific functions to be called asynchronously, depending on the event.

Creating New Events

Creating a new event is as simple as adding a new entry to the LocalEvents enum.

After it has been added to enum then you have to introduce calls to trigger the event and had a case with the new event in the appropriate EventHandlers.



This has largely been automated with the LocalEventHandler class that you can inherit from.

LocalEventHandler

LocalEventHandler is a virtual class that automates the subscription to LocalEvent delegate & has functions to override for event handling. Some examples can be seen in the follow scripts.

The source code for these scripts provide examples of overriding the OnLocalEvent function.

Additional Features

The LocalEventHandler also includes functions OnJoinedRoom() & OnPhotonInstantiate. These can be implemented and will be called upon connecting to a room or upon an object's instantation through the NetworkManager/ObjectInteractionManager. This is useful to avoid polling and to create networked objects in ASL as soon as possible.