Playmaker Forum

PlayMaker Feedback => Feature Requests => Topic started by: NotoriousGIB on August 22, 2013, 02:44:03 PM

Title: Get FSM by Name API Feature
Post by: NotoriousGIB on August 22, 2013, 02:44:03 PM
I have many instances where I need to find a single FSM out of many attached to a GameObject in a script.  As of now, I have to loop through an array of FSMs and locate one by Name/Tag/etc.  I would like a simple FSM.GetByName (or something) method added to the API.  This would save a lot of overhead and looping.
Thank you.
Title: Re: Get FSM by Name API Feature
Post by: Alex Chouls on August 22, 2013, 05:15:54 PM
Are you doing this at runtime or in the editor?

In the editor you can use:
Code: [Select]
HutongGames.PlayMakerEditor.FsmEditorUtility.FindFsmOnGameObject(GameObject go, string FsmName)
At runtime there is a list of active FSMs you can use instead of GetComponents:
PlayMakerFsm.FsmList

So you could do this:
Code: [Select]
    public static PlayMakerFSM FindFsmOnGameObject(GameObject go, string fsmName)
    {
        foreach (var fsm in PlayMakerFSM.FsmList)
        {
            if (fsm.gameObject == go && fsm.FsmName == fsmName)
            {
                return fsm;
            }
        }
        return null;
    }

I'll add this to API, but it probably won't make it into the next update since that release is imminent...
Title: Re: Get FSM by Name API Feature
Post by: NotoriousGIB on August 23, 2013, 10:30:04 AM
I am trying to refer to a specific FSM on a dynamic GameObject at run time.  With your second solution, I would still be looping through a list of FSMs.  I would much rather be able to refer to the specific FSM directly.
Thanks
Title: Re: Get FSM by Name API Feature
Post by: Alex Chouls on August 23, 2013, 11:06:28 AM
The script could find the FSM once and cache the reference. But you'll need to loop through the PlayMakerFSM components one way or another to find the one with the right name. Either using GetComponents (which is slow) or using PlayMakerFSM.FsmList (which should be faster, unless you have a LOT of FSMs in the scene). But either way, if you do it once in Start (or whenever you need it) the perf should be fine...

Alternatively, you could put a script on the same GameObject as the FSM with a public PlayMakerFSM field and reference the FSM directly. Other scripts could get your component and then get the FSM reference... that might be quicker... but you'd still want to cache the result so you don't keep calling GetComponent.

Internally PlayMaker could keep some dictionaries to lookup PlayMakerFSMs on a GameObject or to lookup by name, but I'm not convinced the perf gain would be worth it... I'll try to do some testing...
Title: Re: Get FSM by Name API Feature
Post by: NotoriousGIB on August 23, 2013, 11:45:08 AM
Thanks for the response.  I'll look in to how I am accessing the FSMs and see if there is a more efficient option.

Right now I am creating objects (from Prefabs) and then getting a reference to their FSMs (from different scripts), so I can't cache the reference on Start.  I am caching the reference once the object is created, but I am still looping through a list of FSMs every time I create an object.  I'm really not sure if it's effecting performance, but I was hoping for a more efficient way.  I will definitely try the FSMList instead of the GetComponents array.

If it looks to be too much trouble, please don't worry about it. I will probably have this game finished before your update would be released.
Title: Re: Get FSM by Name API Feature
Post by: Kein on June 30, 2017, 07:27:10 AM
Hey, gonna bump this thread, I have a question: how to I locate all available FSMs on an Object1 through API?

Title: Re: Get FSM by Name API Feature
Post by: jeanfabre on June 30, 2017, 08:59:02 AM
Hi,

 you use Unity Go.GetComponents<PlayMakerFsm>();

https://docs.unity3d.com/ScriptReference/Component.GetComponents.html

Bye,

 Jean
Title: Re: Get FSM by Name API Feature
Post by: Kein on June 30, 2017, 11:06:33 AM
Code: [Select]
PlayMakerFSM[] each = GameObject.Find("MyStuff").GetComponents<PlayMakerFSM>();
foreach (PlayMakerFSM playMakerFSM in each)
{
Debug.Log("Test: " + each);
}

Returns empty, am I missing something?
Title: Re: Get FSM by Name API Feature
Post by: jeanfabre on August 09, 2017, 03:38:06 AM
Hi,

 well, it looks like "MyStuff" GameObject doesn't have any PlayMakerFSM if you have an empty list.

 OR, if you do that inside an action the log goes in the PlayMaker log not the Unity log ( by default, you have preferences to handle this).

 Bye,

 Jean