playMaker

Author Topic: Bool Multi Condition (update)  (Read 698 times)

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 772
Bool Multi Condition (update)
« on: January 03, 2022, 02:22:13 PM »
I can't remember if that's a default or ecosystem action but its every frame option doesn't properly, it will quit the action no matter what if there's a passed test on the conditions, even if no event is declared, so it's impossible to keep it running to test for all other negatives outcomes outside of the single positive one.
I did a little modification and even added a boolean for the result, just in case.

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2015. All rights reserved.
/*--- __ECO__ __PLAYMAKER__ __ACTION__ ---*/

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory(ActionCategory.Logic)]
    [Tooltip("Adds multipe float variables to float variable.")]
    public class BoolMultiCondition : FsmStateAction
    {
        [UIHint(UIHint.Variable)]
        [Tooltip("The variables to evaluate.")]
        public FsmBool[] booleans;

[Tooltip("The passing conditions for the above variable list. Must be the same count as Booleans.")]
public FsmBool[] conditions;

        [Tooltip("The result of the comparison. Returns False is at least one comparison failed.")]
        public FsmBool result;

[Tooltip("If the evaluated booleans DO match the conditions, this event can fire.")]
public FsmEvent passEvent;

[Tooltip("If the evaluated booleans do NOT match the conditions, this event can fire.")]
public FsmEvent failEvent;

[Tooltip("Repeat every frame while the state is active.")]
public bool everyFrame;

[Tooltip("Reports clear information about failure events.")]
public bool debug;

        public override void Reset()
        {
booleans = null;
conditions = null;
passEvent = null;
failEvent = null;
            everyFrame = false;
debug = false;
        }

        public override void OnEnter()
        {
            DoEvaluate();

            if (!everyFrame)
            {
                Finish();
            }
        }

        public override void OnUpdate()
        {
            DoEvaluate();
        }

        void DoEvaluate()
        {
if (conditions.Length != booleans.Length)
{
Debug.LogError ("<color=red>BoolMultiCondition Action Fatal Error:</color> The number of <i>Booleans</i> and <i>Conditions</i> must be equal!");
Finish ();
return;
}

bool[] list = new bool[booleans.Length];

for (var i = 0; i < booleans.Length; i++)
            {
list[i] = (booleans[i].Value == conditions[i].Value);
                result.Value = list[i];
if (!list[i])
{
if (debug)
{
Debug.Log ("Failure at bool <b>Element "+i+": (" +booleans[i].Name+ ")</b> against its pass condition of <b>"+conditions[i]+".</b>");
}

Fsm.Event(failEvent);

if (!everyFrame)
{
Finish ();
}

return;
}
            }

if (debug)
{
Debug.Log ("All " +list.Length + " boolean conditions passed.");
}

Fsm.Event(passEvent);
            if (!everyFrame)
            {
                Finish ();
            }
        }
    }
}