playMaker

Author Topic: Specify FSM Component in C# Script [SOLVED]  (Read 3244 times)

acornbringer

  • Junior Playmaker
  • **
  • Posts: 75
    • Acornbringer.com
Specify FSM Component in C# Script [SOLVED]
« on: August 07, 2019, 01:51:19 PM »
Hey there!

I'm trying to make a script that receives animation events from an animation clip and sends them to a specific FSM component. Here's what I have so far:

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

public class AnimationEventsToFSM : MonoBehaviour
{
    private PlayMakerFSM fsm;

    void Update()
    {
        // Find FSM
        fsm = this.gameObject.GetComponent<PlayMakerFSM>();
    }

    public void Animation_Event (string stringToInput)
    {
        // Send event
        fsm.SendEvent(stringToInput);
    }

}

This will take the animation events and send them to the first FSM on the object. This is limiting and I would like to be able to specify the FSM in case there are multiple on the game object.

I found an early thread about referencing the "GameObject.GetComponents" page in the Unity docs for the solution but haven't been able to figure that one out:

https://hutonggames.com/playmakerforum/index.php?topic=1355.0

Any help would be appreciated!
« Last Edit: August 13, 2019, 08:07:18 AM by acornbringer »

Thore

  • Sr. Member
  • ****
  • Posts: 480
Re: Specify FSM Component in C# Script
« Reply #1 on: August 07, 2019, 05:59:25 PM »
If I understand you correctly, this basic function already exists: add an event to the animation timeline, then configure it to SendEvent(String) or something similar, and pass in the name of a global event.

acornbringer

  • Junior Playmaker
  • **
  • Posts: 75
    • Acornbringer.com
Re: Specify FSM Component in C# Script
« Reply #2 on: August 07, 2019, 06:10:29 PM »
[...] add an event to the animation timeline, then configure it to SendEvent(String)[...]

That works indeed but only on editable animation clips. In order to use the events from the clips on the fbx itself, I need to send them through this script first then to the FSM.

I can't work with editable animation clips because when I need to update the animation, I need to replace the old clip with a new one and replace any data that I added to the clip like events and such. This becomes more and more of a time-sink with the more clips I'm dealing with.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Specify FSM Component in C# Script
« Reply #3 on: August 08, 2019, 02:03:17 AM »
Hi,
 

also, I created a wizard to let you create these bridges between Unity events and PlayMaker event:

https://hutonggames.com/playmakerforum/index.php?topic=11172.0

So you don't need to create them manually or even need to know scripting for this.

Bye,

 Jean

acornbringer

  • Junior Playmaker
  • **
  • Posts: 75
    • Acornbringer.com
Re: Specify FSM Component in C# Script
« Reply #4 on: August 08, 2019, 07:45:08 AM »
[...]
also, I created a wizard to let you create these bridges between Unity events and PlayMaker event:

https://hutonggames.com/playmakerforum/index.php?topic=11172.0
[...]

Hi Jean,

The script I posted up there works for non-editable animations which is what I need. It just doesn't specify an FSM on the object - it uses the first fsm component in the list. I'd like to know if someone could help me add to this script to allow it to specify an exact FSM component as there can be several on one object.

I'm not super familiar with C# at all but I believe I could change this piece of the script to specify a particular FSM rather than the first one it finds. Would you know how to do that?

Code: [Select]
void Update()
    {
        // Find FSM
        fsm = this.gameObject.GetComponent<PlayMakerFSM>();
    }

To clarify, here is where the events are being added: (Not in the Animation window but through the Animation tab in the Inspector.)


I'm unable to add or edit events on the Animation timeline like in the example video so I'm unsure the proxy is what I'm looking for. Is the Proxy able to use the events on non-editable clips and does it allow me to specify a specific FSM component to send the event to?

Thanks :)

acornbringer

  • Junior Playmaker
  • **
  • Posts: 75
    • Acornbringer.com
Re: Specify FSM Component in C# Script
« Reply #5 on: August 08, 2019, 09:55:13 AM »
Hey there!

I read somewhere that it's odd to have multiple components of the same kind (and of the same name) on one game object so with the Play Maker FSM (Script)s, it's a unique situation. The way to specify is to create an array of the components and reference them using the integer of the component you need. In my situation, I won't know the intager of the component will be the same on each game object so I make the array and send the event to all FSM components.

It's working in a way I'm okay with so I'll leave this here in case someone needs a similar script:

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

public class AnimationEventsToFSM : MonoBehaviour
{
    private PlayMakerFSM fsmArray;

    public void Animation_Event(string stringToInput)
    {
        // Finds all PlayMakerFSMs on the GameObject
        PlayMakerFSM[] fsmArray = GetComponents<PlayMakerFSM>();
        for (int i = 0; i < fsmArray.Length; i++)
        {
            // Sends events to all FSMs
            fsmArray[i].SendEvent(stringToInput);

        }

    }

}

This script is meant to work with the un-editable animation clips normally nested inside the fbx.

- Put this script on a Game Object with (1.) the Play Maker FSM (Script) component(s) you wish to send animation events to and (2.) the Animator Controller playing the un-editable animation clips you wish to send events from.

- Use the inspector to find the animation clips you want to put events on and add the event keys to the timeline there.

- Edit the Function of each key to say Animation_Event (or edit the function in the script to use what ever you want) then add the exact name of the FSM events you wish to send to the String field.

Like this:


- When the animator of that object plays the animation clip, it will send the events to all FSMs on the object at the keyed time, triggering the correlating event inside the FSM.

I hope this helps someone in a similar situation as myself! Also if anyone sees something wrong with the code, please let me know because I'm just stumbling around in the dark when it comes to this stuff.

Thanks!
« Last Edit: August 08, 2019, 10:15:50 AM by acornbringer »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Specify FSM Component in C# Script
« Reply #6 on: August 12, 2019, 04:01:56 AM »
Hi,

 you can also use the PlayMaker Utils, which features extended PlayMaker api to do just that.

PlayMakerUtils.SendEventToGameObject(null,GO,"MyEvent",bool includeChildren)

Bye,

 Jean

acornbringer

  • Junior Playmaker
  • **
  • Posts: 75
    • Acornbringer.com
Re: Specify FSM Component in C# Script
« Reply #7 on: August 12, 2019, 07:22:35 AM »
[...]
 you can also use the PlayMaker Utils, which features extended PlayMaker api to do just that.
[...]

Very cool! I did not know about that. I'll have to give it a try at some point.

Thanks!