Example:
Toggling a variable between TRUE and FALSE

Actions used:

Control Structure - ELSE

Control Structure - END IF

Control Structure - IF

Page - Hide Object

Page - Show Object

Text Object - Set Text

Variable - Set Value

In this example, we'll show you how to toggle a variable's value between "true" and "false". To help you understand why this would be useful, we'll make this happen every time a Text Object is clicked, and use the value to determine what is actually displayed in the Text Object. In other words, we'll show you how to use this technique to create a basic toggle switch.

Here's what the action list looks like:

Note that most of the actions are just there to do something once we've toggled our variable between TRUE and FALSE. It actually only takes a single "Variable - Set Value" action to toggle a variable.

In this case, we're toggling a make-believe "XYZ feature" on and off, so we named our variable %xyx_toggle%. Toggling a variable means changing it between two different values...in this case, between the two Boolean values "TRUE" and "FALSE". (If it's true, change it to false; if it's false, change it to true.)

Now, we could use a "Control Structure - IF" action to do this with an IF block...by testing whether the value is true or false, and then setting the variable to the opposite value. But there's actually a much easier way: we just use the ! (pronounced "NOT") operator.

The ! operator basically reverses the true/false nature of the value it's applied to. For example, the number 5 is true...so !5 is false. False is false, obviously, so !False is true.

This leads to a very elegant technique, where we can switch a variable between a true value and a false value, by applying the ! operator to whatever is currently in the variable, and assigning the result back to the variable—essentially replacing the current value with its Boolean opposite.

So, we just use a "Variable - Set Value" action with the Evaluate value as expression option enabled, and set the value of %xyz_enabled% to !%xyz_enabled%.

At this point, our %xyz_enabled% variable has officially been toggled. If it was false before, now it's true...and if it was true before, now it's false.

NOTE

 

 

 

If the variable wasn't assigned to anything yet, then it was essentially false. A variable that hasn't been assigned to anything is just a regular string, i.e. the name of the variable itself. Until we assign a value to %xyz_enabled%, it's "value" is essentially just "%xyz_enabled%". All strings except "true" are considered false, so "%xyz_enabled%" is false. So, unless the variable is given a value somewhere else, the first time our action script is run, %xyz_enabled% will be toggled from "%xyz_enabled%" to "true".

The rest of the action list will make the Text Objects on our page reflect the new value of %xyz_enabled%.

First, we use a "Control Structure - IF" action to start an IF...ELSE...END IF structure that will do one thing if %xyz_enabled% is true, and another if %xyz_enabled% is false.

If %xyz_enabled% is true, our XYZ feature is enabled, so we use a "Page - Hide Object" action to hide our "OFF" status Text Object...

...followed by a "Page - Show Object" action to reveal our "ON" status Text Object in its place...

...followed by a "Text Object - Set Text" action to change the text in the Text Object that we're using as a toggle switch (since clicking on the toggle switch will now have the opposite effect).

Next, we use a "Control Structure - ELSE" action to begin the part of our IF...ELSE...END IF structure that will happen if %xyz_enabled% is false.

Then, we use a "Page - Hide Object" action to hide our "ON" status Text Object...

...followed by a "Page - Show Object" action to reveal our "OFF" status Text Object in its place...

...followed by a "Text Object - Set Text" action to change the text in the Text Object that we're using as a toggle switch (since clicking on the toggle switch will now have the opposite effect).

Finally, we use a "Control Structure - END IF" action to end our IF...ELSE...END IF structure.

Here's a link to the finished example, as an AutoPlay Media Studio 4.0 page that you can import into your own project: