playMaker

Author Topic: FsmGameObjectCompare.cs  (Read 1659 times)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 12351
  • Official Playmaker Support
    • View Profile
FsmGameObjectCompare.cs
« on: April 20, 2011, 01:07:44 AM »
Hi,

 Ok, following FsmBoolTest ( http://hutonggames.com/playmakerforum/index.php?topic=74.0 ),

 Here is FsmGameObjectCompare test.

 Based on GameObjectCompare https://hutonggames.fogbugz.com/default.asp?W488

Same thing but now you can select remote gameobject from other fsms.

It features a flag to prevent triggering a true comparison if both variable are null. While it is mathematically correct, this is unlikely what you want in practice. This easily solved like so and prevent more states and actions to check that both variable are null in case Equal is triggered.


Quote
// (c) Copyright HutongGames, LLC 2010-2011. All rights reserved.

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
   [ActionCategory(ActionCategory.Logic)]
   [Tooltip("Compares 2 Game Objects from different fsms and sends Events based on the result.")]
   public class FsmGameObjectCompare : FsmStateAction
   {
      
      [RequiredField]
      public FsmOwnerDefault gameObject;
      [UIHint(UIHint.FsmName)]
      [Tooltip("Optional name of FSM on Game Object")]
      public FsmString fsmName;
      [RequiredField]
      [UIHint(UIHint.FsmGameObject)]
      public FsmString variableName;
      
      [RequiredField]
      public FsmOwnerDefault compareToGameObject;
      [UIHint(UIHint.FsmName)]
      [Tooltip("Optional name of FSM on Game Object")]
      public FsmString compareToFsmName;
      [RequiredField]
      [UIHint(UIHint.FsmGameObject)]
      public FsmString compareToVariableName;
      
      [Tooltip("If both variable are null, do trigger NOT EQUAL.")]
      public bool nullTriggerNotEqual;
      public FsmEvent equalEvent;
      public FsmEvent notEqualEvent;
      [UIHint(UIHint.Variable)]
      public FsmBool storeResult;
      public bool everyFrame;

      public override void Reset()
      {
         equalEvent = null;
         notEqualEvent = null;
         storeResult = null;
         nullTriggerNotEqual = true;
         everyFrame = false;
      }

      public override void OnEnter()
      {
         DoGameObjectCompare();
         
         if (!everyFrame)
            Finish();
      }

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

      void DoGameObjectCompare()
      {
         
         if (gameObject == null || compareToGameObject == null)  return;
         
         if ( fsmName == null || compareToFsmName == null)  return;
         
         if ( variableName == null || compareToVariableName == null)  return;
         
         GameObject go = Fsm.GetOwnerDefaultTarget(gameObject);
         if (go == null) return;
         
         GameObject compareToGo = Fsm.GetOwnerDefaultTarget(compareToGameObject);
         if (compareToGo == null) return;         
         
         // get the first value
         PlayMakerFSM fsm = ActionHelpers.GetGameObjectFsm(go, fsmName.Value);
         
         if (fsm == null) return;
         
         FsmGameObject fsmGameObject = fsm.FsmVariables.GetFsmGameObject(variableName.Value);
         
         if (fsmGameObject == null) return;
         
         // get the compareTo value
         PlayMakerFSM compareToFsm = ActionHelpers.GetGameObjectFsm(compareToGo, compareToFsmName.Value);
         
         if (fsm == null) return;
         
         FsmGameObject compareToFsmGameObject = compareToFsm.FsmVariables.GetFsmGameObject(compareToVariableName.Value);
         
         if (compareToFsmGameObject == null) return;         
      
         
         bool equal = fsmGameObject.Value == compareToFsmGameObject.Value;
         if (nullTriggerNotEqual && equal){
            if (fsmGameObject.Value== null || compareToFsmGameObject.Value==null){
               equal = false;
            }
         }
         
         if (storeResult != null)
            storeResult.Value = equal;
         
         if (equal && equalEvent != null)
            Fsm.Event(equalEvent);
         else if (notEqualEvent != null)
            Fsm.Event(notEqualEvent);
         
      }
      
   }
}

MaDDoX

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 165
    • View Profile
    • FluidPlay Studios
Re: FsmGameObjectCompare.cs
« Reply #1 on: April 20, 2011, 01:52:18 PM »
Hey very nice addition, this will come in handy for sure in my projects since instead of the finicky prefabs I mostly use object references (prototypes) in a "main" gameflow FSM. Thanks Jean! ;)
--
Breno "MaDDoX" Azevedo
@brenoazevedo

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 12351
  • Official Playmaker Support
    • View Profile
Re: FsmGameObjectCompare.cs
« Reply #2 on: April 20, 2011, 09:50:23 PM »
Hi,

 Your welcome :) I hope your having as much fun working with playmaker then I do.

 I saw tho that a fixed regards the original compare action is coming with the next version, I don't know therefore if my action also has it as well, since it uses the same lines for the comparison. So something to watch out I guess.

 Bye,

 Jean