Playmaker Forum
PlayMaker Help & Tips => PlayMaker Help => Topic started 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:
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.)
-
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...
-
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?
-
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.
-
: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!