playMaker

Author Topic: Send event to Playmaker FSM trough Mecanim Behaviour  (Read 9830 times)

C.J.Geringer

  • Playmaker Newbie
  • *
  • Posts: 48
Send event to Playmaker FSM trough Mecanim Behaviour
« on: March 26, 2015, 08:49:21 PM »
Now that unity allows one to attach C# scripts as behaviours in mecanim clips, I am trying to have mecanim behaviours send an event by name to state machines.

Currently the Mecanim Behaviour sends the event name to a monobehaviour attached to the object and then that monobehaviour send the event to the FSM machine.

I want to eliminate this intermediate script.

What is the best way to have two public string variables in the mecanim (Event Name and FSM name), and have the mecanim behaviour send the event to the specified FSM? (the FSM is in the same gameobject controlled by the mecanim animator)

C.J.Geringer

  • Playmaker Newbie
  • *
  • Posts: 48
Re: Send event to Playmaker FSM trough Mecanim Behaviour
« Reply #1 on: March 31, 2015, 10:24:55 PM »
I am currently trying the following code:

Code: [Select]
using UnityEngine;
using System.Collections;
using HutongGames.PlayMaker;

public class MecPMSendEvent : StateMachineBehaviour {


public string TargetFSMName;
public string EventName;
private PlayMakerFSM  TargetFSM;

// OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {

PlayMakerFSM[]temp= GetComponents<PlayMakerFSM>();
foreach(PlayMakerFSM fsm in temp){
if (fsm.FsmName==TargetFSMName){
TargetFSM=fsm;
break;
}


}
}


// OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
//override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
//
//}

// OnStateExit is called when a transition ends and the state machine finishes evaluating this state
override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {

TargetFSM.Fsm.Event(EventName);


}

// OnStateMove is called right after Animator.OnAnimatorMove(). Code that processes and affects root motion should be implemented here
//override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
//
//}

// OnStateIK is called right after Animator.OnAnimatorIK(). Code that sets up animation IK (inverse kinematics) should be implemented here.
//override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
//
//}
}

but I get an erro that says "the name 'GetComponents' does not exist in the current context.

Would someone be able to tell me what I am doing wrong?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Send event to Playmaker FSM trough Mecanim Behaviour
« Reply #2 on: April 01, 2015, 03:35:45 AM »
Hi,

 The mecanim package on the wiki has a new component that synchs Mecanim states with PlayMaker states, I suggest you use this as it's automated and works for any states or transition you want without any work on your side then just match the fsm states names with mecanim states.

otherwise, if you want something custom, I suggest you use the PlayMaker Utils package, in there you'll find convenient public variables wrapping PlayMaker Fsm Event Target and PlayMaker Event setup for the user. It's direct and troublefree on your side.

Check out the latest UI package on the wiki, and how the various proxies are done, it's very simple to implement.

https://github.com/jeanfabre/PlayMaker--Unity--UI/blob/master/Assets/PlayMaker%20uGui/Proxies/PlayMakerUGuiDragEventsProxy.cs


 Bye,

 Jean

C.J.Geringer

  • Playmaker Newbie
  • *
  • Posts: 48
Re: Send event to Playmaker FSM trough Mecanim Behaviour
« Reply #3 on: April 02, 2015, 07:26:16 PM »
Thank you for your answer.

is there a way to set which state machine the script sincronizes to? the examples I have found only have one FSM per object. And the component seems to only allow me to select which object it syncrhonizes, but not which FSM in a given Object.

Seems like the "Getcomponents' method only exixsts in Monobehaviour, and since mecanim´s behavious do not extend from mono behaviour, they do not have that capability.

I need at least to be able to synchronize different animator layers with diferent FSMs in a same object.
« Last Edit: April 02, 2015, 07:29:34 PM by C.J.Geringer »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Send event to Playmaker FSM trough Mecanim Behaviour
« Reply #4 on: April 03, 2015, 02:18:57 AM »
Hi,

 ok, good case, I'll work on this to give you more flexibility on that front. Please bump me early next week ok.

 Bye

Jean

C.J.Geringer

  • Playmaker Newbie
  • *
  • Posts: 48
Re: Send event to Playmaker FSM trough Mecanim Behaviour
« Reply #5 on: April 03, 2015, 06:55:42 AM »
Thank you for the quick reply.

I will bump next week.

C.J.Geringer

  • Playmaker Newbie
  • *
  • Posts: 48
Re: Send event to Playmaker FSM trough Mecanim Behaviour
« Reply #6 on: April 06, 2015, 08:32:35 AM »
As requested, I am bumping this.

Thank you for your time.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Send event to Playmaker FSM trough Mecanim Behaviour
« Reply #7 on: April 07, 2015, 02:56:43 AM »
Hi,

 Do you have a animator component with several layer, so that I see how you are organized and how I can tackle this? that will help me progress on that front. Thanks.

pm me if you can't share publicly.

also, you do have an option in the component, "LayerIndex". Is it not what you actually need? have one component for each layer, and they will all work together, each dealing with their owne assigned layer.


 Bye,

 Jean

C.J.Geringer

  • Playmaker Newbie
  • *
  • Posts: 48
Re: Send event to Playmaker FSM trough Mecanim Behaviour
« Reply #8 on: April 07, 2015, 08:39:04 AM »
I have a component and will share it tomorrow night when I get home.

If I understood correctly, the "LayerIndex" chooses whch animator layer the component syncs to. My problem is that I haven´t found out how to sync the selected layer to a specific Playmaker FSM in an object with multiple playmaker FSMs

When I tried the component only gave me the option to chose a game object, not which FSM in the object, nor was I able to find documentation on this.

In other words, If I have many FSMs in a given object, how can I choose only one to be sync with?

Am I missing something?
« Last Edit: April 07, 2015, 05:43:47 PM by C.J.Geringer »

C.J.Geringer

  • Playmaker Newbie
  • *
  • Posts: 48
Re: Send event to Playmaker FSM trough Mecanim Behaviour
« Reply #9 on: April 07, 2015, 05:43:05 PM »
Just re-read your last answer and you requested my animator component with "several" Layers. My animator component actually only has 3 layers, one for each arm and another for the rest of the body.

 It is also fairly simple because I haven´t solved this problem yet, and am waiting to have this working smoothly before adding complexity.

Do you still want me to send it?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Send event to Playmaker FSM trough Mecanim Behaviour
« Reply #10 on: April 08, 2015, 05:07:30 AM »
Hi,

 if you haven't worked it out, yes, I'll do a sample that works.

 Bye,

 Jean

C.J.Geringer

  • Playmaker Newbie
  • *
  • Posts: 48
Re: Send event to Playmaker FSM trough Mecanim Behaviour
« Reply #11 on: April 08, 2015, 10:22:52 PM »
PM sent