Author Topic: NGUI 3 - Can no longer use anonymous delegates for events...  (Read 12822 times)

kurozael

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 12
    • View Profile
NGUI 3 - Can no longer use anonymous delegates for events...
« on: October 18, 2013, 03:51:16 AM »
I used to be able to do this:

tweener.onFinished = delegate(UITweener tween) { // Do something; }

Now I can't, because EventDelegate.Set(tweener.onFinished, delegate() {}) never gets called due to code Callback Get() (EventDelegate.cs)

Can this be solved, because I choose to use NGUI completely programmatically.

Thanks.

Smooth P

  • Guest
Re: NGUI 3 - Can no longer use anonymous delegates for events...
« Reply #1 on: October 18, 2013, 10:10:42 AM »
Is it just me, or is the new event system completely counter to best practices in real world professional-level programming?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI 3 - Can no longer use anonymous delegates for events...
« Reply #2 on: October 18, 2013, 04:22:05 PM »
You can't inline delegates with the new system. Just make them an explicit function and it will work as expected.

The system was designed to make it easier to use from the editor (inspector) side, making it more accessible for beginners and non-developers.

To me it's a minor inconvenience personally, since I always found inline delegates to be difficult to read and a less clean way to do programming.

kurozael

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 12
    • View Profile
Re: NGUI 3 - Can no longer use anonymous delegates for events...
« Reply #3 on: October 22, 2013, 04:11:08 PM »
Sometimes, when programming UI dynamically - and dynamic programming in general, anonymous methods are very useful. I would disagree that this is unclean, and is in fact just a more modern approach. I would also argue that they are easier to read, because you can see what is happening right there, without having to locate the callback elsewhere in the script when reading through.

You could at least implement a way to use anonymous delegates while having it still usable by beginners. Please don't ruin it for the more advanced users, too! One way to do this, might be to have a boolean parameter to the EventDelegate class whereby we can specify whether this particular callback is anonymous.

NGUI is a very good tool, and without a doubt the best for Unity at the moment. I just hope it doesn't get watered down even further.

Thank you.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI 3 - Can no longer use anonymous delegates for events...
« Reply #4 on: October 25, 2013, 12:57:03 AM »
The latest 3.0.3 allows you to use anonymous delegates:
  1. EventDelegate.Add(btn.onClick, delegate() { Debug.Log("Click!"); });

helmesjo

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 116
    • View Profile
Re: NGUI 3 - Can no longer use anonymous delegates for events...
« Reply #5 on: April 05, 2014, 05:40:06 PM »
So, is this again not working? Still having some issues (it ignores the lambda and sets some default, public method)...
I like that your going for a more inspector-only approach, but lambdas are crusual for event-callbacks in code. Writing small methods for every callback is cumbersome and clutters the code. I mean, often it's no more than a boolean swopping value on finish of some tween.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI 3 - Can no longer use anonymous delegates for events...
« Reply #6 on: April 06, 2014, 02:46:46 AM »
There are no known issues with anonymous delegates. What code are you using that isn't working?

helmesjo

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 116
    • View Profile
Re: NGUI 3 - Can no longer use anonymous delegates for events...
« Reply #7 on: April 06, 2014, 03:21:35 AM »
There are no known issues with anonymous delegates. What code are you using that isn't working?

  1. UITweener t = TweenAlpha.Begin(ScrollViewPanel.cachedGameObject, 0.01f, 0f);
  2. EventDelegate.Add(t.onFinished, delegate(){
  3.         TweenAlpha.Begin(ScrollViewPanel.cachedGameObject, 0.25f, 1f);
  4. });
  5.  

I get this error:
Could not find method '<ReloadTable>m__16' on QuestionTable

since it's setting some arbitrary method when registering the callback (I'm guessing). So it's calling the method (or trying) instead of invoking the callback. ReloadTable is actually the method this code is run within.

huuloc

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: NGUI 3 - Can no longer use anonymous delegates for events...
« Reply #8 on: April 06, 2014, 03:59:42 AM »
  1. UITweener t = TweenAlpha.Begin(ScrollViewPanel.cachedGameObject, 0.01f, 0f);
  2. EventDelegate.Add(t.onFinished, delegate(){
  3.         TweenAlpha.Begin(ScrollViewPanel.cachedGameObject, 0.25f, 1f);
  4. });
  5.  

I get this error:
Could not find method '<ReloadTable>m__16' on QuestionTable



since it's setting some arbitrary method when registering the callback (I'm guessing). So it's calling the method (or trying) instead of invoking the callback. ReloadTable is actually the method this code is run within.

Look like same issue with me. I can't use EventDelegate of upgrade the latest version 3.5.6.

huuloc

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: NGUI 3 - Can no longer use anonymous delegates for events...
« Reply #9 on: April 06, 2014, 04:06:13 AM »
For more infor,
I use the old pattern:
  1. EvenDelegate.Add(btn.onClick,MethodName)

It works as well on Editor. However, stop working when run on WP8.


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI 3 - Can no longer use anonymous delegates for events...
« Reply #10 on: April 06, 2014, 01:06:05 PM »
So WP8 as the target device? Reflection doesn't work properly (or at all) on Flash and NETFX_CORE platforms (WP8/Windows Store?). I just went through the EventDelegate class and made sure it compiles/works correctly if reflection is not supported. There were a few issues I ran into, but nothing that matches "Could not find method". If you don't have Pro, PM me / email me your OR# and I'll send you the updated file to try out.