Skip to content

Action Tasks

Action Tasks are tasks that the agent has to execute if the attached decision has been selected. They are executed either in sequence or in parallel, depending on the execution mode of the action list.

Execution Modes

After the agent finds out the best decision, it will execute the action list either in sequence or in parallel, depending on your choice. Currently, there are two execution modes for the action list:

  • Sequence
    • The actions will be run sequentially.
    • If an action finishes in success, the agent will run the next action, and the action list will finish in success if the last action finishes in success.
    • If an action finishes in failure, the action list will finish in failure.
  • Parallel
    • The actions will be run simultaneously.
    • The action list will finish in success if all actions are finished in success.
    • If any action finishes in failure, other actions will be aborted and the action list will finish in failure.
  • ParallelComplete
    • The actions will be run simultaneously.
    • If any action finishes in success or failure, other actions will be aborted and the action list will return the child status immediately.

You can choose the execution mode you want by selecting it from this drop down menu:
Attachments/UtilityIntelligence/Documentation/UtilityAgent/ActionTasks/actions-execution-mode.png

MaxRepeatCount

It is the number of times to repeat the action list.

Note

  • The action list only repeat if it is finished in success.
  • If MaxRepeatCount <= 0 it will be repeated forever

You can change MaxRepeatCount of the action list here:

Attachments/UtilityIntelligence/Documentation/UtilityAgent/ActionTasks/max-repeat-count.png

Keep Running Until Finished

In case you want to prevent the current agent from making a new decision while the action list is running, you can check the option: Keep Running Until Finished in the Action List Editor.

Tip

  • By enabling this option for important decisions, such as AttackEnemy, ChargeHealth, and ReloadAmmunition, it stops the agent from getting distracted by other non-important decisions. This helps reduce the oscillation between these important decisions and other non-important ones.
  • For example, with AttackEnemy decision, you should enable this option because the agent needs to finish the attack before switching to another decision, such as RunAwayFromEnemy.

Note

  • If you enable this option, the agent can only change its decision after the action list is finished, regardless of whether the scores of other decisions are higher than the current one.
  • For example, with AttackEnemy decision, the agent can only switch to another decision after each attack is finished, even if the scores of other decisions such as RunAwayFromEnemy or ReloadAmmunition are higher than AttackEnemy.
  • Additionally, if the score of the AttackEnemy decision remains the highest after each attack, the agent will keep running this decision.

To enable/disable Keep Running Until Finished option, you need to check/uncheck it in the Action List Editor:

Attachments/UtilityIntelligence/Documentation/UtilityAgent/ActionTasks/keep-running-until-finished.png

Creating Action Tasks

  1. To create a new action task, you need to create a new class inherited from ActionTask:

    public class Wait : ActionTask
    {
        private float elapsedTime;
        public VariableReference<float> WaitTime = 1.0f;
    
        protected override void OnStart()
        {
            elapsedTime = 0;
        }
    
        protected override UpdateStatus OnUpdate(float deltaTime)
        {
            elapsedTime += deltaTime;
    
            if (elapsedTime > WaitTime) return UpdateStatus.Success;
            return UpdateStatus.Running;
        }
    }
    

  2. To assign the action task to a decision, you need to go the the Action List Editor in Intelligence Tab, select the action type, then click the Create button:
    Attachments/UtilityIntelligence/Documentation/UtilityAgent/Decisions/assign-action-task.png

Action Task Statuses

At runtime, action tasks have 4 statuses:

 : Running


 : Success

 : Failed


 : Aborted

Built-in Action Tasks

Currently, Utility Intelligence provides these built-in action tasks:

  • Idle
  • Log
  • Wait
  • RandomWait
  • MoveTowards
  • NavmeshMoveTowards
  • DestroySelf

Properties and Functions

Properties

Here are some useful properties that you can use in your custom tasks:

Transform Transform { get; private set; }  

GameObject GameObject { get; private set; }  

UtilityAgentController AgentController { get; private set; }

Functions

GetComponent Functions

You can get any component attached to the GameObject by calling these functions:

T GetComponent<T>()
T GetComponentInChildren<T>()

Coroutine functions

We provides these functions to help you start/stop coroutines from action tasks:

void StartCoroutine(string methodName);

Coroutine StartCoroutine(IEnumerator routine);

Coroutine StartCoroutine(string methodName, object value);

void StopCoroutine(string methodName);

void StopCoroutine(IEnumerator routine);

void StopAllCoroutines();

Overridable Functions

Here is the list of functions you could override to make your actions works as you want:

  • Lifecycle Functions:

    void OnAwake();
    
    void OnStart();
    
    Status OnUpdate();
    
    void OnLateUpdate();
    
    void OnFixedUpdate();
    
    //OnAbort is called when the action's target changes or when the agent makes a new decision
    void OnAbort();
    
    //OnEnd is called after the action returns a success or failure
    void OnEnd();
    

  • Collision/Trigger 3D:

    void OnCollisionEnter(Collision collision);
    
    void OnCollisionStay(Collision collision);
    
    void OnCollisionExit(Collision collision);
    
    void OnTriggerEnter(Collider other);
    
    void OnTriggerStay(Collider other);
    
    void OnTriggerExit(Collider other);
    
    void OnControllerColliderHit(ControllerColliderHit hit);
    

  • Collision/Trigger 2D:

    void OnCollisionEnter2D(Collision2D collision);
    
    void OnCollisionStay2D(Collision2D collision);
    
    void OnCollisionExit2D(Collision2D collision);
    
    void OnTriggerEnter2D(Collider2D other);
    
    void OnTriggerStay2D(Collider2D other);
    
    void OnTriggerExit2D(Collider2D other);
    

  • Animation:

    void OnAnimatorMove();
    
    void OnAnimatorIK(int layerIndex);
    


If you find this plugin more valuable than its price, especially when it's on sale, or if you simply want to support me on my development journey, there are currently 3 ways to do so:

1. Leave a 5-star review on the Unity Asset Store.
2. Buy assets through my Affiliate Links. It won't cost you any extra money, and I'll get a commission from Unity.
    • Utility Intelligence: A user-friendly Utility AI Framework
    • Top New Assets
    • Top Paid Assets
    • Top Downloaded Assets
3. Buy me a coffee at Ko-fi. Donation Benefits:
    • Your requests will be prioritized in my todo-list.
    • You'll receive a special role on my Discord server exclusively for donors:
       • Supporter: One-time donation.
       • Patron: Monthly donation.

Thank you so much for your support! 🥰
It allows me to dedicate more time to developing this plugin.


Last update : May 13, 2024
Created : March 31, 2024