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.
The building blocks for asking questions are comparing one thing to another.
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...
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.
If we want to ask multiple questions upon the same values, we can use the Comparison node, which answers all questions at once.
Now that we have the basic building blocks, we can combine them into more complex questions.
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.
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.
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.
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...