playMaker

Author Topic: Printing all FSMvariables and their value of a specific GameObject runtime  (Read 1282 times)

Kein

  • Playmaker Newbie
  • *
  • Posts: 17
    • View Profile

then you need to iterate and use this :
Code: [Select]
var  currentFsm = componentList [i]
var fmsName = currentFsm
if (fsmName == "FSM")
     {
       //   get VeryImportantVar
     }

Yes, this is what you said^

Like, even if I know that MyObject has PlaymakerFSM component named "temp copmponent" attached I still have to iterate to get it. Here is example from your built-in util:

Code: [Select]
public static PlayMakerFSM LocateFSM(GameObject go, string fsmName)
{
if (go != null)
{
PlayMakerFSM[] components = go.GetComponents<PlayMakerFSM>();
if (components != null)
{
for (int i = 0; i < components.Length; i++)
{
if (components[i].FsmName == fsmName)
{
return components[i];
}
}
}
}
return null;
}


As I mentioned before I iterate through list of objects after scene loaded, I already sort them out by layer to narrow  results but even if I know that these specific gameObject will have specific FSM in 99% of cases I still need to iterate through all FSM-components and find the one matching by name instead of directly requesting it.
« Last Edit: July 27, 2017, 12:11:08 PM by Kein »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 3656
    • View Profile
    • jinxtergames
Hi,
I mean from this post :

Quote
Hi,
No problem :)

FindObjectsOfType is 'expensive'

Maybe you could get the fsms into an array within play maker?
and then get the array

Are these objects created @ runtime or preset?

If preset you can make an array and set the array type to 'Object' and Object Type to 'PlaymakerFSM' then you can drag the "VeryImportantFSM" into the array.

If from prefab or clone, then you can use the action i have added in the attachment. to get the fsm.
It will show the gameobject name it is in, but it has the correct fsm (if you use the correct name of course)

I have tested it a bit, but not much yet so it might not work perfectly, i will test some more later and add it to the Ecosystem

So you would only need to get that array, something like this :

Code: [Select]
PlayMakerFSM GameObjectFsm = GameObject.GetComponent<PlayMakerFSM>();
        int arrayLenght = GameObjectFsm .FsmVariables.GetFsmArray("arrayname").Length;
        for (int z = 0; z < arrayLenght; z++)
        {
            PlayMakerFSM  go = GameObjectFsm .FsmVariables.GetFsmArray("ArrayName").Get(z) as PlayMakerFSM;

          //  do your thing to the fsm
        }

it is best to place that array on a gameobject with a single fsm. but if not possible.

Then you can something like this to get to the array :
Code: [Select]
PlayMakerFSM[] playMakerFsms = gameObject.GetComponents<PlayMakerFSM>();
        for (int z = 0; z < playMakerFsms.Length; z++)
        {
            string currentFsmName = playMakerFsms[z].name;
            if (currentFsmName == "theNeededFsm")
            {
                PlayMakerFSM playMakerFsm = playMakerFsms[z];
            }
        }

or this way :
Code: [Select]
public static PlayMakerFSM GetPlayMakerFsmByName(this GameObject gameObject, string fsmName)
    {
        PlayMakerFSM[] playMakerFsms = gameObject.GetComponents<PlayMakerFSM>();

        return playMakerFsms.FirstOrDefault(playMakerFsm => playMakerFsm.FsmName == fsmName);
    }
I found this on this website

Kein

  • Playmaker Newbie
  • *
  • Posts: 17
    • View Profile
Thanks djaydino, but I should have specified - getting FSMs into array is no issue to me, the fact I need to iterate through them and thus affect performance is some way - is. I was hoping there was a direct way to get specific FSM knowing its name. Oh well, not a big deal.

I have another question

Hi.
Yes you can :

Code: [Select]
FsmEvent[] myFsm = RegionFsm.FsmEvents.ToArray(); //for test
        for (int i = 0; i < fsmName.Length; i++)
        {
            var fsmEventName = myFsm [i].Name;
            Debug.Log(fsmEventName);
        }

As I can see I'm getting list of all events? Is there a way to narrow per FSM component?
like
Code: [Select]
PlaymakerFSM tempFMS = FSMUtility.LocateFSM(gameObject, "fms_name");
FSMevent[] events = something.GetallEventOnFSM(tempFMS);
foreach [...]
etc
?

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 3656
    • View Profile
    • jinxtergames
Hi.
Code: [Select]
FsmEvent[] events = something.FsmEvents.ToArray();
Btw you  can find some api references here :)

Kein

  • Playmaker Newbie
  • *
  • Posts: 17
    • View Profile
Is there a bug in .GetAllNamedVariables() extension method? It returns me ALL named variables on all FSMs of an object, even tho I specific specific FSM.

Kein

  • Playmaker Newbie
  • *
  • Posts: 17
    • View Profile
Today I learned that when I iterate through NamedVar I need to do nullref check for EACH specific aspect of apparently...

Code: [Select]
NamedVariable[] allNamedVariables = curFSM.FsmVariables.GetAllNamedVariables();
if (allNamedVariables != null && allNamedVariables.Length != 0)
{
Debug.LogError("NameVars array aint null");
for (int v = 0; v < allNamedVariables.Length; v++)
{
if (allNamedVariables[v].Name != null && allNamedVariables[v] != null && allNamedVariables[v].VariableType != null)
{
Debug.LogError("Joke nullcheck passed lol");
VariableType variableType = allNamedVariables[v].VariableType;
File.AppendAllText(curFSM.FsmName + "_vars.txt", string.Concat(new object[]
{
"[name]: ",
allNamedVariables[v].Name,
" [value]: ",
allNamedVariables[v].ToString(),
" [type]: ",
allNamedVariables[v].VariableType.ToString(),
Environment.NewLine
}));
}
}
}