Playmaker Forum

PlayMaker Help & Tips => PlayMaker Help => Topic started by: wrocum on April 07, 2012, 11:42:40 PM

Title: Send event or variable from script to FSM with a specific name. [SOLVED]
Post by: wrocum on April 07, 2012, 11:42:40 PM
Hello! I need help in javascript. I found a similar example on the forum but unfortunately on C# and I don't know how to rewrite it.

So the question is:
How to send event or variable value from script to FSM with a specific name? I know how to send event from script to object with one FSM. Here is a working code:

Code: [Select]
var CubeFSM : PlayMakerFSM;
CubeFSM = gameObject.Find("Cube").GetComponent.<PlayMakerFSM>();

function Update () {
if (Input.GetKey ("m"))
   {
   CubeFSM.Fsm.Event("Moving");
   }
if (Input.GetKey ("s"))
   {
   CubeFSM.Fsm.Event("Stop");
   }
}

But what if I need to send event to several FSM's with a specific names? For example in the same "Cube" object I have FSM's named "Jump'' "Move" "Rotate"? How to address to each one individually?

Sorry for my english.)
Title: Re: Send event or variable from script to FSM with a specific name.
Post by: Alex Chouls on April 08, 2012, 12:06:34 AM
Use GetComponents to get all PlayMakerFSM components on an object:
http://unity3d.com/support/documentation/ScriptReference/GameObject.GetComponents.html

Then iterate through all PlayMakerFSMs and send the event.

The javascript example in the Unity docs should help...
Title: Re: Send event or variable from script to FSM with a specific name.
Post by: wrocum on April 08, 2012, 12:34:48 AM
Thank you Alex for your quick response. Sorry but I am quite new to programming and don't know what to do with that code in your link.( Could you be more specific? How to connect it with that code that I already have?
Title: Re: Send event or variable from script to FSM with a specific name.
Post by: kru on April 08, 2012, 01:29:30 AM
var CubeFSM : PlayMakerFSM;
CubeFSM = gameObject.Find("Cube").GetComponent.<PlayMakerFSM>();

becomes

var CubeFSMs : PlayMakerFSM[];
CubeFSMs = gameObject.Find("Cube").GetComponents.<PlayMakerFSM>();


Then your update function becomes

for (var fsm : PlayMakerFSM in CubeFSMs)
{
 if (Input.GetKey ("m"))
   {
   fsm.Fsm.Event("Moving");
   }
 if (Input.GetKey ("s"))
   {
   fsm.Fsm.Event("Stop");
   }
}

This is iterating over a collection of objects, as was suggested.
Title: Re: Send event or variable from script to FSM with a specific name.
Post by: wrocum on April 08, 2012, 05:31:13 AM
 :D Thank you very very much kru!  I am very grateful for your help! Now everything works as it needed. All the best and good luck to you!