Author Topic: Checkbox calls event receiver on start even if startsChecked is false  (Read 3264 times)

zippo227

  • Jr. Member
  • **
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 55
    • View Profile
Hi, I appreciated your advice at Unite2013 and when you looked at a game with me and my coworker. I've just updated to the newest version of NGUI 2.6.5b.

I am using a checkbox to change settings in my own personal game. When the panel containing the checkbox is enabled, it calls that checkbox's event receiver, even though I have startsChecked set to false. The problem is that when this happens, the checkbox is now activating my toggled parameter in reverse because checking the box will now set the parameter I'm toggling to false.

I looked through the code, and it seems to me that UICheckbox's Start is in the wrong order. Start calls Set after already setting the mStarted variable to true. I think line 115 and 116 should be reversed. Otherwise, in my humble opinion, line 131 will never return true and you'll always proceed into the else if on line 137. I've modified it in my project and the checkboxes are now working as expected. I'm surprised this never bothered anyone else? :o

I've tried only listing the line numbers to keep from posting any source code to the public eye.

Lord_Meshadieme

  • Guest
Re: Checkbox calls event receiver on start even if startsChecked is false
« Reply #1 on: September 08, 2013, 03:08:57 AM »
i did come across this and just saved my checkbox value inversely :P.

zippo227

  • Jr. Member
  • **
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 55
    • View Profile
Re: Checkbox calls event receiver on start even if startsChecked is false
« Reply #2 on: September 08, 2013, 08:26:32 PM »
You should, hopefully, never have to code your project in a reverse order of logic just to meet the requirement of the library.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Checkbox calls event receiver on start even if startsChecked is false
« Reply #3 on: September 09, 2013, 01:04:14 AM »
It's done so for consistency. This way your function is guaranteed to be called setting its starting value correctly.

zippo227

  • Jr. Member
  • **
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 55
    • View Profile
Re: Checkbox calls event receiver on start even if startsChecked is false
« Reply #4 on: September 09, 2013, 09:46:06 AM »
It's done so for consistency. This way your function is guaranteed to be called setting its starting value correctly.

I see now that the message contains "mChecked" parameter. I was previously setting the variables in the received object at compile time and having the function toggle that parameter. Now, I have removed the hard coding, and the function receives the parameter. It is going to cause a noticeable stutter when the option menu is enabled and all of my functions set their parameters to what could have been done at compile time.

I still cannot see the scenario where the Set function will be called without the Start function being called first, so I stand by line 131-136 never being called.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Checkbox calls event receiver on start even if startsChecked is false
« Reply #5 on: September 10, 2013, 08:58:50 AM »
Start() gets called after all scripts received their Awake() and OnEnable() calls. Nothing is stopping users from doing a 'set' inside one of those.

Dover8

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 20
    • View Profile
Re: Checkbox calls event receiver on start even if startsChecked is false
« Reply #6 on: September 20, 2013, 04:54:30 AM »
I am also looking at this as I have used checkboxes to control the tween position of menus sliding in. Everything works as expected, other than the fact the tween is getting called because of the Set in OnStart(), so all my menus appear on the screen a slide away at the start.