# Parallel

**Parallel nodes** allow multiple child nodes to be executed simultaneously, enabling characters to perform parallel tasks or monitor multiple conditions at the same time. Unlike sequence or selector nodes, which execute child nodes one after the other, parallel nodes run all their child nodes concurrently or according to specific rules.

#### Key Aspects of Parallel Nodes:

1. **Simultaneous Execution**:
   * Parallel nodes can run several child tasks at the same time. For instance, an NPC can attack an enemy while also monitoring its health or surroundings to make appropriate decisions.
2. **Managing Child Nodes**:
   * Parallel nodes can contain multiple tasks that continue to run independently while the parallel node is active. For example, one node may check the environment while another controls the execution of an attack.
3. **Use for Background Processes**:
   * Parallel nodes are often used to run background tasks or constantly monitor conditions that can affect the NPC’s behavior. For instance, one node might continuously check for enemies in sight while another executes patrol or attack behaviors.
4. **Asynchronous Execution**:
   * Parallel nodes allow for asynchronous task execution, which is crucial for creating complex and dynamic behaviors. An NPC can process multiple sources of information at the same time, making its behavior more realistic. _An asynchronous operation is simulated, in fact, parallelization does not mean that tasks will be performed in different C# tasks or threads._

#### Example of Using a Parallel Node:

Imagine an NPC tasked with attacking an enemy while also monitoring its health. A parallel node could contain two tasks:

* **Attack task**: The NPC continues to attack the enemy until the attack is complete.
* **Health monitoring task**: The NPC constantly checks its health and may decide to retreat or heal if its health drops below a critical threshold.

The parallel node can be configured to finish based on the success of either task. For example, if the NPC's health drops to a critical level, it might immediately stop attacking and begin healing.

#### Use Cases for Parallel Nodes:

* **Complex NPC Behavior**: NPCs can simultaneously track multiple targets or conditions, making their behavior more realistic and dynamic.
* **Background Checks**: Constant monitoring of environmental conditions, health levels, or other important factors without interrupting primary actions.
* **Multitasking**: NPCs can perform several actions simultaneously, such as fighting and coordinating with allies, enhancing their tactical behavior.

#### Pros and Cons of Parallel Nodes:

* **Pros**:
  * Enable NPCs to multitask, making their behavior more natural and adaptive.
  * Useful for complex scenarios where multiple conditions must be checked at the same time.
* **Cons**:
  * Can complicate debugging and development, as multiple tasks need to be managed simultaneously.
  * Poorly configured parallel nodes might lead to unexpected behavior if some tasks complete earlier than others.

Overall, parallel nodes are a powerful tool for creating complex, adaptive, and dynamic NPC behavior in games, allowing them to perform multiple tasks or check several conditions at once.

{% content-ref url="parallel/parallel" %}
[parallel](parallel/parallel)
{% endcontent-ref %}

{% content-ref url="parallel/parallel-selector" %}
[parallel-selector](parallel/parallel-selector)
{% endcontent-ref %}

{% content-ref url="parallel/parallel-complete" %}
[parallel-complete](parallel/parallel-complete)
{% endcontent-ref %}
