2019 2022

Thank you for taking part in the NotSlot journey!
We invite you to join us in our next chapter:

  • by nir,
  • Danielle Elias

Unity Visual Scripting Part 4: Logic Composing

What makes games unique compared to other entertainment mediums is their ability to react and adapt to their consumer in real-time.
Reacting to players’ actions dictates the game to be aware of those actions and the rest of the game’s state.
In this article, we will learn how to compose logic for asking the game about the current state.

When writing scripts, we can ask questions about the players’ actions and the game state; after receiving answers, we can decide upon them, as we will see in a later video.
To compose questions, Unity’s Visual Scripting offers us a handful of Logic nodes.
Each Logic node provides a single output, a boolean; thus, this article is all about the art of true and false.

Let’s say our game features a car. We can ask whether it has gas, do we have its key, is it broken, etc.

Comparing Values

The building blocks for asking questions are comparing one thing to another.

Unity Visual Scripting – Equal and Not Equal nodes
Equal and Not Equal nodes.

Equality

The fundamental comparison type is asking whether one thing is the same as another.
In a car, if the remaining amount of gas in the tank is equal to 0 gallons, we are out of gas; therefore, the car can’t drive.
In a graph script, we have the Equal and Not Equal nodes; each receives two values and answers if they are the same.
Those nodes accept any data type, including numbers, strings, booleans, vectors, and others...

Unity Visual Scripting – Less, Less or Equal, Greater and Greater or Equal nodes
Less, Less or Equal, Greater and Greater or Equal nodes.

Less & Greater

When we have two numbers, we can compare their values and ask whether one is greater than, or maybe less than, the other.
If the car’s speed is above 55 miles per hour, we are exceeding the speed limit.
In a script, we can use the Greater and Less nodes to compare numbers. We also have the Greater Or Equal node to ask if a value is at least some value. And the Less Or Equal node to ask if a value is at most some value.

Unity Visual Scripting – Comparison node
Comparison node.

Comparison

If we want to ask multiple questions upon the same values, we can use the Comparison node, which answers all questions at once.

Composing Logic

Now that we have the basic building blocks, we can combine them into more complex questions.

Unity Visual Scripting – And node
And node.

And

If a character owns a car And has a key, it can drive the car.
We use the And node to combine two questions, concluding if the answer to both is true.

Unity Visual Scripting – Or and Exclusive Or nodes
Or and Exclusive Or nodes.

Or

If a car is out of gas Or has a malefaction, it can’t continue driving.
We can use the Or node to ask if either of the answers is true.
One thing to note is that when we say either question is true, we don’t mean that only one can be true. If we do want to limit only one of the questions to be true, we have another node to accomplish it.

Exclusive Or

Even if two characters own keys to the same car, it can only have one driver at once.
We use the Exclusive Or node to ask whether one and only one of the questions is true.

Unity Visual Scripting – Negate node
Negate node.

Negate

As stated before, all of the nodes we discussed in this video result in a boolean value. We use the Negate node to convert a positive boolean into a negative and vice-versa.
For example, the Not Equal node is just a shortcut for negating the Equal node.

We can combine multiple questions to ask even more complex questions. In fact, the Exclusive Or node is an example for composing two questions.


Now that we know how to ask questions and compose them into more complex queries, we can move on to make decisions based on the answers.
More about it in the following article...

© All rights reserved.