Files
Cielonos/Packages/com.opsive.behaviordesigner/Runtime/Tasks/Actions/StartBehaviorTree.cs

72 lines
2.4 KiB
C#
Raw Normal View History

2025-11-25 08:19:33 -05:00
#if GRAPH_DESIGNER
/// ---------------------------------------------
/// Behavior Designer
/// Copyright (c) Opsive. All Rights Reserved.
/// https://www.opsive.com
/// ---------------------------------------------
2026-05-10 11:47:55 -04:00
namespace Opsive.BehaviorDesigner.Runtime.Tasks.Actions.BehaviorTree
2025-11-25 08:19:33 -05:00
{
using Opsive.GraphDesigner.Runtime;
using System.Collections;
using UnityEngine;
2026-05-10 11:47:55 -04:00
using UnityEngine.Scripting.APIUpdating;
2025-11-25 08:19:33 -05:00
[NodeIcon("e0a8f1df788b6274a9a24003859dfa7e")]
[Opsive.Shared.Utility.Description("Starts the specified behavior tree.")]
2026-05-10 11:47:55 -04:00
[MovedFrom(false, "Opsive.BehaviorDesigner.Runtime.Tasks.Actions", "Opsive.BehaviorDesigner.Runtime", "StartBehaviorTree")]
2025-11-25 08:19:33 -05:00
public class StartBehaviorTree : TargetBehaviorTreeAction
{
2026-05-10 11:47:55 -04:00
[Tooltip("Wait for the behaviro tree to complete before returning Success.")]
[SerializeField] protected bool m_WaitForCompletion;
2025-11-25 08:19:33 -05:00
private TaskStatus m_Status;
/// <summary>
/// The task has started.
/// </summary>
public override void OnStart()
{
m_Status = TaskStatus.Queued;
}
/// <summary>
/// Executes the task logic.
/// </summary>
/// <returns>The status of the task.</returns>
public override TaskStatus OnUpdate()
{
// The coroutine has already been started if the status is not queued.
if (m_Status != TaskStatus.Queued) {
2026-05-10 11:47:55 -04:00
if (m_WaitForCompletion && m_Status == TaskStatus.Running && !m_ResolvedBehaviorTree.IsRunning()) {
m_Status = m_ResolvedBehaviorTree.Status;
}
2025-11-25 08:19:33 -05:00
return m_Status;
}
2026-05-10 11:47:55 -04:00
if (m_ResolvedBehaviorTree == null || m_ResolvedBehaviorTree.IsRunning()) {
2025-11-25 08:19:33 -05:00
return TaskStatus.Failure;
}
m_Status = TaskStatus.Running;
StartCoroutine(StartBehavior());
return m_Status;
}
/// <summary>
/// Starts the behavior tree using a coroutine to allow structural changes.
/// </summary>
private IEnumerator StartBehavior()
{
yield return new WaitForEndOfFrame();
2026-05-10 11:47:55 -04:00
if (m_ResolvedBehaviorTree.StartBehavior()) {
if (!m_WaitForCompletion) {
m_Status = TaskStatus.Success;
}
} else {
m_Status = TaskStatus.Failure;
}
2025-11-25 08:19:33 -05:00
}
}
}
#endif