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;
}
}