2026-05-10 11:47:55 -04:00
#if GRAPH_DESIGNER
2025-11-25 08:19:33 -05:00
/// ---------------------------------------------
/// Behavior Designer
/// Copyright (c) Opsive. All Rights Reserved.
/// https://www.opsive.com
/// ---------------------------------------------
namespace Opsive.BehaviorDesigner.Runtime.Tasks.Composites
{
using Opsive.BehaviorDesigner.Runtime.Components ;
using Opsive.BehaviorDesigner.Runtime.Utility ;
2026-05-10 11:47:55 -04:00
using Opsive.GraphDesigner.Runtime.Variables.ECS ;
2025-11-25 08:19:33 -05:00
using Opsive.GraphDesigner.Runtime ;
using Unity.Burst ;
using Unity.Entities ;
using UnityEngine ;
/// <summary>
/// A node representation of the parallel selector task.
/// </summary>
[NodeIcon("d47aff1a00bcc6d4da8ca0df32ed8415", "108591b5d7a6bd94383d16a62cb3b4a7")]
[ Opsive . Shared . Utility . Description ( "Similar to the selector task, the parallel selector task will return success as soon as a child task returns success. " +
"The parallel task will run all of its children tasks simultaneously versus running each task one at a time. " +
"If one tasks returns success the parallel selector task will end all of the child tasks and return success. " +
"If every child task returns failure then the parallel selector task will return failure." ) ]
2026-05-10 11:47:55 -04:00
public class ParallelSelector : ECSCompositeTask < ParallelSelectorTaskSystem , ParallelSelectorComponent , ParallelSelectorFlag > , IParentNode , IParallelNode
2025-11-25 08:19:33 -05:00
{
/// <summary>
/// Returns a new TBufferElement for use by the system.
/// </summary>
/// <returns>A new TBufferElement for use by the system.</returns>
public override ParallelSelectorComponent GetBufferElement ( )
{
return new ParallelSelectorComponent ( ) {
Index = RuntimeIndex ,
} ;
}
/// <summary>
/// Adds the IBufferElementData to the entity.
/// </summary>
/// <param name="world">The world that the entity exists in.</param>
/// <param name="entity">The entity that the IBufferElementData should be assigned to.</param>
2026-05-10 11:47:55 -04:00
/// <param name="registry">The ECS variable registry for registering SharedVariable fields.</param>
2025-11-25 08:19:33 -05:00
/// <param name="gameObject">The GameObject that the entity is attached to.</param>
/// <returns>The index of the element within the buffer.</returns>
2026-05-10 11:47:55 -04:00
public override int AddBufferElement ( World world , Entity entity , ECSVariableRegistry registry , GameObject gameObject )
2025-11-25 08:19:33 -05:00
{
2026-05-10 11:47:55 -04:00
var index = base . AddBufferElement ( world , entity , registry , gameObject ) ;
2025-11-25 08:19:33 -05:00
ComponentUtility . AddInterruptComponents ( world . EntityManager , entity ) ;
return index ;
}
}
/// <summary>
/// The DOTS data structure for the ParallelSelector class.
/// </summary>
public struct ParallelSelectorComponent : IBufferElementData
{
[Tooltip("The index of the node.")]
[SerializeField] ushort m_Index ;
public ushort Index { get = > m_Index ; set = > m_Index = value ; }
}
/// <summary>
/// A DOTS tag indicating when a ParallelSelector node is active.
/// </summary>
public struct ParallelSelectorFlag : IComponentData , IEnableableComponent { }
/// <summary>
/// Runs the ParallelSelector logic.
/// </summary>
[DisableAutoCreation]
public partial struct ParallelSelectorTaskSystem : ISystem
{
private EntityQuery m_Query ;
/// <summary>
/// Builds the query.
/// </summary>
/// <param name="state">THe current SystemState.</param>
private void OnCreate ( ref SystemState state )
{
m_Query = SystemAPI . QueryBuilder ( ) . WithAllRW < BranchComponent > ( ) . WithAllRW < TaskComponent > ( ) . WithAllRW < ParallelSelectorComponent > ( ) . WithAll < ParallelSelectorFlag , EvaluateFlag > ( ) . Build ( ) ;
}
/// <summary>
/// Creates the job.
/// </summary>
/// <param name="state">The current state of the system.</param>
[BurstCompile]
private void OnUpdate ( ref SystemState state )
{
2026-02-13 09:22:11 -05:00
var ecb = SystemAPI . GetSingleton < EndSimulationEntityCommandBufferSystem . Singleton > ( ) . CreateCommandBuffer ( state . WorldUnmanaged ) ;
2025-11-25 08:19:33 -05:00
state . Dependency = new ParallelSelectorJob ( )
{
2026-02-13 09:22:11 -05:00
EntityCommandBuffer = ecb . AsParallelWriter ( )
2025-11-25 08:19:33 -05:00
} . ScheduleParallel ( m_Query , state . Dependency ) ;
}
/// <summary>
/// Job which executes the task logic.
/// </summary>
[BurstCompile]
private partial struct ParallelSelectorJob : IJobEntity
{
[Tooltip("CommandBuffer which sets the component data.")]
public EntityCommandBuffer . ParallelWriter EntityCommandBuffer ;
/// <summary>
/// Executes the parallel selector logic.
/// </summary>
/// <param name="entity">The entity that is being acted upon.</param>
/// <param name="entityIndex">The index of the entity.</param>
/// <param name="branchComponents">An array of BranchComponents.</param>
/// <param name="taskComponents">An array of TaskComponents.</param>
/// <param name="parallelSelectorComponents">An array of ParallelSelectorComponents.</param>
[BurstCompile]
public void Execute ( Entity entity , [ EntityIndexInQuery ] int entityIndex , ref DynamicBuffer < BranchComponent > branchComponents , ref DynamicBuffer < TaskComponent > taskComponents , ref DynamicBuffer < ParallelSelectorComponent > parallelSelectorComponents )
{
for ( int i = 0 ; i < parallelSelectorComponents . Length ; + + i ) {
var parallelSelectorComponent = parallelSelectorComponents [ i ] ;
var taskComponent = taskComponents [ parallelSelectorComponent . Index ] ;
2026-05-10 11:47:55 -04:00
var taskStatus = taskComponent . Status ;
// Skip inactive tasks before any branch lookups.
if ( taskStatus ! = TaskStatus . Queued & & taskStatus ! = TaskStatus . Running ) {
continue ;
}
2025-11-25 08:19:33 -05:00
var branchComponent = branchComponents [ taskComponent . BranchIndex ] ;
2026-01-03 18:19:39 -05:00
// Do not continue if there will be an interrupt or the branch cannot execute.
if ( branchComponent . InterruptType ! = InterruptType . None | | ! branchComponent . CanExecute ) {
2025-11-25 08:19:33 -05:00
continue ;
}
ushort childIndex ;
TaskComponent childTaskComponent ;
2026-05-10 11:47:55 -04:00
if ( taskStatus = = TaskStatus . Queued ) {
2025-11-25 08:19:33 -05:00
taskComponent . Status = TaskStatus . Running ;
taskComponents [ taskComponent . Index ] = taskComponent ;
childIndex = ( ushort ) ( parallelSelectorComponent . Index + 1 ) ;
while ( childIndex ! = ushort . MaxValue ) {
childTaskComponent = taskComponents [ childIndex ] ;
2026-05-10 11:47:55 -04:00
if ( childTaskComponent . Status ! = TaskStatus . Queued ) {
childTaskComponent . Status = TaskStatus . Queued ;
taskComponents [ childIndex ] = childTaskComponent ;
}
2025-11-25 08:19:33 -05:00
var childBranchComponent = branchComponents [ childTaskComponent . BranchIndex ] ;
2026-05-10 11:47:55 -04:00
if ( childBranchComponent . NextIndex ! = childTaskComponent . Index ) {
childBranchComponent . NextIndex = childTaskComponent . Index ;
branchComponents [ childTaskComponent . BranchIndex ] = childBranchComponent ;
}
2025-11-25 08:19:33 -05:00
2026-05-10 11:47:55 -04:00
childIndex = childTaskComponent . SiblingIndex ;
2025-11-25 08:19:33 -05:00
}
}
var childSuccess = false ;
var childrenRunning = false ;
childIndex = ( ushort ) ( parallelSelectorComponent . Index + 1 ) ;
while ( childIndex ! = ushort . MaxValue ) {
childTaskComponent = taskComponents [ childIndex ] ;
if ( childTaskComponent . Status = = TaskStatus . Queued | | childTaskComponent . Status = = TaskStatus . Running ) {
childrenRunning = true ;
} else if ( childTaskComponent . Status = = TaskStatus . Failure ) {
var childBranchComponent = branchComponents [ childTaskComponent . BranchIndex ] ;
2026-05-10 11:47:55 -04:00
if ( childBranchComponent . ActiveIndex ! = ushort . MaxValue & & childBranchComponent . NextIndex ! = ushort . MaxValue ) {
2025-11-25 08:19:33 -05:00
childBranchComponent . NextIndex = ushort . MaxValue ;
branchComponents [ childTaskComponent . BranchIndex ] = childBranchComponent ;
}
} else if ( childTaskComponent . Status = = TaskStatus . Success ) {
childSuccess = true ;
var childBranchComponent = branchComponents [ childTaskComponent . BranchIndex ] ;
2026-05-10 11:47:55 -04:00
if ( childBranchComponent . NextIndex ! = ushort . MaxValue ) {
childBranchComponent . NextIndex = ushort . MaxValue ;
branchComponents [ childTaskComponent . BranchIndex ] = childBranchComponent ;
}
2025-11-25 08:19:33 -05:00
break ;
}
2026-05-10 11:47:55 -04:00
childIndex = childTaskComponent . SiblingIndex ;
2025-11-25 08:19:33 -05:00
}
// If a single child succeeds then all tasks should be stopped.
if ( childSuccess ) {
2026-05-10 11:47:55 -04:00
var maxChildIndex = taskComponent . ChildUpperIndex > taskComponent . Index
? taskComponent . ChildUpperIndex
: ( ushort ) ( taskComponent . Index ) ;
var interruptedFlagSet = false ;
2025-11-25 08:19:33 -05:00
for ( ushort j = ( ushort ) ( taskComponent . Index + 1 ) ; j < = maxChildIndex ; + + j ) {
childTaskComponent = taskComponents [ j ] ;
if ( childTaskComponent . Status = = TaskStatus . Running | | childTaskComponent . Status = = TaskStatus . Queued ) {
childTaskComponent . Status = TaskStatus . Failure ;
taskComponents [ j ] = childTaskComponent ;
branchComponent = branchComponents [ childTaskComponent . BranchIndex ] ;
2026-05-10 11:47:55 -04:00
if ( ! interruptedFlagSet ) {
EntityCommandBuffer . SetComponentEnabled < InterruptedFlag > ( entityIndex , entity , true ) ;
interruptedFlagSet = true ;
}
2025-11-25 08:19:33 -05:00
if ( branchComponent . ActiveIndex = = childTaskComponent . Index ) {
2026-05-10 11:47:55 -04:00
if ( branchComponent . NextIndex ! = ushort . MaxValue ) {
branchComponent . NextIndex = ushort . MaxValue ;
branchComponents [ childTaskComponent . BranchIndex ] = branchComponent ;
}
2025-11-25 08:19:33 -05:00
}
}
}
} else if ( childrenRunning ) {
continue ;
}
// No more children are running. Resume the parent task.
taskComponent . Status = TaskStatus . Success ;
taskComponents [ taskComponent . Index ] = taskComponent ;
2026-05-10 11:47:55 -04:00
if ( branchComponent . NextIndex ! = taskComponent . ParentIndex ) {
branchComponent . NextIndex = taskComponent . ParentIndex ;
branchComponents [ taskComponent . BranchIndex ] = branchComponent ;
}
2025-11-25 08:19:33 -05:00
}
}
}
}
}
#endif