News Forums RAIN Code Snips Decorator to force a BT node to return a specific value

This topic contains 0 replies, has 1 voice, and was last updated by  prime 1 year, 5 months ago.

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #36264

    prime
    Keymaster

    This is a custom decision that can be used to force the return value of a single node or a set of nodes (as a sequencer) in your behavior tree.

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using RAIN.Action;
    using RAIN.Core;
    using RAIN.Representation;
    [RAINDecision("Force Result Decorator")]
    public class ForceResultDecorator : RAINDecision
    {
        public Expression ReturnValue = new Expression();
        private int _lastRunning = 0;
        public override void Start(RAIN.Core.AI ai)
        {
            base.Start(ai);
            _lastRunning = 0;
        }
        public override ActionResult Execute(RAIN.Core.AI ai)
        {
            ActionResult tResult = ActionResult.SUCCESS;
            for (; _lastRunning < _children.Count; _lastRunning++)
            {
                tResult = _children[_lastRunning].Run(ai);
                if (tResult != ActionResult.SUCCESS)
                    break;
            }
            return GetResult(ReturnValue, ai);
        }
        public ActionResult GetResult(Expression aExpression, AI ai)
        {
            if ((aExpression == null) || !aExpression.IsValid)
                return ActionResult.FAILURE;
            if (aExpression.IsVariable && (aExpression.VariableName.ToLower() == "success"))
                return ActionResult.SUCCESS;
            else if (aExpression.IsVariable && (aExpression.VariableName.ToLower() == "running"))
                return ActionResult.RUNNING;
            else if (aExpression.IsVariable && (aExpression.VariableName.ToLower() == "failure"))
                return ActionResult.FAILURE;
            string tReturnValue = aExpression.Evaluate<string>(ai.DeltaTime, ai.WorkingMemory);
            if (string.IsNullOrEmpty(tReturnValue))
                return ActionResult.FAILURE;
            tReturnValue = tReturnValue.ToLower();
            if (tReturnValue == "success")
                return ActionResult.SUCCESS;
            else if (tReturnValue == "running")
                return ActionResult.RUNNING;
            else
                return ActionResult.FAILURE;
        }
    }
Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.