Author Topic: EventDelegate with multiple Callbacks seems to be broken  (Read 2244 times)

yozzozo

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
EventDelegate with multiple Callbacks seems to be broken
« on: October 03, 2014, 03:20:39 AM »
Hi,

I'm trying to do this in a loop:

  1. var ed = new EventDelegate(delegate()
  2. {
  3.         playButtonClicked(q.id);
  4. });
  5.  
  6. playButton.onClick.Add(ed);
  7.  

where playButton changes to a different gameObject (UIButton) each time, and q.id is a string that changes each time as well.

I'm trying to do this in a loop with 2 iterations.  This doesn't appear to work -- when clicking the FIRST button, it actually activates the 2nd button's EventDelegate, even though its GameObject is completely inactive.  I've verified this though logging the GetHashCode of the button, and logging when the delegate gets called (what its string param is set to and the hashcode of the button there too).

The solution is unfortunately to not use EventDelegate.Callback when doing multiple EventDelegates this way -- use Parameters:

  1.  
  2. var ed = new EventDelegate(this, "playButtonClicked");
  3. ed.parameters[0].value = q.id;
  4.  
  5. playButton.onClick.Add(ed);
  6.  
  7.  

This means we can't use anonymous function delegates (delegate()) or lambdas (() => {}) in EventDelegates until this is fixed.
Any thoughts?

Thanks.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: EventDelegate with multiple Callbacks seems to be broken
« Reply #1 on: October 04, 2014, 07:35:26 AM »
EventDelegate.Set(playButton.onClick, delegate () { playButtonClicked(q.id); });