playMaker

Author Topic: Send event or variable from script to FSM with a specific name. [SOLVED]  (Read 14727 times)

wrocum

  • Playmaker Newbie
  • *
  • Posts: 3
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.)
« Last Edit: April 08, 2012, 05:32:03 AM by wrocum »

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3987
  • Official Playmaker Support
    • LinkedIn
Re: Send event or variable from script to FSM with a specific name.
« Reply #1 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...

wrocum

  • Playmaker Newbie
  • *
  • Posts: 3
Re: Send event or variable from script to FSM with a specific name.
« Reply #2 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?
« Last Edit: April 08, 2012, 12:37:55 AM by wrocum »

kru

  • Playmaker Newbie
  • *
  • Posts: 3
Re: Send event or variable from script to FSM with a specific name.
« Reply #3 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.

wrocum

  • Playmaker Newbie
  • *
  • Posts: 3
Re: Send event or variable from script to FSM with a specific name.
« Reply #4 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!