#if GRAPH_DESIGNER
/// ---------------------------------------------
/// Behavior Designer
/// Copyright (c) Opsive. All Rights Reserved.
/// https://www.opsive.com
/// ---------------------------------------------
namespace Opsive.BehaviorDesigner.Runtime.Tasks.Conditionals
{
using Opsive.GraphDesigner.Runtime;
using Opsive.GraphDesigner.Runtime.Variables;
using Opsive.Shared.Events;
using UnityEngine;
///
/// A TaskObject implementation of the Conditional task. This class can be used when the task should not be grouped by the StackedConditional task.
///
[NodeIcon("e6fc90c130121da4f9067b5e15b02975", "69959064b54a0cb4cb077dbb6967a3e1")]
[NodeDescription("Returns success as soon as the event specified by eventName has been received.")]
public class HasReceivedEvent : TargetBehaviorTreeConditional
{
[Tooltip("The name of the event that should be registered.")]
[SerializeField] protected SharedVariable m_EventName;
[Tooltip("Is the event a global event?")]
[SerializeField] protected SharedVariable m_GlobalEvent;
[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_EventRegistered;
private bool m_EventReceived;
///
/// The behavior tree has started.
///
public override void OnBehaviorTreeStarted()
{
base.OnBehaviorTreeStarted();
RegisterEvents();
}
///
/// Initializes the target behavior tree.
///
protected override void InitializeTarget()
{
if (m_ResolvedBehaviorTree != null) {
UnregisterEvents();
}
base.InitializeTarget();
RegisterEvents();
}
///
/// Registers for the events.
///
private void RegisterEvents()
{
if (m_EventRegistered) {
return;
}
if (string.IsNullOrEmpty(m_EventName.Value)) {
Debug.LogError("Error: Unable to receive event. The event name is empty.");
return;
}
if (m_StoredValue1 == null || !m_StoredValue1.IsShared) {
if (m_GlobalEvent.Value) {
EventHandler.RegisterEvent(m_EventName.Value, ReceivedEvent);
} else {
EventHandler.RegisterEvent(m_ResolvedBehaviorTree, m_EventName.Value, ReceivedEvent);
}
} else {
if (m_StoredValue2 == null || !m_StoredValue2.IsShared) {
if (m_GlobalEvent.Value) {
EventHandler.RegisterEvent