Skip to content

JShull/FP_Event

Repository files navigation

FP Event

FP Event is a small, package-friendly event layer for Unity projects. It combines ScriptableObject event data, MonoBehaviour event emitters, and a singleton event manager so other FuzzPhyte packages can share a common event pattern.

The package is intended to sit underneath higher-level systems such as inventory, dialogue, graph, network, XR, and gameplay packages.

What It Provides

  • FPEvent: an abstract event data asset that inherits from FP_Data.
  • FPEventComponent<T>: a MonoBehaviour wrapper that triggers an FPEvent.
  • FP_EventManager<T>: a persistent singleton manager that records triggered event components and can process manager-side callbacks.
  • Samples~/SamplesURP: simple starter classes and a sample scene showing the generic pattern.

Requirements

  • Unity 6000.4 or newer.
  • com.fuzzphyte.utility, because the runtime assembly references FuzzPhyte.Utility.
  • Unity packages listed in package.json, including com.unity.mathematics and com.unity.textmeshpro.

See Installation~/Dependencies.md for the FP Utility dependency note.

Runtime Architecture

FPEvent

FPEvent is the base class for all event data assets.

  • Inherits from FP_Data, which supplies UniqueID.
  • Stores a Priority value used for event ordering.
  • Requires derived event types to implement Execute(object data = null).
  • Implements IComparable<FPEvent> so events can be sorted by priority.

FPEventComponent<T>

FPEventComponent<T> is the bridge between a scene object and an event asset.

  • Assign GameEvent in the Inspector.
  • TriggerEvent() invokes OnEventTriggered.
  • The default Awake() wires OnEventTriggered to the component's ExecuteEvent().
  • After local execution, TriggerEvent() records the component with FP_EventManager<T>.Instance.
  • Derived classes provide event data through GetEventData(), immediate behavior through ExecuteEvent(), and manager-side behavior through ManagerEvent().

FP_EventManager<T>

FP_EventManager<T> records triggered components and can process them later.

  • Keeps one persistent singleton instance per event type.
  • Uses DontDestroyOnLoad.
  • Records triggered components in insertion order by default.
  • Can use a priority queue if a derived manager enables UsePriorityQueue.
  • Calls each recorded component's ManagerEvent() when ProcessRecordedManagerEvents() runs.

Quick Start

Create a concrete event data asset:

using FuzzPhyte.SystemEvent;
using UnityEngine;

[CreateAssetMenu(menuName = "FuzzPhyte/Events/Door Event")]
public class DoorEvent : FPEvent
{
    public override void Execute(object data = null)
    {
        Debug.Log($"Door event executed with data: {data}");
    }
}

Create a component that triggers the event:

using FuzzPhyte.SystemEvent;
using UnityEngine;

public class DoorEventComponent : FPEventComponent<DoorEvent>
{
    protected override object GetEventData()
    {
        return gameObject;
    }

    protected override void ExecuteEvent()
    {
        GameEvent.Execute(GetEventData());
    }

    public override void ManagerEvent()
    {
        Debug.Log($"Manager processed {GameEvent.name}");
    }
}

Create a manager for that event type:

using FuzzPhyte.SystemEvent;
using UnityEngine;

public class DoorEventManager : FP_EventManager<DoorEvent>
{
    private void Update()
    {
        ProcessRecordedManagerEvents();
    }
}

Then in Unity:

  1. Create a DoorEvent asset from the Create Asset menu.
  2. Add DoorEventManager to one GameObject in the scene.
  3. Add DoorEventComponent to any GameObject that should fire the event.
  4. Assign the DoorEvent asset to the component's GameEvent field.
  5. Call TriggerEvent() from gameplay code, UI, animation events, or another UnityEvent.

Event Flow

Caller
  -> FPEventComponent<T>.TriggerEvent()
  -> UnityEvent OnEventTriggered
  -> FPEventComponent<T>.ExecuteEvent()
  -> FP_EventManager<T>.RecordEvent(component)
  -> FP_EventManager<T>.ProcessRecordedManagerEvents()
  -> FPEventComponent<T>.ManagerEvent()

Use ExecuteEvent() for immediate local behavior and ManagerEvent() for behavior that should be coordinated by a manager.

Priority Notes

Priority is compared in ascending order. When a manager uses the priority queue, lower priority values are dequeued first. The base manager keeps UsePriorityQueue disabled, so derived managers should opt in when priority processing is needed.

public class PriorityDoorEventManager : FP_EventManager<DoorEvent>
{
    public override void Awake()
    {
        base.Awake();
        UsePriorityQueue = true;
    }
}

Samples

Import Samples~/SamplesURP through Unity Package Manager to inspect:

  • EventDataSample: minimal FPEvent implementation.
  • EventFPSample: minimal FPEventComponent<FPEvent> implementation.
  • EventMgrSample: minimal FP_EventManager<FPEvent> implementation.
  • EventTest.unity: sample scene.

These samples are intentionally lightweight and are best used as inheritance templates.

Extending

When creating events for another package:

  • Derive a specific event data type from FPEvent.
  • Derive a matching component from FPEventComponent<TEvent>.
  • Add exactly one matching FP_EventManager<TEvent> instance to the active scene or bootstrap flow.
  • Keep event assets reusable and scene-independent.
  • Pass scene-specific context through GetEventData() instead of storing scene references on the asset.

Dependencies

Please see package.json for Unity package dependencies.

Runtime assembly reference:

  • com.fuzzphyte.utility

License Notes

See LICENSE.md for details.

Contact

About

Setup for FP_Event class that sits onto other core FP_Packages

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages