playMaker

Author Topic: EnableChildFsm  (Read 2350 times)

giyomu

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 108
    • View Profile
    • blog
EnableChildFsm
« on: July 14, 2011, 10:24:47 PM »
Hi all , maybe some will find a use for this,

this work like EnableFsm ( there is no reset in my case, can add that if some want ), but perform on
all fsm contained in a GameObject root, useful if you have such kind of set and want activate or not Fsm in there.

Code: [Select]
using UnityEngine;
using HutongGames.PlayMaker;

[ActionCategory(ActionCategory.StateMachine)]
[Tooltip("Same as enable Fsm , but operate on all child contained in a root game object, root object is exclude by default, be aware that check are in order, so if you get a childName and put a Tag also only childName will be checked.\n" +
          "If you let all field blank , all Fsm in this hierarchy will be affected")]
public class EnableChildFsm : FsmStateAction
{
[RequiredField]
public FsmOwnerDefault gameObject;
[Tooltip("Will be taken by default if not null, leave blank if not used")]
public FsmString childName;
[Tooltip("Will be choosen if you do not have childName value filled, leave blank if not used")]
[UIHint(UIHint.Tag)]
public FsmString withTag;
[Tooltip("Use in combination with childName or Tag, or use alone on all Fsm found in the hierarchy, leave blank if not used")]
public FsmString fsmName;
public FsmBool enable;

// cache
GameObject rootObject;

public override void Reset ()
{
gameObject = null;
childName = "";
withTag = "Untagged";
fsmName = "";
enable = true;

rootObject = null;
}
public override void OnEnter ()
{
DoEnableChildFsm();
Finish();
}

void DoEnableChildFsm()
{
rootObject = Fsm.GetOwnerDefaultTarget(gameObject);
if (rootObject== null)
return;

// first find all fsm components in our child
var fsmComponents = rootObject.transform.GetComponentsInChildren<PlayMakerFSM>();

// define how we perform our check on child
if(!string.IsNullOrEmpty(childName.Value))
CheckByName(fsmComponents, fsmName.Value);
else if(withTag.Value != "Untagged")
CheckByTag(fsmComponents, fsmName.Value);
else if(string.IsNullOrEmpty(childName.Value) && withTag.Value == "Untagged" && !string.IsNullOrEmpty(fsmName.Value))
        CheckByFsmName(fsmComponents, fsmName.Value);
else
{
// if all check fail , we perform on all Fsm found
foreach(PlayMakerFSM listFsm in fsmComponents)
{
if(listFsm.gameObject != rootObject)
listFsm.enabled = enable.Value;
}
}

// release
fsmComponents = null;
}

// Check by child name and filter by fsm name if provided
void CheckByName(PlayMakerFSM[] foundFsm, string filterByFsmName)
{
if(!string.IsNullOrEmpty(filterByFsmName))
    {
foreach(PlayMakerFSM listFsm in foundFsm)
{
if(listFsm.gameObject != rootObject && listFsm.gameObject.name == childName.Value && listFsm.FsmName == filterByFsmName)
listFsm.enabled = enable.Value;
}
}
else
{
foreach(PlayMakerFSM listFsm in foundFsm)
{
if(listFsm.gameObject != rootObject && listFsm.gameObject.name == childName.Value)
listFsm.enabled = enable.Value;
}
}
}

// check by tag and filter by fsm name if provided
void CheckByTag(PlayMakerFSM[] foundFsm, string filterByFsmName)
{
if(!string.IsNullOrEmpty(filterByFsmName))
    {
foreach(PlayMakerFSM listFsm in foundFsm)
{
if(listFsm.gameObject != rootObject && listFsm.tag == withTag.Value && listFsm.FsmName == filterByFsmName)
listFsm.enabled = enable.Value;
}
}
else
{
foreach(PlayMakerFSM listFsm in foundFsm)
{
if(listFsm.gameObject != rootObject && listFsm.tag== withTag.Value)
listFsm.enabled = enable.Value;
}
}
}

// check on all fsm in child and filter by name
void CheckByFsmName(PlayMakerFSM[] foundFsm, string filterFsmName)
{
foreach(PlayMakerFSM listFsm in foundFsm)
{
if(listFsm.gameObject != rootObject && listFsm.FsmName == filterFsmName)
listFsm.enabled = enable.Value;
}
}
}

MaDDoX

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 165
    • View Profile
    • FluidPlay Studios
Re: EnableChildFsm
« Reply #1 on: July 15, 2011, 05:31:22 AM »
This can actually come quite handy Giyomu, thanks :)
--
Breno "MaDDoX" Azevedo
@brenoazevedo