#if GRAPH_DESIGNER
/// ---------------------------------------------
/// Behavior Designer
/// Copyright (c) Opsive. All Rights Reserved.
/// https://www.opsive.com
/// ---------------------------------------------
namespace Opsive.BehaviorDesigner.Runtime.Tasks.Events
{
using Opsive.GraphDesigner.Runtime;
using Opsive.GraphDesigner.Runtime.Variables;
using Opsive.Shared.Events;
using UnityEngine;
///
/// The EventNode that is invoked when the specified event is received.
///
[AllowMultipleTypes]
[NodeDescription("EventNode that is invoked when the specified event is received")]
public class OnReceivedEvent : EventNode
{
[Tooltip("The name of the event that starts the branch.")]
[SerializeField] protected SharedVariable m_EventName;
[Tooltip("Optionally store the first sent argument.")]
[RequireShared] [SerializeField] protected SharedVariable m_StoredValue1;
[Tooltip("Optionally store the second sent argument.")]
[RequireShared] [SerializeField] protected SharedVariable m_StoredValue2;
[Tooltip("Optionally store the third sent argument.")]
[RequireShared] [SerializeField] protected SharedVariable m_StoredValue3;
private string m_RegisteredEventName;
private bool m_Initialized;
///
/// Initializes the node to the specified graph.
///
/// The graph that is initializing the task.
public override void Initialize(IGraph graph)
{
if (m_Initialized) {
return;
}
m_Initialized = true;
base.Initialize(graph);
m_BehaviorTree.OnBehaviorTreeDestroyed += Destroy;
m_EventName.OnValueChange += UpdateEvents;
if (m_StoredValue1 != null) { m_StoredValue1.OnValueChange += UpdateEvents; }
if (m_StoredValue2 != null) { m_StoredValue2.OnValueChange += UpdateEvents; }
if (m_StoredValue3 != null) { m_StoredValue3.OnValueChange += UpdateEvents; }
RegisterEvents();
}
///
/// Registers for the events.
///
private void RegisterEvents()
{
if (m_StoredValue1 == null || !m_StoredValue1.IsShared) {
EventHandler.RegisterEvent(m_BehaviorTree, m_EventName.Value, ReceivedEvent);
} else {
if (m_StoredValue2 == null || !m_StoredValue2.IsShared) {
EventHandler.RegisterEvent