Playmaker Forum

PlayMaker Help & Tips => PlayMaker Help => Topic started by: speccy666 on January 20, 2016, 02:51:16 PM

Title: Targetting a Specific FSM from script when having MANY Fsm on one object??
Post by: speccy666 on January 20, 2016, 02:51:16 PM
Hi, I suppose that I know how to target and disable an Fsm.... 

Code: [Select]
  var ObjetFSM : PlayMakerFSM;
        ObjetFSM = gameObject.Find("Wall").GetComponent.<PlayMakerFSM>();
         ObjetFSM.enabled=false;
But my project contains an object (named "WALL") with  3 Fsm on it (Fsm1,Fsm2,Fsm3):

From the script, I want to fire an event to launch a state with a global transition which is "OpenDoor"... From script, I want to launch a global Transition situated on FSM3.

It works when I use the  command below : ObjetFSM.BroadcastEvent("OpenDoor");

BUT it does NOT work when i try :         ObjetFSM.Fsm.Event("OpenDoor");

I suppose this mean I do not target the right Fsm.... but I cannot manage to do such a  :
ObjetFSM.Fsm.FSM3.Event("OpenDoor");

Any clues please?

Thanx. Jean Marc
Title: Re: Targetting a Specific FSM from script when having MANY Fsm on one object??
Post by: Alex Chouls on January 20, 2016, 03:04:21 PM
You can use GetComponents instead of GetComponent to get all the PlayMakerFSMs on the GameObject, then loop through the components to find the one with the right name. I'll try to post example code in a bit...

EDIT: Actually there is a static method in PlayMakerFSM to make this easier:

Code: [Select]
public static PlayMakerFSM FindFsmOnGameObject(GameObject go, string fsmName)
So you can just call:

Code: [Select]
var fsm = PlayMakerFSM.FindFsmOnGameObject(gameObject, "FSM3");
Title: Re: Targetting a Specific FSM from script when having MANY Fsm on one object??
Post by: speccy666 on January 21, 2016, 10:37:37 AM
Ok Thanks! very interesting so I tried this but I cannot manage to use it correctly.... :'(

I try to reach the FSM named "FSM" in the "DOOR" object :

Not working : 
Code: [Select]
var fsm = PlayMakerFSM.FindFsmOnGameObject("Door", "FSM");It says : Not compatible with argument "String, String"...seems to be easy to understand so I then tried :
Code: [Select]
static var OneObject : GameObject;
OneObject="porte2";
var fsm = PlayMakerFSM.FindFsmOnGameObject(OneObject, "FSM");

It says : "Cannot convert 'String' to unity engine....

What should I exactly write to get it work.... ??? :-[

Thanks

Title: Re: Targetting a Specific FSM from script when having MANY Fsm on one object??
Post by: speccy666 on January 21, 2016, 10:57:58 AM
It seems I found a solution just to fall on another problem :
SO I discovered this which seems at first glance to work :
Code: [Select]
            fsm=PlayMakerFSM.FindFsmOnGameObject(GameObject.Find("porte2"), "FSM");
            fsm.enabled=false;
            fsm=PlayMakerFSM.FindFsmOnGameObject(GameObject.Find("porte2"), "FSM2");           //
            fsm.enabled=false;
            fsm=PlayMakerFSM.FindFsmOnGameObject(GameObject.Find("porte3"), "FSM");
            fsm.enabled=false;
            fsm=PlayMakerFSM.FindFsmOnGameObject(GameObject.Find("porte3"), "FSM2");           //
            fsm.enabled=false;

But I got  an a warning message and my FSM are not DISABLED.
The warning is :
NullReferenceException: Object reference not set to an instance of an object
Manager.Update () (at Assets/Manager.js:37)
Title: Re: Targetting a Specific FSM from script when having MANY Fsm on one object??
Post by: Alex Chouls on January 21, 2016, 02:04:08 PM
Is Manager.js your script?

The method signature tells you what parameter types to use:
Code: [Select]
public static PlayMakerFSM FindFsmOnGameObject(GameObject go, string fsmName)
So the first parameter is a GameObject reference. GameObject.Find returns a GameObject reference, or you could use public GameObject variables that you assign at edit time.

If FindFsmOnGameObject cannot find the fsm it returns null, and then fsm.enabled=false would give you a NullReferenceException.

You need to track down why the return is null, e.g., the GameObject name is wrong, or the FSM name is wrong.

Note, GameObject.Find also only returns active GameObjects:
http://docs.unity3d.com/ScriptReference/GameObject.Find.html

Maybe one of the GameObjects is not active...? You basically have to debug the code and figure out exactly where it is failing...