Toggle/Switch Example: Turn a light on and off

This example will show how to toggle a Boolean variable to turn something on and off. We'll use a light in this case. The same script structure can be used for anything you want to toggle.

Setup

In the Workshop, we will create a script that will turn the light on and off anytime you select the light stand.

Note: In Unity, I have modified the Light Stand by adding a Motive Scene Object, naming it, put a collider around it and made the collider a trigger. I then added Light Stand to the Workshop Objects catalog in Storyflow.

Step 1 - Add Scene Object to a New Script

  • Create a new script and call it Light Toggle Example
  • Open the script and rename the first sub frame Scene Objects
  • Add the following Scene Objects:
    • Lights
    • Light Stand

Step 2 - Add a Frame for Default Values

  • Click the "+" button next to Main Frame
  • Rename the new frame Default Values

Step 3 - Create a Boolean Variable to Use as a Toggle

In this step, we will create a Boolean variable that we can use to toggle the light on and off.

  • In the Default Values frame, click the Variables tab on the right side of the editor.
  • In the name field, type Toggle
  • In the second drop down menu under valueDefinition, select Boolean
  • Click the Checkmark in the banner next to the variable name
Create Toggle Variable

Save the script.

Step 4 - Assign the Toggle to the Light Visibility

Add a Set Object Properties resource to the Default Values frame.

  • Select Lights from the World Objects drop down menu
  • In the Properties menu, select Visibility and click the "+" button

  • Click the dynamic symbol next to the Is Visible field
  • Select the Toggle variable from the drop down menu
  • Click Set Value and check the checkbox
Assign Toggle to Light Visibility

Now, the Toggle variable will control if the light is visible or not. At any point in the script, we can change the value of the Toggle variable and the Light visibility will change.

Save the Script.

Step 5 - Select the Light Stand

Create a new frame and call it Select the Light Stand. You can use different types of interactions but we'll use Select for this example.

  • Add a Select Resource to the frame
  • In the World Object drop down menu, choose Light Stand
  • In the Event drop down menu, choose success

Click the blue "+" button at the bottom of the Select resource and click on success. This will create a new frame with the condition that the Select has succeeded.

Success from Shortcut Menu

Step 6 - Toggle the Value On and Off

In this frame, the Light Stand has been selected. We want the visibility of the light to change from off to on or from on to off.

Drag the Toggle variable into the new frame.

Click the Dynamic symbol above the Variables section.

  • Click the "+" button next to Defaults
  • Click the "+" button next to Branch
  • Rename the first branch Turn Off
  • Rename the second branch Turn On

Turn Off Branch

  • Expand the Turn Off branch by clicking on it
  • Add a Variable Comparison condition
  • In the condition, select Toggle from the Variable Menu
  • Click the "+" button next to Comparer
  • Check the checkbox next to Value
  • Below the condition, in the Variables drop down menu, select Toggle and leave the Value unchecked.
Set Turn Off Branch

This branch will look at the value of Toggle. If it is true, or on, it will change the value to false, or off. 

Turn On Branch

This branch is the same as the previous one except the checkbox values are reversed.

  • Expand the Turn On branch by clicking on it
  • Add a Variable Comparison condition
  • In the condition, select Toggle from the Variable Menu
  • Click the "+" button next to Comparer
  • Uncheck the checkbox next to Value
  • Below the condition, in the Variables drop down menu, select Toggle and check the checkbox next to Value

Save the script.

Step 7 - Add a Replay

We want the light to turn on and off whenever a user selects it. A replay can make that happen.

  • Add a child frame to the we worked on in the previous step
  • Rename it Replay Select Light Switch

You may still see the Dynamic view for the frame. Click the icon shown to return to the standard view.

  • Add a Replay Frame resource to the frame
  • In the Frame drop down menu, choose the Select Light Frame frame

Save the script.

Step 8 - Test the Script

In the Workshop, run the script and select the Light Stand. You can do this repeatedly to turn the light off and on.

Toggle Light On and Off