playMaker

Author Topic: C# Event binding to FSM Event  (Read 1996 times)

Webtech

  • Playmaker Newbie
  • *
  • Posts: 5
    • View Profile
C# Event binding to FSM Event
« on: January 10, 2015, 10:05:21 AM »
Howdy,

Couldn't find a way to do this built in so wrote a little script which hooks everything up....apart from one thing which I'll explain after the script. It works fine on PC/OSX, should work on iOS as I've done something similar in the past. I can't check on Android though but as it's only using the built in libraries I don't see why not.

Here is the script.

Code: [Select]
using System;
using System.Reflection;
using UnityEngine;

public class SendFSMEvent : MonoBehaviour
{
    public Component BroadcastingComponent;
    public string EventToSend;
    public string ListenForEvent;
    public PlayMakerFSM RecievingFSM;

    private void Start()
    {
        var eventInfo = BroadcastingComponent.GetType().GetEvent(ListenForEvent);
        var eventArgType = GetEventArgsType(eventInfo);
        var methodInfo = typeof (SendFSMEvent).GetMethod("OnEvent");
        var boundEventhandler = methodInfo.MakeGenericMethod(eventArgType);
        var newDelegate = Delegate.CreateDelegate(eventInfo.EventHandlerType, this, boundEventhandler);
        eventInfo.AddEventHandler(this, newDelegate);
    }

    public Type GetEventArgsType(EventInfo eventType)
    {
        var eventHandlerType = eventType.EventHandlerType;
        var method = eventHandlerType.GetMethod("Invoke");
        var parameters = method.GetParameters();
        return parameters[0].ParameterType;
    }

    public void OnEvent<TEventArgsType>(TEventArgsType eventArgs)
    {
        RecievingFSM.SendEvent(EventToSend);
    }
}

The gist of what you need to do is throw this script on a GameObject, drag the script (not the game object) of what is firing the event (so if you use EasyTouch then you would grab the EasyButton/EasyJoystick etc script from within the GameObject) into the Broadcasting Component property in the Inspector. Enter the name of the c# event you are wanting to hook up into the Listen For Event property. Then drag the gameObject which contains the Event you want to fire into the ReceivingFSM property and finally type in the name of the FSM event you want to trigger in the Event to Send. Thats it. I've used reflection and generics to hook up a generic C# event which just triggers the FSM event. It works fine for simple triggers and I'll work some more on this too but thought at this stage people may want to play with it.

Things I know I need to do.
  • Hook up a way to transfer the eventArgs data
  • Find a way to differentiate between two controls firing the same event (i.e Two Easy Buttons, but this could be solved by transferring the eventArgs)

Any additions people want let me know or if I've properly gone west and there is a built in way to sort this :).

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 12342
  • Official Playmaker Support
    • View Profile
Re: C# Event binding to FSM Event
« Reply #1 on: January 19, 2015, 12:54:40 AM »
Hi,

 I like the sound of this!! Keep us updated!


 Bye,

 Jean