playMaker

Author Topic: FsmBoolTest.cs  (Read 9823 times)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15563
  • Official Playmaker Support
FsmBoolTest.cs
« on: April 04, 2011, 08:28:29 AM »
Hi Everyone,

 Posting the custom action that I use the most currently as I build flow charts involving several fsm and central fsm for variable pooling. It saves me several creation steps every time and more importantly prevent having to store duplicate variables all over fsm.

 If you have suggestions, critics or better code for that, it's all welcome!

Hope you'll find it useful :)

 Bye,

 Jean


Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2011. All rights reserved.
// Modified by Jean Fabre : contact@fabrejean.net
// this is a combination of BoolTest 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("Sends Events based on the value of a Bool Variable from another FSM.")]
public class FsmBoolTest : 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;
[RequiredField]
[UIHint(UIHint.Variable)]

public FsmEvent isTrue;
public FsmEvent isFalse;

public bool everyFrame;

private bool storedValue;

GameObject goLastFrame;
PlayMakerFSM fsm;


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


storedValue = false;
isTrue = null;
isFalse = null;
}

public override void OnEnter()
{
DoGetFsmBool();

Fsm.Event(storedValue ? isTrue : isFalse);

if (!everyFrame)
Finish();
}

public override void OnUpdate()
{
DoGetFsmBool();

Fsm.Event(storedValue ? isTrue : 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;

storedValue = fsmBool.Value;
}

}
}

tobbeo

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 186
Re: FsmBoolTest.cs
« Reply #1 on: April 04, 2011, 06:55:20 PM »
Maybe posted under the wrong section but thank you :)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15563
  • Official Playmaker Support
Re: FsmBoolTest.cs
« Reply #2 on: April 05, 2011, 01:03:44 AM »
Hi,

 Where should I put it? the "playmaker updates and downloads" sections seems to be dedicated to official actions. I would think this is a request/answer in one post  ;)

Bye,

 Jean

tobbeo

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 186
Re: FsmBoolTest.cs
« Reply #3 on: April 05, 2011, 06:57:26 PM »
Hi,

 Where should I put it? the "playmaker updates and downloads" sections seems to be dedicated to official actions. I would think this is a request/answer in one post  ;)

Bye,

 Jean

That's where I was thinking it should be with all the new action (not just ones made by Hungtong), though I'll let the admins decide! :)

MaDDoX

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 165
    • FluidPlay Studios
Re: FsmBoolTest.cs
« Reply #4 on: April 06, 2011, 07:45:24 PM »
Very nice idea, it can be easily expanded to other types. The only possible drawback that I can imagine is if the variable check is performed on a per-update basis (every frame), then it might be costlier than simply checking a local variable which was pulled during the initialization of the FSM. In any case, if the variable is updated elsewhere and you have to check it "live" there won't be any better option, performance will be all up to the ActionHelpers.GetGameObjectFsm speed.

Having a central FSM as a variable database is exactly the approach I'm using btw, I just try really hard to make per-update tests/processes in local variables and only update the "global" variable after the condition is met. Even if the performance gain is negligible, I find it more elegant for just a bit more effort - and it "breaks down" the debugging process, making it easier.

Where should I put it? the "playmaker updates and downloads" sections seems to be dedicated to official actions.
Judging from previous posts from Alex I'm sure that's the right section, it just wasn't available before. I've posted one action there btw, no complaints all greetings :)
--
Breno "MaDDoX" Azevedo
@brenoazevedo

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15563
  • Official Playmaker Support
Re: FsmBoolTest.cs
« Reply #5 on: April 07, 2011, 01:02:53 AM »
Hi,

Yes, this is indeed targeted as single check, not to be used every frame. In my case, speed is not critical, I am looking for a convenient way to define process, and having a single fsm acting as a broker for variables really speed up the whole development and provides me with a ultra flexible system.

Will move now the code to that other section.

 Bye,

 Jean

600

  • Moderator
  • Hero Member
  • *****
  • Posts: 720
    • 600
Re: FsmBoolTest.cs
« Reply #6 on: November 25, 2015, 09:41:16 AM »
Hello,

thanks for the action, came here several times to copy/paste and decided to put it on the Ecosystem. Followed the official video to use Snipt, not sure why it won't show up it search results.

Link: https://snipt.net/600/

Please advise :)

600

  • Moderator
  • Hero Member
  • *****
  • Posts: 720
    • 600
Re: FsmBoolTest.cs
« Reply #7 on: November 25, 2015, 09:57:13 AM »
Ok nevermind, it is there now :)

Another thing, an empty tag appearing - how to fix it?

Thanks

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15563
  • Official Playmaker Support
Re: FsmBoolTest.cs
« Reply #8 on: December 02, 2015, 01:12:56 AM »
Hi,

 This is great! I am really pleased you all contribute to the Ecosystem!

 your empty tag is becaase you have filled up the tags properly.


so you need to have "ActionCatogory.Logic" in the tags and then, the Ecosystem will properly set the category tag in the item within browser itself.

Bye,

 Jean

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15563
  • Official Playmaker Support
Re: FsmBoolTest.cs
« Reply #9 on: December 02, 2015, 01:13:40 AM »
Hi,

me again, so make that change, and let me know, I'll tweet about it, or you could tweet and I'll retweet :)

 Bye,

 Jean

600

  • Moderator
  • Hero Member
  • *****
  • Posts: 720
    • 600
Re: FsmBoolTest.cs
« Reply #10 on: December 02, 2015, 05:47:26 AM »
Hi, yes I had Tags set up, just noticed that after I Save and Close, tags disappear and are not visible after Edit again. What could be the trick?


P.S. I don't have Twitter account, feel free to post about it :)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15563
  • Official Playmaker Support
Re: FsmBoolTest.cs
« Reply #11 on: December 04, 2015, 12:52:50 AM »
Hi,

 You have to come back to the file, as Snipt has a bug that when you just saved, it doesn't show the tags.

I still can't see your tags, so indeed I don't think they got saved. Are you respecting the format ( I think it's comma separated right?)

Bye,

 Jean

600

  • Moderator
  • Hero Member
  • *****
  • Posts: 720
    • 600
Re: FsmBoolTest.cs
« Reply #12 on: February 02, 2016, 11:57:02 AM »
Category fixed, finally... there was just a typo  ;D

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15563
  • Official Playmaker Support
Re: FsmBoolTest.cs
« Reply #13 on: February 04, 2016, 02:03:36 AM »
Great :)

Thanks for keeping track of all of this, I know it's hard.

Bye,

 Jean