sexta-feira, 16 de maio de 2014

Logical Operators and Boolean Expressions

Logical operators perform an operation between boolean values ​​and return a boolean value (true or false) as a result of the operation.

Expressions using boolean values ​​and logical operators are known as boolean expressions.

The main logical operators are: OR, AND, NOT.

The logical OR operator returns "true" if any of the input values ​​are "true". The table below shows the results of an OR operation for different input values.

This type of table that shows the possible outcomes of a boolean expression is known as "truth table".

Imagine a game that ends when the player's energy or time are less than zero. The example below tests this condition and prints "GAME OVER" on the screen if the condition is true.

Click to enlarge

The logical AND operator returns "true" only if all inputs are "true". The truth table of the AND operator is the following:

The following example simulates a simple decision of an Enemy in a game. If the Enemy is low on ammo (LowAmmunition variable) and the player is nearby (PlayerIsNear variable) then the Enemy decides to run away.

Click to enlarge

The logical NOT operator receives only one input value and the result will be the reverse value:

Imagine that the player has a shield that can be activated and deactivated using the Spacebar. This image shows how we can toggle the value of the variable "Shield On" in a simple way using the NOT operator.

Next: A very simple game in Blueprints
Prev: Relational Operators and Branchs
Table of Contents