playMaker

Author Topic: FsmBoolChanged.cs  (Read 2096 times)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14387
  • Official Playmaker Support
    • View Profile
FsmBoolChanged.cs
« on: July 15, 2011, 07:09:29 AM »
Hi Everyone,

 Just a simple addition to allow for watching a boolean change from another fsm. Very handy to avoid storing duplicate information across fsm.

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2011. All rights reserved.
// Modified by Jean Fabre : contact@fabrejean.net
// this is a combination of BoolChanged and GetFsmBool since I don't want the extras steps required otherwise.
// I also don't feel like saving a variable for every single check from other fsm, I want to avoid redundance sometimes.
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Logic)]
[Tooltip("Tests if the value of a Bool variable from another FSM has changed. Use this to send an event on change")]
public class FsmBoolChanged : FsmStateAction
{
[RequiredField]
public FsmOwnerDefault gameObject;
[UIHint(UIHint.FsmName)]
[Tooltip("Optional name of FSM on Game Object")]
public FsmString fsmName;
[RequiredField]
[UIHint(UIHint.FsmBool)]
public FsmString variableName;

public FsmEvent isTrue;
public FsmEvent isFalse;

bool currentValue;
bool previousValue;

GameObject goLastFrame;
PlayMakerFSM fsm;


public override void Reset()
{
gameObject = null;
fsmName = "";

isTrue = null;
isFalse = null;

}

public override void OnEnter()
{
DoGetFsmBool();

previousValue = currentValue;

}

public override void OnUpdate()
{
DoGetFsmBool();



if (currentValue != previousValue)
{
if (currentValue){
Fsm.Event(isTrue);
}else{
Fsm.Event(isFalse);
}
}
}

void DoGetFsmBool()
{
GameObject go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null) return;

// only get the fsm component if go has changed

if (go != goLastFrame)
{
goLastFrame = go;
fsm = ActionHelpers.GetGameObjectFsm(go, fsmName.Value);
}

if (fsm == null) return;

FsmBool fsmBool = fsm.FsmVariables.GetFsmBool(variableName.Value);

if (fsmBool == null) return;

currentValue = fsmBool.Value;
}


}
}

Bye,

 Jean

tobbeo

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 186
    • View Profile
Re: FsmBoolChanged.cs
« Reply #1 on: July 15, 2011, 04:33:22 PM »
Very cool! I'll be using this a lot, thanks for sharing!