playMaker

Author Topic: Get FSM by Name API Feature  (Read 2098 times)

NotoriousGIB

  • Playmaker Newbie
  • *
  • Posts: 4
    • View Profile
    • Mann Made Games, LLC
Get FSM by Name API Feature
« on: August 22, 2013, 11:44:03 AM »
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.

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3173
  • Official Playmaker Support
    • View Profile
    • LinkedIn
Re: Get FSM by Name API Feature
« Reply #1 on: August 22, 2013, 02: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...

NotoriousGIB

  • Playmaker Newbie
  • *
  • Posts: 4
    • View Profile
    • Mann Made Games, LLC
Re: Get FSM by Name API Feature
« Reply #2 on: August 23, 2013, 07: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

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3173
  • Official Playmaker Support
    • View Profile
    • LinkedIn
Re: Get FSM by Name API Feature
« Reply #3 on: August 23, 2013, 08: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...

NotoriousGIB

  • Playmaker Newbie
  • *
  • Posts: 4
    • View Profile
    • Mann Made Games, LLC
Re: Get FSM by Name API Feature
« Reply #4 on: August 23, 2013, 08: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.

Kein

  • Playmaker Newbie
  • *
  • Posts: 17
    • View Profile
Re: Get FSM by Name API Feature
« Reply #5 on: June 30, 2017, 04:27:10 AM »
Hey, gonna bump this thread, I have a question: how to I locate all available FSMs on an Object1 through API?


jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 11918
  • Official Playmaker Support
    • View Profile
Re: Get FSM by Name API Feature
« Reply #6 on: June 30, 2017, 05:59:02 AM »
Hi,

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

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

Bye,

 Jean

Kein

  • Playmaker Newbie
  • *
  • Posts: 17
    • View Profile
Re: Get FSM by Name API Feature
« Reply #7 on: June 30, 2017, 08: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?
« Last Edit: June 30, 2017, 08:41:09 AM by Kein »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 11918
  • Official Playmaker Support
    • View Profile
Re: Get FSM by Name API Feature
« Reply #8 on: August 09, 2017, 12: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