playMaker

Author Topic: Read Float List  (Read 1270 times)

wyliam

  • Playmaker Newbie
  • *
  • Posts: 22
    • View Profile
Read Float List
« on: September 23, 2011, 06:17:46 PM »
Based off of giyomu's ReadBool, I created an action that will take any number of FsmFloat values from one FSM and overwrite FsmFloat values on another FSM on the same gameObject.

It's great that we got Global variables, but more often than not, I need them accessible from any other FSM on the same game object. I usually have an FSM "Thinking" and another "Acting" on those thoughts. Originally I made one for a single Float, but didn't want to do that many times over!

Please note, the action will copy the value to value of the same index in the second array. Wont work if you have different array lengths.

Cheers! (not sure why the formatting looks wonky..)

Code: [Select]

using UnityEngine;
using HutongGames.PlayMaker;

[ActionCategory(ActionCategory.StateMachine)]
[Tooltip("Read a list of floats from one FSM and overwrite floats from another FSM on the same gameObject.")]
public class ReadFloatList : FsmStateAction
{
[RequiredField]
public FsmOwnerDefault gameObject;

[UIHint(UIHint.FsmName)]
[Tooltip("Name of FSM on Game Object to COPY floats from.")]
public FsmString fromFSM;

    [RequiredField]
    [Tooltip("List of floats to COPY from.")]
    [UIHint(UIHint.FsmFloat)]
    public FsmString[] fromFsmFloats;

    [UIHint(UIHint.FsmName)]
    [Tooltip("Name of FSM on Game Object to OVERWRITE floats.")]
    public FsmString toFSM;

    [RequiredField]
    [Tooltip("List of floats to OVERWRITE.")]
    [UIHint(UIHint.FsmFloat)]
    public FsmString[] toFsmFloats;
   
public bool everyFrame;
    public bool debug;

GameObject goLastFrame;
PlayMakerFSM fsm1;
    PlayMakerFSM fsm2;

// cache
GameObject go;

public override void Reset ()
{
gameObject = null;
fromFSM = "";
        toFSM = "";
        fromFsmFloats = null;
        toFsmFloats = null;
        debug = false;
}

public override void OnEnter ()
{
// get owner reference and cache it here
go = Fsm.GetOwnerDefaultTarget(gameObject);

        DoReadFloat();

if(!everyFrame) Finish();
}
    public override void OnUpdate()
    {
        DoReadFloat();
    }
   
void DoReadFloat()
{
        if (fromFsmFloats.Length == 0 || go == null) return;

// only get the fsm component if go has changed
if (go != goLastFrame)
{
goLastFrame = go;
fsm1 = ActionHelpers.GetGameObjectFsm(go, fromFSM.Value);
            fsm2 = ActionHelpers.GetGameObjectFsm(go, toFSM.Value);
}

        if (fsm1 == null || fsm2 == null) return;

        // Overwrite the second float with the first
        for (int i = 0; i < fromFsmFloats.Length; i++)
        {
            FsmFloat f1 = fsm1.FsmVariables.GetFsmFloat(fromFsmFloats[i].Value);
            FsmFloat f2 = fsm2.FsmVariables.GetFsmFloat(toFsmFloats[i].Value);

            f2.Value = f1.Value;

            if (debug)
                Debug.LogWarning(
                    "READFLOATS: FSM " + fromFSM + ":" + fromFsmFloats[i] + " (" + f1.Value + ") copied to FSM " + toFSM + ":" + toFsmFloats[i]);
        }
}
}