Skip to content

BaseOnEventAction

BaseOnEventAction is an abstract base class for actions that subscribe to C# or Unity-style events and send PlayMaker events when those callbacks occur.

It is useful for actions that listen for events such as SceneManager.sceneLoaded, VideoPlayer.prepareCompleted, and other delegate-based callbacks.

Core Functionality

The class provides:

OnEvent Update Mode

BaseOnEventAction uses UpdateMode.OnEventUpdate by default:

public override UpdateMode DefaultUpdateMode => UpdateMode.OnEventUpdate;

This is appropriate for actions that do not need per-frame updates and only react when an event is raised.

Can Finish

BaseOnEventAction allows the action to finish:

public override bool CanFinish => true;

In most cases, an event-listener action subscribes in OnStart(), sends a PlayMaker event when the callback happens, and then finishes normally when the state changes or the action is stopped.

Event Validation Helper

The base class includes a helper for checking whether a required PlayMaker event has been assigned:

protected string CheckEventError(EventRef eventRef)

This returns:

  • null when the event setup is valid
  • "Event not set!" when the action is not blocking and no event has been assigned

This is useful for actions that should warn the user if they forgot to assign an event output.

Typical Pattern

Actions that inherit from BaseOnEventAction usually follow this pattern:

  1. Subscribe to the C# or Unity event in OnStart()
  2. Unsubscribe in OnStop()
  3. Send a PlayMaker event from the event callback

Example

SceneManagerOnSceneLoaded is a typical example:

using JetBrains.Annotations;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace HutongGames.PlayMaker.Actions.SceneManagement
{
    [System.Serializable]
    [PublicAPI]
    [ActionCategory(Category.SceneManager)]
    [ActionDescription("Send event when a Scene has loaded.")]
    public sealed class SceneManagerOnSceneLoaded : BaseOnEventAction
    {
        [Tooltip("Event sent when a Scene has loaded.")]
        [SerializeField]
        private EventRef _sceneLoaded;

        [OptionalField]
        [Tooltip("The scene that was loaded.")]
        [SerializeField, WriteOnly]
        private SceneRef _scene;

        [OptionalField]
        [Tooltip("The mode used to load the scene.")]
        [SerializeField, WriteOnly]
        private LoadSceneModeRef _loadSceneMode;

        public override void OnStart()
        {
            SceneManager.sceneLoaded += OnSceneLoaded;
        }

        public override void OnStop()
        {
            SceneManager.sceneLoaded -= OnSceneLoaded;
        }

        private void OnSceneLoaded(Scene scene, LoadSceneMode loadSceneMode)
        {
            _scene.Value = scene;
            _loadSceneMode.Value = loadSceneMode;
            SendEvent(_sceneLoaded);
        }
    }
}

With this setup, the base class provides the correct action behavior for event-driven actions, and the derived action only needs to manage subscription and translate the callback into PlayMaker data and events.