playMaker

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

Kein

  • Playmaker Newbie
  • *
  • Posts: 17
So I'm iterating though GO and i want to get/print all FSM variables of a specific gameobject runtime, is it possible?

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Hi,
You can use 'Get Fsm....' Actions to get the variables

Here is a video explaining Get Fsm Actions :


Kein

  • Playmaker Newbie
  • *
  • Posts: 17
Thanks but I mean through API during runtime, no Editor (obviously I could just check it there what is set to happen), just through default script attached to dummy object in the scene I want to get list of all variables on specific GO and iterate through each of it to retrieve value
« Last Edit: July 06, 2017, 08:39:35 AM by Kein »

tcmeric

  • Beta Group
  • Hero Member
  • *
  • Posts: 768
To clarify, you want to print all the variables of a FSM to a string or series of strings to display in the scene during run time?

If not a string, where do you want to save the values?

Kein

  • Playmaker Newbie
  • *
  • Posts: 17
To clarify, you want to print all the variables of a FSM to a string or series of strings to display in the scene during run time?

If not a string, where do you want to save the values?

Basically I'm doing this right now:

Code: [Select]
NamedVariable[] allNamedVariables = g3.GetComponent<PlayMakerFSM>().FsmVariables.GetAllNamedVariables();
for (int k = 0; k < allNamedVariables.Length; k++)
{
Debug.Log(allNamedVariables[k].ToString());
}

Ir returns me VALUES of some variables but not all of them, I need all children too.  Well, components, not children.

This does not return much useful info:

Code: [Select]
foreach (PlayMakerFSM c in GameObject.Find("myObjectMa,e").GetComponents<PlayMakerFSM>())
{
Debug.Log(string.Concat(new object[]
{
"name ",
c.name,
" type ",
c.GetType(),
" basetype ",
c.GetType().BaseType
}));
foreach (FieldInfo fi in c.GetType().GetFields())
{
object obj = c;
Debug.Log(string.Concat(new object[]
{
"fi name ",
fi.Name,
" val ",
fi.GetValue(obj)
}));
}
}
« Last Edit: July 06, 2017, 10:16:47 AM by Kein »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Hi,
Ah you want to get them in a script :)

I think you need to use GetComponents and place them (the multiple fsm components) in an array or list
then do the GetAllNamedVariables for each one in the array.

or get each fsm manually and set them in a variable using this :

Code: [Select]
public static PlayMakerFSM FindFsmOnGameObject(GameObject go, string fsmName)
I can't test right now, i will try tomorrow if this works (if not sorry for the wrong advice)

Kein

  • Playmaker Newbie
  • *
  • Posts: 17
Hey, no worries djaydino. I figured it out, just forgot to update post.

I have a different question, not necessarily PM related but in case someone is bored I'd appreciate a hint.

So, right now I iterate through all GameObjects in the scene to find objects that on specific layer. Then I again iterate through these narrowed objects to find the ones that have component "VeryImportantComponent" so I can get the value of "VeryImportantVar" from that component.

Is there an existing PM method that just gets me list of all gameObjects with specific named component?

And how do I even condition NamedVariable if I cant use normal operators like ==/!= ?
« Last Edit: July 06, 2017, 11:24:24 AM by Kein »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Hi,
is 'VeryImportantComponent' an fsm Component?

if so, after you got the GameObjects by layer, you can use :

Code: [Select]
PlayMakerFSM[] componentList = FoundGameobjectByLayer.GetComponentsInChildren<PlayMakerFSM>().ToArray();
then you need to iterate and use this :
Code: [Select]
var  currentFsm = componentList [i]
var fmsName = currentFsm
if (fsmName == "FSM")
     {
       //   get VeryImportantVar
     }

if it is not a fsm component change PlayMakerFSM to VeryImportantComponent.

I don't know if it is the best or fastest way, but this should work.

Kein

  • Playmaker Newbie
  • *
  • Posts: 17
Yeah, thanks.

Can I get list of Events for specific FMS?

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
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);
        }

Kein

  • Playmaker Newbie
  • *
  • Posts: 17
Thanks for all the tips. I'm currently using this method but it is quite slow. You do't have to answer this but if you have an idea off the top of your head how to do it faster I'd appreciate it:

Code: [Select]
GameObject[] array = UnityEngine.Object.FindObjectsOfType(typeof(GameObject)) as GameObject[];
for (int i = 0; i < array.Length; i++)
{
if (FSMUtility.LocateFSM(GameObject.Find(array[i].name), "VeryImportantFSM") != null)
{
this.fmsPool.Add(array[i].name.ToString());
}
}

Cheers!

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
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


Kein

  • Playmaker Newbie
  • *
  • Posts: 17
Thanks this may come in handy.

Quote
Are these objects created @ runtime or preset?
As of right now all stuff is runtime

Kein

  • Playmaker Newbie
  • *
  • Posts: 17
So to make sure there is no way to directly fetch FSM without iterating? Even the built-in helper func does that?

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Hi,
Have you tried my suggestions with the action i provided?