Author Topic: UIToggle and scripting  (Read 3101 times)

Livealot

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 17
    • View Profile
UIToggle and scripting
« on: June 04, 2014, 06:28:46 PM »
In the basic description for UIToggle, it would be great to share the best/easiest/latest way to use checkboxes to execute different scripts.

Let's take the basic options menu example.  If the user checks the box, execute the TurnOn script.  If the user, at some later point, unchecks the box, execute the TurnOff script.  And remember from game session to game session what the user selected with the UISaved Option component.

Here's my current guess as to the best answer. 

Create a method and attach it to the OnValueChange field of the UIToggle component. That method should look something like this…

       public void ChangeWithTheCheckBox () {
      //when the toggle is checked
      if (MyToggle.current.value) {
         //do this
                        TurnOn();
      }
      //when the toggle is unchecked
      else {
         //do this other thing
                        TurnOff();
      }
   }

I've noticed that UIToggle.value is better to use than the PlayerPref Key Name for that toggle since there's a race condition between the toggle notifying the script and the playerpref being updated.

The trouble with this approach is that the toggle value changes (sometimes) at scene startup, which would execute either TurnOn or TurnOff without user intent, which may not be desired.  For example, if the checkbox was for Facebook, the first time it was checked you would want to register and login, but you would not want to re login every time you loaded the options menu.

So I'm hoping there's a best practice for this common scenario.
« Last Edit: June 05, 2014, 09:51:16 AM by Livealot »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIToggle and scripting
« Reply #1 on: June 05, 2014, 01:38:18 AM »
I'd advise using UIToggle.current.value -- this is always going to be valid inside a toggle-triggered callback.

The fact that the callback function gets called during startup is intentional. It's done so that the associated callback is in the same state as the toggle.

Livealot

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 17
    • View Profile
Re: UIToggle and scripting
« Reply #2 on: June 05, 2014, 10:09:33 AM »
Thanks!  I updated for MyToggle.current.value above.  I missed that originally because "current" was not an auto-fill choice in MonoDevelop but "value" was.

I also understand why it's important to have the callback function get called during startup. 

My question is, "Do you have a recommendation on how best to handle the scenario where we don't want the saved state to fire the associated OnValueChange function?"  Or would you advise not using checkboxes in this way if this scenario is possible?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIToggle and scripting
« Reply #3 on: June 06, 2014, 02:09:27 AM »
Just check inside your callback function to see if the script has executed its Start() function yet or not, and exit out early if it hasn't. You will also want to make your script's execution order to be +1 -- so that it runs after the toggle.