# EvaluateDecorator

EvaluateDecorator can be written in a C# script.

## How to create

1. Click the “+” button in the Project window.
2. Select Logic Toolkit > Scripts > Decorator > Evaluate Decorator C# Script from the menu.
3. Enter the script name and confirm with Enter.

## How to write a script

- Create a class that inherits from `LogicToolkit.BehaviorTree.EvaluateDecorator`.
- Apply `System.SerializableAttribute` to the type.
- Implement `protected override bool OnEvaluate()` and write the processing at runtime.
- `protected override void OnActivated()` is called when activated.
- `protected override void OnDeactivated()` is called when it becomes inactive.
- `protected override void OnEnterEvaluation()` is called when it becomes the evaluation target.
  If Lower Priority is set in the Abort Flags of the Decorator set as a child node of the Selector, even if a node with a lower priority (younger node) is running, it will be subject to evaluation.
- `protected override void OnLeaveEvaluation()` is called when it is no longer subject to evaluation.

## Code example

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using LogicToolkit;
    using LogicToolkit.BehaviorTrees;

    [System.Serializable]
    public class EvaluateDecoratorExample : EvaluateDecorator
    {
        private float startTime = float.MaxValue;

        // OnActivated is called when activated.
        protected override void OnActivated()
        {
            startTime = Time.time;
        }

        // OnEnterEvaluation is called when entering the evaluation target
        protected override void OnEnterEvaluation()
        {
        }

        // OnEvaluate is called when evaluating the condition
        protected override bool OnEvaluate()
        {
            return Time.time - startTime <= 1f;
        }

        // OnLeaveEvaluation is called when leaving the evaluation target
        protected override void OnLeaveEvaluation()
        {
        }

        // OnDeactivated is called when it is deactivated.
        protected override void OnDeactivated()
        {
        }
    }

In this example, setting Abort Flags to Self will cause the node to abort after 1 second of activation.

In addition, a script called TimeLimit is included in advance as a similar function.