This tip can cut the size of your scripts in half! (Ternary Operator tutorial)

Sound Familiar?

Do your scripts ever look like this?

Or have you ever nested stacks upon stacks of boolean expression controls, like this?

Anyone who’s attempted to make a game in max2d has more than likely created scripts that look similar to these beasts. I know I have. Once a script becomes larger than your screen when zoomed all way out, it’s not only difficult to follow the logic, but if you’re like me, as you try to move around the script, you occasionally (and unintentionally) remove a path connecting two script controls without knowing it and break your game! (Haha. Thank goodness for undo!)

Obviously, the less number of script actions, controls, and pathways connecting it all together you have in your script, the better. Right?

What if I told you that you can cut your script size in half , and in the case of the stacked boolean expression controls, by a lot? Keep reading to find out how. But first, lets talk about what is making your scripts so large.

The Culprit!

Out of all the possible script controls that are available to use, there is one that stands out from the rest. It’s the most frequently used control and more importantly, it provides the most connections to other parts of your script. It is at the heart of every game.

I’m talking about the Boolean Expression Control

This control alone creates a mess of your scripts. It’s the only script control with three possible output ports, (true, false, and bypass)

Of course, it provides the critical boolean logic that makes your game work. Without boolean logic, your game wouldn’t be very playable or interesting.

The Ternary Operator to the rescue!

So we need the boolean logic that the boolean expression control provides in order to make a good game. But, did you know max2d has another way to perform boolean logic without adding any controls to your script? It’s a powerful language construct who’s name comes from Javascript, but it is available in just about every modern day programming language there is. Its called the “Ternary Operator” in javascript and other languages. Some languages call it the “Conditional Operator”.

The syntax might seem difficult at first but I promise it’s not. In javascript (and max2d, and many other programming languages) the syntax is this:

condition ? exprIfTrue : exprIfFalse

That is:
some condition,
followed by a question mark,
followed by an expression that executes when the condition is true,
followed by a colon,
followed by an expression that executes if the condition is NOT true

The name ternary operator comes from the fact that it has 3 parts (the condition, the expression if true, and the expression if false). An “operator” in programming is " a character that represents a specific mathematical or logical action or process ." Arithmetic operators include “+” for summing, “-” for subtraction, etc. Logical operators include “&&” for a logical AND, “||” for a logical OR, etc.

In javascript and many other languages, the ternary operator is the only operator with 3 parts.

This is often used in coded languages as a quick, 1-line alternative to an if...then...else statement. It can be used in max2d as well, as a quick, 1 line alternative to… you guessed it! The boolean expression script control

Examples

I’ll first give you an example using a pseudo-language, because it will be easier to understand how to use this powerful construct in max2d afterward.

Lets say I have 2 variables, PlayerScore, which is a number representing the player’s score, and GameOver, which is a boolean that indicates whether the player has died or the game is over. (The type of these variables isn’t important. They could be anything) In this psuedo-code below, we are increasing the player’s score by 10 points if the game is not over. If the game is over, we instead want to subtract 100 from their score.

Using an if… then statement would look something like this (in pseudo-code):

if GameOver  then: 
     PlayerScore = PlayerScore - 100;
else:
     PlayerScore = PlayerScore + 10;
endif

This does the job. But it takes 5 lines and there is some repetition in the code. (We have PlayerScore = twice.) We can do better than that.

The same logic can be written in one line, using the Ternary Operator:

PlayerScore = GameOver ? PlayerScore -100 : PlayerScore + 10;

Notice there is no if...then statement at all! But the result is the exact same.

Remember the syntax:

condition ? exprIfTrue : exprIfFalse

So:

PlayerScore = GameOver  ? PlayerScore -100 : PlayerScore + 10;

PlayerScore = condition ? exprIfTrue       : exprIfFalse

In the above example:
The condition is simply GameOver. (This is the same thing as GameOver == true) The question mark immediately after tells the compiler that we aren’t setting PlayerScore equal to GameOver. We want it to be the condition of a ternary operator and what follows will be the exprIfTrue.

The exprIfTrue is PlayerScore-100. So when GameOver is True, subtract 100 from PlayerScore.

The exprIfFalse always starts after the colon and in our example, it is PlayerScore+10. In other words if the game is NOT over (GameOver == false) then add 10 to PlayerScore

Ternary Operator in Max2d

“But max2d scripts are not written in code! Max2d uses visual scripting!”

While this statement is true for the most part, don’t forget about the expression solver! Many of the scripting controls allow values to be set by using an expression:

Anywhere that you can utilize the expression solver, you can use the Ternary Operator syntax.

For example, lets say I have a sprite and a variable called camLeft. I only want the sprite to be visible when camLeft is greater than 0. You might set up the script like so:

The boolean expression checks if camLeft is greater than 0, if so, the scale of X is set to 1, other wise it will be set to 0. Simple enough. But we used 4 total script controls to do it.

With a ternary operator, all you need is the on Object Loaded event and a single set Transform action. 2 controls. That’s it! You just cut the script in half! Here’s what it looks like:


The expression on the scaleX property uses a simple ternary operator: camLeft > 0 ? 1.0 : 0.0 (If camLeft is greater than 0, set the scale on the x axis to 1.0, otherwise set it to 0.0. )

Nested Ternary Operators

This is just the tip of the iceberg. You can nest ternary operators easily. Both expressions, (if the condition is true and if the condition is false) can themselves be conditions for a new ternary expression.

In the screenshot above, I am setting the x axis scale of this game object to 1 if topLeft_x is not equal to 0 AND bottomRight_x is equal to 0. Otherwise, the scaleX is set to 0. (NOTE: This could have been done using a single ternary operator along with a logical AND && operator, (That would look like topLeft == 1 && bottomRight == 0 ? 1.0 : 0.0) but I wanted to show and example of nested ternary operators. ) The nesting can go as deep as you’d like. Just make sure that for any “?”, you have a matching “:”.

Conclusion

This was a long post. If you made it this far, I hope you learned something from it. If you didn’t, go through the examples once more. You wont regret it.

Also, I made this post because, sadly, I am not going to be around here much any more. I am moving on to the next chapter in life and I just won’t have enough time to be active. I hope this tutorial on the ternary operator is useful to someone.

Well, thank you all for supporting Max2d and for helping me when I got stuck. I hope that max2d continues to thrive and this community never dies.

So, the next time you add a boolean expression to set a property or variable that supports the expression solver, consider using a ternary operator instead!

I used my phone to post this so forgive me if the formatting isn’t right or the pictures don’t fit correctly around the text.

2 Likes