playMaker

Author Topic: making custom actions: when to use a bool and when to use an event?  (Read 740 times)

wetcircuit

  • Full Member
  • ***
  • Posts: 158
    • wetcircuit.com
I'm just starting to make custom actions for some of my 3rd-party assets. Sometimes when surfacing an internal state, I'm not sure whether to make a Playmaker action that stores a bool (reflecting the state change) or an event (triggered by the state change).

Are there rules-of-thumb to tell me which is better, or when to pick one over the other?

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7621
    • jinxtergames
Hi.
You can use both, many cases you would want to check the bool but also many cases you would want to trigger an event.

Here is a sample script which i use on a scriptable object :

Code: [Select]
// (c) Copyright JinxterGames, LLC 2010-2022. All rights reserved.
// Made by djaydino -- http://www.jinxtergames.com/ --

using UnityEngine;
using JinxterGames.Darklight;
using JinxterGames.Darklight.ScriptableObjects;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory("Scriptable Objects/Stats")]
    [Tooltip("")]
    public class So_StatsGetLocationsSeen : FsmStateAction
    {
        [RequiredField]
        public StatsDefaultNew statsDefaultNew;

        [RequiredField]
        [UIHint(UIHint.FsmString)]
        public FsmString reference;

        [ActionSection("Result")]

        [UIHint(UIHint.Variable)]
        public FsmBool seen;

        [ActionSection("Events")]

        [Tooltip("Event to send if the Bool variable is True.")]
        public FsmEvent isTrue;

        [Tooltip("Event to send if the Bool variable is False.")]
        public FsmEvent isFalse;

        private int index;

        public bool everyFrame;


        public override void Reset()
        {
            statsDefaultNew = null;
            seen = new FsmBool { UseVariable = true };

            isTrue = null;
            isFalse = null;
        }

        public override void OnEnter()
        {
            index = ((int)statsDefaultNew.locationsSeenName.FindIndex(a => a.Equals(reference.Value)));

            DoCheck();

            if (!everyFrame) Finish();
        }

        void DoCheck()
        {
            if (index > -1)
            {
                if (!seen.IsNone) seen.Value = statsDefaultNew.locationsSeen[index];
                Fsm.Event(statsDefaultNew.locationsSeen[index] ? isTrue : isFalse);
            }
            else
            {
#if UNITY_EDITOR
                Debug.LogError("Reference NOT FOUND : " + reference.Value + " / Fsm Name : " + Fsm.Name + " / State : " + Fsm.ActiveStateName, Fsm.Owner);
#endif
            }
        }
    }
}

these are the important parts :

Code: [Select]
        public override void Reset()
        {
            seen = new FsmBool { UseVariable = true };
        }

        void DoCheck()
        {
            if (!seen.IsNone) seen.Value = statsDefaultNew.locationsSeen[index];
            Fsm.Event(statsDefaultNew.locationsSeen[index] ? isTrue : isFalse);
        }

the "UseVariable = true" will set the variable to none by default.

Christoph

  • Beta Group
  • Sr. Member
  • *
  • Posts: 254
I would say you use a bool when you have further use for that condition within the FSM or somewhere else. If it's just important in that very moment I would use events.