playMaker

Author Topic: Delegate and Event patterns in Playmaker  (Read 3368 times)

DuaneDog

  • Playmaker Newbie
  • *
  • Posts: 9
Delegate and Event patterns in Playmaker
« on: February 14, 2014, 11:20:54 PM »
Greetings,

I'm coming from a C# background and naturally in game development I'm doing a lot of delegates and events. Maybe I'm missing something simple, but if I have all these delegates and of course the classes and variables within those classes to handle events... how do I hook into them from Playmaker?

Here is an example to make it clear what I am asking:

Code: [Select]
public class TGCConnectionController : MonoBehaviour {
private TcpClient client;
  private Stream stream;
  private byte[] buffer;

public delegate void UpdateIntValueDelegate(int value);
public delegate void UpdateFloatValueDelegate(float value);

public event UpdateIntValueDelegate UpdatePoorSignalEvent;
public event UpdateIntValueDelegate UpdateAttentionEvent;
public event UpdateIntValueDelegate UpdateMeditationEvent;
public event UpdateIntValueDelegate UpdateRawdataEvent;
public event UpdateIntValueDelegate UpdateBlinkEvent;

public event UpdateFloatValueDelegate UpdateDeltaEvent;
public event UpdateFloatValueDelegate UpdateThetaEvent;
public event UpdateFloatValueDelegate UpdateLowAlphaEvent;
public event UpdateFloatValueDelegate UpdateHighAlphaEvent;
public event UpdateFloatValueDelegate UpdateLowBetaEvent;
public event UpdateFloatValueDelegate UpdateHighBetaEvent;
public event UpdateFloatValueDelegate UpdateLowGammaEvent;
public event UpdateFloatValueDelegate UpdateHighGammaEvent;

etc...

Now in my C# class I could do this...

Code: [Select]
controller = GameObject.Find("MyGameObject").GetComponent<TGCConnectionController>();

controller.UpdatePoorSignalEvent += OnUpdatePoorSignal;

void OnUpdatePoorSignal(int value){
poorSignal1 = value;
}

The code block above is what I need to be able to make happen in a Playmaker state. How would I 'subscribe' to all those events I'm setting up so that I can get, for example, to set a playmaker variable 'poorSignal1' to the value that has been modified because of the event?

I'm assuming that Playmaker must have an easy way to hook into these events or otherwise you really couldn't easily bind the state machines without creating other interfaces.

And I guess the next obvious question if it is not intuitive is how do I do the unsubscribe equivalent of this in Playmaker:

Code: [Select]
controller.UpdatePoorSignalEvent -= OnUpdatePoorSignal;
Many thanks in advance!


DuaneDog

  • Playmaker Newbie
  • *
  • Posts: 9
Re: Delegate and Event patterns in Playmaker
« Reply #1 on: February 15, 2014, 03:33:43 PM »
The more I look into this, the more it seems that it is perhaps the biggest weakness of using Playmaker to build up games. Correct me if I'm wrong... but you can't directly subscribe to delegates from within Playmaker and must essentially create a bridge.

That is why we have a whole set of scripts you must acquire separately just so we can work with NGui and Playmaker together. Or at least not without a very substandard design.

The problem we are going to face is that I'll likely have a hundred or more delegates in C# classes scattered through my game. Some of these will be written by myself, some by people who are closely involved with this project, and some written by people who don't even know Unity but can write the components I will need. Then it appears we are going to spend a great deal of time writing actions just so that then we can go and use them in Playmaker. This kills any sort of prototyping workflow and creates a more brittle, coupled design.

I'd like to heard from Hutong Games on why there can't be a generic action that can subscribe and unsubscribe to these events directly? Is there anyone out there that has something like this or would have the skills to help me at least figure out if it is possible? 

Love this tool but we really need an action we can drop in and configure the following:

Code: [Select]
controller = GameObject.Find("TheObjectWithTheDelegatesINeed").GetComponent<MyDelegateClass>();

controller.TheEventIneed += TheEventtocallInTheStateMachine;


Again, maybe I'm missing something very simple... but the awkward handling between NGui and Playmaker (the necessity for extra scripts) seem that is the path you go down for nearly everything if you want to use Playmaker for serious development.
« Last Edit: February 15, 2014, 03:37:48 PM by DuaneDog »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Delegate and Event patterns in Playmaker
« Reply #2 on: February 16, 2014, 11:05:04 PM »
Hi,

 Interesting post.

Having PlayMaker event delegates is not going to completly remove the need to create bridges and custom actions for Ngui support and the likes. I think that bridges will always be needed. BUT I do also think that a delegating system would make the job a lot easier and bring more possibilities on the table.

 I did mention this few times to Alex. I do see a lot of potential as well.

Writing custom actions should be seen as building blocks. I think this is a great way to create these bridges, and indeed in some of my custom action I do use delegates, because in a custom action, that's completly ok to do this.

I think PlayMaker delegates are missing for listening to an Fsm sending an event or for catching a global event broadcast. That's the missing feature imo. Typically, porting solutions like GameAnalytics would be a case of catching global events, and treat them: if they match a certain pattern this can then be transform as a GameAnalytics event sent to their server. In this I see a great potential.


In your pseudo code, I am confused a little bit, because it seems that you want to send a PlayMaker event, in which case there is nothing stopping you. You can fire a PlayMaker event just fine with the current api. There are few restrictions, but it's very easily overcome. If you need more info on this let me know.

Also, the typically problem you will face with such delegating, is that PlayMaker may not be in a position to treat all variables of the delegate signature, maybe some variables will be classes and types unsupported, so your pseudo code would still not not work for all cases.

So for me the actual pseudo code I would like to have is the opposite, I want to catch an FSM firing an event so that I can forward it to the outside world withint having to write a hardcoded bridge with an fsm implement the events I want to listen too, I want to catch ALL sent event, or maybe all event sent from a particular Fsm.

In the case of NGUI, you won't be able to avoid creating a bridge of some sort, your pseudo code will have to leave somewhere and building a monobehavior will make sense in terms of workflow to let the developer define how he wants ngui and playmaker to interact. Giving this assumption, in the case of NGUI, I don't think there is are missing features in PlayMaker API, but maybe I don't see a use case.

Could you give an example on integrating ngui and playmaker with a pseudo code? Id be very interested to see how you would like to work on this.

Bye,

Jean






Bye,

Jean