Author Topic: OnClick on a button fires again when modifying the same OnClick event  (Read 6197 times)

sbox

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 2
    • View Profile
Hi, I was wondering if this is intended behaviour.
My button fires again if I modify the existing OnClick EventDelegate during an OnClick event.
Basically I have a layered menu, and I want to go back one level, but instead because I modify the OnClick event of the same button during the execution of said event, it returns all the way down to the bottom level.

  1. void Start()
  2. {
  3.                 backButton.onClick.Add(new EventDelegate(ShowMenu3));
  4. }
  5. void ShowMenu1()
  6. {
  7.                 ...
  8. }
  9. void ShowMenu2()
  10. {
  11.                 ...
  12.                 backButton.onClick[0] = new EventDelegate(ShowMenu1);
  13. }
  14. void ShowMenu3()
  15. {
  16.                 ...
  17.                 backButton.onClick[0] = new EventDelegate(ShowMenu2);
  18. }

One click on the button, and it goes all the way to ShowMenu1 , instead of stopping at the end of ShowMenu3.
If I comment out the
  1. backButton.onClick[0] = new EventDelegate(ShowMenu2);
then it stops at ShowMenu3.
« Last Edit: April 17, 2015, 04:25:12 PM by sbox »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: OnClick on a button fires again when modifying the same OnClick event
« Reply #1 on: April 18, 2015, 12:57:46 PM »
You should not be modifying a list that's being executed. Delay your action by a frame.
  1. void ShowMenu3 ()
  2. {
  3.     Invoke("ChangeMenu3", 0f);
  4. }
  5.  
  6. void ChangeMenu3 ()
  7. {
  8.     EventDelegate.Set(backButton.onClick, "ShowMenu2");
  9. }

sbox

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: OnClick on a button fires again when modifying the same OnClick event
« Reply #2 on: April 18, 2015, 01:06:42 PM »
Seems odd that it requires that, but fair enough, thanks!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: OnClick on a button fires again when modifying the same OnClick event
« Reply #3 on: April 18, 2015, 01:17:53 PM »
It's generally advisable to create different buttons and then enable/disable them instead of changing their delegates and content.

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: OnClick on a button fires again when modifying the same OnClick event
« Reply #4 on: April 18, 2015, 05:26:00 PM »
Remember that EventDelegate and delegates (and its variations Action, Func, ~event) are essentially just a list of functions - if you modify a list while iterating through it, which is what you'll be doing when the delegates are invoked, you change the container that's being run though.. this is generally something you want to avoid.

  1. //equivalent ish
  2. for(int i = 0; i < myIntList.Count; i++)
  3. {
  4.  if (myIntList[i] == 0) myIntList.RemoveAt(i); //bad baby *kick*
  5. }
  6.