Author Topic: Does event delegates has to be methods on MonoBehavior? (3.0.2)  (Read 2094 times)

mortennobel

  • Guest
Does event delegates has to be methods on MonoBehavior? (3.0.2)
« on: October 20, 2013, 09:57:05 PM »
I'm a little confused why event delegates now has to be methods on a MonoBehavior object. In my code I use something like

      EventDelegate.Callback onChange = delegate {
            // foo
      };
      EventDelegate.Add(slider.onChange, onChange);

However this now creates a event delegate  which 'isValid' property returns false. The event delegate is added to the slider.onChange but is never invoked since it is invalid.

Is this "feature" intended?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Does event delegates has to be methods on MonoBehavior? (3.0.2)
« Reply #1 on: October 21, 2013, 01:10:12 PM »
Yes.

To save a delegate reference in Unity that has not been specified by code, the only way to do it is to have a reference to a MonoBehaviour, as well as a string for the function name. Given these two values it's possible to recover the delegate.

Delegates specified using inline functionality you're using are not possible to save, so they are incompatible with this system.

mortennobel

  • Guest
Re: Does event delegates has to be methods on MonoBehavior? (3.0.2)
« Reply #2 on: October 21, 2013, 06:44:35 PM »
In my case I'm scripting the events runtime and don't need the events to be persistent.

I understand your argument though, but you suggest a warning if the target cannot be cast to MonoBehavior (EventDelegate.Set (line 130)).

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Does event delegates has to be methods on MonoBehavior? (3.0.2)
« Reply #3 on: October 22, 2013, 12:41:08 AM »
That's a fair point. I'll add one.