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 Opsive.Shared.Utility ;
using System ;
using Unity.Burst ;
using Unity.Collections ;
2026-05-10 11:47:55 -04:00
using Unity.Collections.LowLevel.Unsafe ;
2025-11-25 08:19:33 -05:00
using Unity.Entities ;
using UnityEngine ;
/// <summary>
/// A node representation of the priority selector task.
/// </summary>
[NodeIcon("cea0f2b6cee06a742bb35dcc40202e8e", "744afc2640950e045961296f1d5800d7")]
[ Opsive . Shared . Utility . Description ( "Similar to the selector task, the priority selector task will return success as soon as a child task returns success. " +
"Instead of running the tasks sequentially from left to right within the tree, the priority selector will ask the task what its priority is to determine the order. " +
"The higher priority tasks have a higher chance at being run first." ) ]
2026-05-10 11:47:55 -04:00
public class PrioritySelector : ECSCompositeTask < PrioritySelectorTaskSystem , PrioritySelectorComponent , PrioritySelectorFlag > , IParentNode , ISavableTask , ICloneable
2025-11-25 08:19:33 -05:00
{
private ushort m_ComponentIndex ;
/// <summary>
/// Returns a new TBufferElement for use by the system.
/// </summary>
/// <returns>A new TBufferElement for use by the system.</returns>
public override PrioritySelectorComponent GetBufferElement ( )
{
return new PrioritySelectorComponent ( )
{
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
m_ComponentIndex = ( ushort ) base . AddBufferElement ( world , entity , registry , gameObject ) ;
2025-11-25 08:19:33 -05:00
return m_ComponentIndex ;
}
/// <summary>
/// Specifies the type of reflection that should be used to save the task.
/// </summary>
/// <param name="index">The index of the sub-task. This is used for the task set allowing each contained task to have their own save type.</param>
public MemberVisibility GetSaveReflectionType ( int index ) { return MemberVisibility . None ; }
/// <summary>
/// Returns the current task state.
/// </summary>
/// <param name="world">The DOTS world.</param>
/// <param name="entity">The DOTS entity.</param>
/// <returns>The current task state.</returns>
public object Save ( World world , Entity entity )
{
var prioritySelectorComponents = world . EntityManager . GetBuffer < PrioritySelectorComponent > ( entity ) ;
var prioritySelectorComponent = prioritySelectorComponents [ m_ComponentIndex ] ;
2026-05-10 11:47:55 -04:00
// Save the active child index.
return prioritySelectorComponent . ActiveRelativeChildIndex ;
2025-11-25 08:19:33 -05:00
}
/// <summary>
/// Loads the previous task state.
/// </summary>
/// <param name="saveData">The previous task state.</param>
/// <param name="world">The DOTS world.</param>
/// <param name="entity">The DOTS entity.</param>
public void Load ( object saveData , World world , Entity entity )
{
var prioritySelectorComponents = world . EntityManager . GetBuffer < PrioritySelectorComponent > ( entity ) ;
var prioritySelectorComponent = prioritySelectorComponents [ m_ComponentIndex ] ;
2026-05-10 11:47:55 -04:00
prioritySelectorComponent . ActiveRelativeChildIndex = ( ushort ) saveData ;
2025-11-25 08:19:33 -05:00
prioritySelectorComponents [ m_ComponentIndex ] = prioritySelectorComponent ;
}
/// <summary>
/// Creates a deep clone of the component.
/// </summary>
/// <returns>A deep clone of the component.</returns>
public object Clone ( )
{
var clone = Activator . CreateInstance < PrioritySelector > ( ) ;
clone . Index = Index ;
clone . ParentIndex = ParentIndex ;
clone . SiblingIndex = SiblingIndex ;
2026-05-10 11:47:55 -04:00
clone . Enabled = Enabled ;
2025-11-25 08:19:33 -05:00
return clone ;
}
}
2026-05-10 11:47:55 -04:00
/// <summary>
/// Immutable blob entry mapping a task index to its PriorityValueComponent index.
/// </summary>
public struct PriorityItemBlobEntry
{
[Tooltip("The index of the task.")]
public ushort TaskIndex ;
[Tooltip("The index of the PriorityValueComponent. ushort.MaxValue indicates no corresponding component.")]
public ushort PriorityValueIndex ;
}
/// <summary>
/// Blob asset storing the priority item entries (task index and priority value index per child).
/// </summary>
public struct PriorityItemsBlob
{
[Tooltip("The priority item entries.")]
public BlobArray < PriorityItemBlobEntry > Items ;
}
2025-11-25 08:19:33 -05:00
/// <summary>
/// The DOTS data structure for the PrioritySelector class.
/// </summary>
public struct PrioritySelectorComponent : IBufferElementData
{
[Tooltip("The index of the node.")]
public ushort Index ;
[Tooltip("The relative index of the child that is currently active.")]
public ushort ActiveRelativeChildIndex ;
2026-05-10 11:47:55 -04:00
[Tooltip("The blob containing task-to-priority-value mapping per child.")]
public BlobAssetReference < PriorityItemsBlob > PriorityItems ;
[Tooltip("Task indices in sorted order by priority (highest first).")]
public UnsafeList < ushort > SortedOrder ;
2025-11-25 08:19:33 -05:00
}
/// <summary>
/// DOTS structure that contains the most recently priority of the task.
/// </summary>
public struct PriorityValueComponent : IBufferElementData
{
[Tooltip("The index of the task.")]
public ushort Index ;
[Tooltip("The current priority value. The higher the value the more likely it will be selected.")]
public float Value ;
}
/// <summary>
/// A DOTS tag indicating when a PrioritySelector node is active.
/// </summary>
public struct PrioritySelectorFlag : IComponentData , IEnableableComponent { }
/// <summary>
/// Runs the PrioritySelector logic.
/// </summary>
[DisableAutoCreation]
public partial struct PrioritySelectorTaskSystem : ISystem
{
2026-05-10 11:47:55 -04:00
private EntityQuery m_WithValuesQuery ;
private EntityQuery m_WithoutValuesQuery ;
2025-11-25 08:19:33 -05:00
/// <summary>
2026-05-10 11:47:55 -04:00
/// Builds the queries.
/// </summary>
/// <param name="state">The current state of the system.</param>
private void OnCreate ( ref SystemState state )
{
m_WithValuesQuery = SystemAPI . QueryBuilder ( )
. WithAllRW < BranchComponent > ( )
. WithAllRW < TaskComponent > ( )
. WithAllRW < PrioritySelectorComponent > ( )
. WithAllRW < PriorityValueComponent > ( )
. WithAll < PrioritySelectorFlag , EvaluateFlag > ( )
. Build ( ) ;
// Special case where there is no PriorityValueComponent buffer.
m_WithoutValuesQuery = SystemAPI . QueryBuilder ( )
. WithAllRW < PrioritySelectorComponent > ( )
. WithAllRW < TaskComponent > ( )
. WithAllRW < BranchComponent > ( )
. WithAll < PrioritySelectorFlag , EvaluateFlag > ( )
. WithNone < PriorityValueComponent > ( )
. Build ( ) ;
}
/// <summary>
/// Creates the jobs.
2025-11-25 08:19:33 -05:00
/// </summary>
/// <param name="state">The current state of the system.</param>
[BurstCompile]
private void OnUpdate ( ref SystemState state )
{
2026-05-10 11:47:55 -04:00
state . Dependency = new PrioritySelectorWithValuesJob ( ) . ScheduleParallel ( m_WithValuesQuery , state . Dependency ) ;
state . Dependency = new PrioritySelectorWithoutValuesJob ( ) . ScheduleParallel ( m_WithoutValuesQuery , state . Dependency ) ;
}
2025-11-25 08:19:33 -05:00
2026-05-10 11:47:55 -04:00
/// <summary>
/// Job which executes the PrioritySelector logic when PriorityValueComponent exists.
/// </summary>
[BurstCompile]
private partial struct PrioritySelectorWithValuesJob : IJobEntity
{
/// <summary>
/// Executes the PrioritySelector logic.
/// </summary>
/// <param name="branchComponents">An array of BranchComponents.</param>
/// <param name="taskComponents">An array of TaskComponents.</param>
/// <param name="prioritySelectorComponents">An array of PrioritySelectorComponents.</param>
/// <param name="priorityValueComponents">An array of PriorityValueComponents.</param>
[BurstCompile]
public void Execute ( ref DynamicBuffer < BranchComponent > branchComponents , ref DynamicBuffer < TaskComponent > taskComponents ,
ref DynamicBuffer < PrioritySelectorComponent > prioritySelectorComponents , ref DynamicBuffer < PriorityValueComponent > priorityValueComponents )
{
2025-11-25 08:19:33 -05:00
for ( int i = 0 ; i < prioritySelectorComponents . Length ; + + i ) {
var prioritySelectorComponent = prioritySelectorComponents [ i ] ;
var taskComponent = taskComponents [ prioritySelectorComponent . Index ] ;
2026-05-10 11:47:55 -04:00
var taskStatus = taskComponent . Status ;
// Skip inactive tasks before branch lookups.
if ( taskStatus ! = TaskStatus . Queued & & taskStatus ! = TaskStatus . Running ) {
continue ;
}
2025-11-25 08:19:33 -05:00
2026-05-10 11:47:55 -04: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 ;
}
2026-05-10 11:47:55 -04:00
if ( taskStatus = = TaskStatus . Queued ) {
2025-11-25 08:19:33 -05:00
taskComponent . Status = TaskStatus . Running ;
2026-05-10 11:47:55 -04:00
taskComponents [ taskComponent . Index ] = taskComponent ;
// Build the priority items blob when first run.
if ( ! prioritySelectorComponent . PriorityItems . IsCreated ) {
var childCount = TraversalUtility . GetImmediateChildCount ( ref taskComponent , ref taskComponents ) ;
var builder = new BlobBuilder ( Allocator . Temp ) ;
ref var root = ref builder . ConstructRoot < PriorityItemsBlob > ( ) ;
var itemsArray = builder . Allocate ( ref root . Items , childCount ) ;
2025-11-25 08:19:33 -05:00
var childIndex = ( ushort ) ( taskComponent . Index + 1 ) ;
for ( ushort j = 0 ; j < childCount ; + + j ) {
2026-05-10 11:47:55 -04:00
itemsArray [ j ] = new PriorityItemBlobEntry ( ) { TaskIndex = childIndex , PriorityValueIndex = ushort . MaxValue } ;
2025-11-25 08:19:33 -05:00
for ( ushort k = 0 ; k < priorityValueComponents . Length ; + + k ) {
2026-05-10 11:47:55 -04:00
if ( priorityValueComponents [ k ] . Index = = childIndex ) {
itemsArray [ j ] . PriorityValueIndex = k ;
2025-11-25 08:19:33 -05:00
break ;
}
}
childIndex = taskComponents [ childIndex ] . SiblingIndex ;
}
2026-05-10 11:47:55 -04:00
prioritySelectorComponent . PriorityItems = builder . CreateBlobAssetReference < PriorityItemsBlob > ( Allocator . Persistent ) ;
builder . Dispose ( ) ;
}
2025-11-25 08:19:33 -05:00
2026-05-10 11:47:55 -04:00
// Build sorted order by current priority values (reuse storage).
ref var priorityItems = ref prioritySelectorComponent . PriorityItems . Value . Items ;
var orderCount = priorityItems . Length ;
if ( ! prioritySelectorComponent . SortedOrder . IsCreated | | prioritySelectorComponent . SortedOrder . Length ! = orderCount ) {
if ( prioritySelectorComponent . SortedOrder . IsCreated ) {
prioritySelectorComponent . SortedOrder . Dispose ( ) ;
}
prioritySelectorComponent . SortedOrder = new UnsafeList < ushort > ( orderCount , Allocator . Persistent ) ;
prioritySelectorComponent . SortedOrder . Resize ( orderCount ) ;
2025-11-25 08:19:33 -05:00
}
2026-05-10 11:47:55 -04:00
for ( ushort j = 0 ; j < orderCount ; + + j ) {
prioritySelectorComponent . SortedOrder [ j ] = j ;
}
2025-11-25 08:19:33 -05:00
2026-05-10 11:47:55 -04:00
for ( int a = 0 ; a < orderCount ; + + a ) {
for ( int b = a + 1 ; b < orderCount ; + + b ) {
ref var entryA = ref priorityItems [ prioritySelectorComponent . SortedOrder [ a ] ] ;
ref var entryB = ref priorityItems [ prioritySelectorComponent . SortedOrder [ b ] ] ;
var valueA = GetPriorityValue ( ref entryA , ref priorityValueComponents ) ;
var valueB = GetPriorityValue ( ref entryB , ref priorityValueComponents ) ;
if ( valueB > valueA ) {
var t = prioritySelectorComponent . SortedOrder [ a ] ;
prioritySelectorComponent . SortedOrder [ a ] = prioritySelectorComponent . SortedOrder [ b ] ;
prioritySelectorComponent . SortedOrder [ b ] = t ;
}
2025-11-25 08:19:33 -05:00
}
2026-05-10 11:47:55 -04:00
}
2025-11-25 08:19:33 -05:00
2026-05-10 11:47:55 -04:00
for ( int j = 0 ; j < orderCount ; + + j ) {
var relIdx = prioritySelectorComponent . SortedOrder [ j ] ;
prioritySelectorComponent . SortedOrder [ j ] = priorityItems [ relIdx ] . TaskIndex ;
2025-11-25 08:19:33 -05:00
}
prioritySelectorComponent . ActiveRelativeChildIndex = 0 ;
2026-05-10 11:47:55 -04:00
prioritySelectorComponents [ i ] = prioritySelectorComponent ;
branchComponent . NextIndex = prioritySelectorComponent . SortedOrder [ prioritySelectorComponent . ActiveRelativeChildIndex ] ;
branchComponents [ taskComponent . BranchIndex ] = branchComponent ;
2025-11-25 08:19:33 -05:00
// Start the child.
var nextChildTaskComponent = taskComponents [ branchComponent . NextIndex ] ;
nextChildTaskComponent . Status = TaskStatus . Queued ;
2026-05-10 11:47:55 -04:00
taskComponents [ branchComponent . NextIndex ] = nextChildTaskComponent ;
2025-11-25 08:19:33 -05:00
}
2026-05-10 11:47:55 -04:00
// The prioritySelector task is currently active. Check the active child.
var childTaskComponent = taskComponents [ prioritySelectorComponent . SortedOrder [ prioritySelectorComponent . ActiveRelativeChildIndex ] ] ;
2025-11-25 08:19:33 -05:00
if ( childTaskComponent . Status = = TaskStatus . Queued | | childTaskComponent . Status = = TaskStatus . Running ) {
// The child should keep running.
continue ;
}
// Switch to the next highest priority. If no more priority values exist the task should act as a normal selector.
2026-05-10 11:47:55 -04:00
if ( prioritySelectorComponent . ActiveRelativeChildIndex = = prioritySelectorComponent . SortedOrder . Length - 1 | |
2025-11-25 08:19:33 -05:00
childTaskComponent . Status = = TaskStatus . Success ) {
// There are no more children or the child succeeded. The selector task should end.
taskComponent . Status = childTaskComponent . Status ;
prioritySelectorComponent . ActiveRelativeChildIndex = 0 ;
2026-05-10 11:47:55 -04:00
taskComponents [ prioritySelectorComponent . Index ] = taskComponent ;
2025-11-25 08:19:33 -05:00
branchComponent . NextIndex = taskComponent . ParentIndex ;
2026-05-10 11:47:55 -04:00
branchComponents [ taskComponent . BranchIndex ] = branchComponent ;
2025-11-25 08:19:33 -05:00
} else {
2026-05-10 11:47:55 -04:00
// The child task returned failure. Move onto the next task.
2025-11-25 08:19:33 -05:00
prioritySelectorComponent . ActiveRelativeChildIndex + + ;
2026-05-10 11:47:55 -04:00
var nextIndex = prioritySelectorComponent . SortedOrder [ prioritySelectorComponent . ActiveRelativeChildIndex ] ;
2025-11-25 08:19:33 -05:00
var nextTaskComponent = taskComponents [ nextIndex ] ;
nextTaskComponent . Status = TaskStatus . Queued ;
2026-05-10 11:47:55 -04:00
taskComponents [ nextIndex ] = nextTaskComponent ;
2025-11-25 08:19:33 -05:00
branchComponent . NextIndex = nextIndex ;
2026-05-10 11:47:55 -04:00
branchComponents [ taskComponent . BranchIndex ] = branchComponent ;
2025-11-25 08:19:33 -05:00
}
2026-05-10 11:47:55 -04:00
prioritySelectorComponents [ i ] = prioritySelectorComponent ;
2025-11-25 08:19:33 -05:00
}
}
2026-05-10 11:47:55 -04:00
}
2025-11-25 08:19:33 -05:00
2026-05-10 11:47:55 -04:00
/// <summary>
/// Job which executes the special case where the PrioritySelector has no PriorityValueComponent buffer.
/// </summary>
[BurstCompile]
private partial struct PrioritySelectorWithoutValuesJob : IJobEntity
{
/// <summary>
/// Executes the no-priority-values fallback logic.
/// </summary>
/// <param name="prioritySelectorComponents">An array of PrioritySelectorComponents.</param>
/// <param name="taskComponents">An array of TaskComponents.</param>
/// <param name="branchComponents">An array of BranchComponents.</param>
[BurstCompile]
public void Execute ( ref DynamicBuffer < PrioritySelectorComponent > prioritySelectorComponents , ref DynamicBuffer < TaskComponent > taskComponents , ref DynamicBuffer < BranchComponent > branchComponents )
{
for ( int i = 0 ; i < prioritySelectorComponents . Length ; + + i ) {
var prioritySelectorComponent = prioritySelectorComponents [ i ] ;
var taskComponent = taskComponents [ prioritySelectorComponent . Index ] ;
// If there are no values then the selector should return failure.
if ( taskComponent . Status = = TaskStatus . Queued & & ! prioritySelectorComponent . PriorityItems . IsCreated ) {
taskComponent . Status = TaskStatus . Failure ;
taskComponents [ prioritySelectorComponent . Index ] = taskComponent ;
var branchComponent = branchComponents [ taskComponent . BranchIndex ] ;
branchComponent . NextIndex = taskComponent . ParentIndex ;
branchComponents [ taskComponent . BranchIndex ] = branchComponent ;
2025-11-25 08:19:33 -05:00
}
}
}
}
/// <summary>
2026-05-10 11:47:55 -04:00
/// Returns the priority value for a blob entry, or float.MinValue if no component.
/// </summary>
/// <param name="entry">The priority item blob entry.</param>
/// <param name="priorityValueComponents">The priority value components buffer.</param>
/// <returns>The priority value.</returns>
[BurstCompile]
private static float GetPriorityValue ( ref PriorityItemBlobEntry entry , ref DynamicBuffer < PriorityValueComponent > priorityValueComponents )
{
if ( entry . PriorityValueIndex = = ushort . MaxValue ) {
return float . MinValue ;
}
return priorityValueComponents [ entry . PriorityValueIndex ] . Value ;
}
/// <summary>
/// Disposes blob assets when the system is destroyed.
2025-11-25 08:19:33 -05:00
/// </summary>
/// <param name="state">The current state of the system.</param>
private void OnDestroy ( ref SystemState state )
{
foreach ( var prioritySelectorComponents in SystemAPI . Query < DynamicBuffer < PrioritySelectorComponent > > ( ) ) {
for ( int i = 0 ; i < prioritySelectorComponents . Length ; + + i ) {
2026-05-10 11:47:55 -04:00
var prioritySelectorComponent = prioritySelectorComponents [ i ] ;
if ( prioritySelectorComponent . PriorityItems . IsCreated ) {
prioritySelectorComponent . PriorityItems . Dispose ( ) ;
}
if ( prioritySelectorComponent . SortedOrder . IsCreated ) {
prioritySelectorComponent . SortedOrder . Dispose ( ) ;
}
2025-11-25 08:19:33 -05:00
}
}
}
}
}
#endif