In game development and programming in general, you will stumble upon situations where you need to make a decision: pick an item or not, shoot the enemy or not, move left or right, and so on.
And based on the decision you make the gameplay workflow will continue to run. Well, these decisions are made using conditional statements.
The Anatomy Of A Conditional Statement
The most famous conditional statement is called the if statement, and it goes like this:
if(condition)
{
// code to execute if condition is true
}
The statement starts with if and inside the parenthesis we put the condition that we are trying to evaluate.
The condition needs to be true in order for us to execute the code that’s between {}
This is where booleans come in. If you remember when we introduced booleans in the first post I mentioned that they are used for conditional statements because their value can either be true or false.
If we rewrite this line of code:
if(true)
{
std::cout << "This statement is true";
}
And if you run the app now you will see “This statement is true” printed in the console.
We can test a more concrete example inside the if statement:
if(3 > 2)
{
std::cout << "3 is greater than 2";
}
Of course, the code above will always print “3 is greater than 2” because this example is hardcoded.
Let’s see a real world example:
int Health = 100;
if(Health < 0)
{
std::cout << "Player died game over";
}
Here, we have a Health variable that we are testing if it’s lower than 0, and this is something that we will do a lot when we start creating our games.
If this is true we are printing “Player died game over” but in our game we will either show some UI to inform the game that the player has died, or we will simply restart the game or something else that makes sense.
One thing that you noticed so far we are comparing numbers to numbers. So far we performed two tests to see if the number is less than a number and if a number is greater than a number.
Here are all comparisons that we can test with numbers:
// if a is GREATER THAN b
if (a > b)
{
}
// if a is GREATER THAN or equal to b
if (a >= b)
{
}
// if a is LESS THAN b
if (a < b)
{
}
// if a is LESS THAN or equal to b
if (a <= b)
{
}
// if a is equal to b
if (a == b)
{
}
These comparisons are the same ones we learned to perform in math. We can test if a number is greater than a number, greater than or equal to, less than number, less than or equal to, and if the number is equal to a number.
You will notice that we are using the double equal sign “==” to perform the equals test, this is because we use single equal sign “=” to assign a value to a variable, so we can write something like:
if (a = b)
{
}
This will not work. Instead, when you want to compare the two numbers if they are equal you use the double equal sign ==.
What If The Statement Is Not True?
So far we tested only for one condition using the if statement, but what happens if that condition is not true?
For example:
int a = 3;
int b = 5;
if(a > b)
{
std::cout << "a is greater than b";
}
In this case we are testing a which has a value of 3 if it’s greater than b which has a value of 5. Obviously this condition is not true because 3 is not greater than 5, so if we can’t execute the code inside the if statement, how can we control the flow of our game?
For this we can use the else statement:
int a = 3;
int b = 5;
if(a > b)
{
std::cout << "a is greater than b";
}
else
{
std::cout << "a is NOT greater than b";
}
In this case, if the first condition of a > b is not true, then the code inside the if statement will not be executed, instead we will proceed to execute the code inside the else statement.
This is how we can have two options in our game that the player can chose, and based on his decision you will continue the game in different way, for example you can spawn 5 enemies that will attack the player instead of 10.
But this if else condition is also limited because it allows us to only test two conditions, maybe we have multiple choices that we can choose from.
Well, there is a solution for that as well:
int a = 3;
int b = 5;
if(a > b)
{
std::cout << "a is greater than b";
}
else if(b > a)
{
std::cout << "b is greater than a";
}
else if(a == b)
{
std::cout << "a is equal to b";
}
We can perform multiple condition tests by using else if statements, and based on the outcome we can execute specific lines of code.
Here is another more concrete example that we will encounter in our game development journey:
int Mana = 10;
if(Mana >= 90)
{
// perform special attack 1
}
else if(Mana >= 50)
{
// perform special attack 2
}
else if(Mana >= 20)
{
// perform special attack 3
}
else
{
// perform special attack 4
}
Based on the Mana value and the condition that we are testing we are going to perform different attacks in our game.
This is one of the ways how you can give a power boost to your game character – the lower his health is, the better and stronger attack he can perform.
The AND & OR Operators
We can also test for multiple conditions inside the if statement:
int Health = 50;
if(Health > 10 && Health < 60)
{
// execute code
}
Inside the if statement above we are testing if the Health variable is greater than 10 AND if its less than 60.
In this case when we use the && (and) operator both conditions needs to be true in order for us to execute the code inside this if statement.
For example, if the Health value is 70, this if statement will not be true, because even though 70 is greater than 10, it is not less than 60, so we will not execute the code inside this if statement.
Or if the value of Health is 10, still it will not be true, because even though 10 is less than 60, it is not greater than 10, since 10 is equal to 10.
And you are not limited to testing only two conditions, you can test multiple conditions:
if(Health > 10 && Health < 60 && EnemyIsAlive && Power > 50)
{
// execute code
}
Besides testing if the Health is greater than 10 and less than 60, we are also testing if the EnenyIsAlive and if the Power is greater than 50, all these conditions needs to be true for the whole if statement to work.
If Health is greater than 10 and less than 60, and the enemy is alive(EnemyIsAlive == true), but Power is not greater than 50, then this whole if statement is not true.
We can also test if at least one of the conditions is true:
if(Health > 10 || Power < 70)
{
// execute code
}
Here we are testing if Health is less than 10 or if the Power is greater than 70.
In this case if Health is 15, and Power is 71, the if statement will be true, because when we use the || (or) operator at least one of the conditions needs to be true for the whole if statement to work.
And same as with the && operator, you can also test multiple conditions:
if(Health > 10 || Power < 70 || LevelTimer > 300 || EnemyCount < 20)
{
// execute code
}
If at least one of these conditions is true then the whole if statement is true and the code inside will execute.
Switch And Case Statement
There is another conditional statement called switch and case that we can use to evaluate conditions:
int Health = 5;
switch(Health)
{
case 1:
// execute code
break;
case 3:
// execute code
break;
case 4:
// execute code
break;
case 5:
// execute code
break;
}
When testing a condition with switch and case, you pass the variable you want to test to switch, and inside the body of switch you use cases to test for conditions.
Every case acts as a test for the value, so case 1 is testing if the Health variable has a value of 1, case 3 is testing if the value is equal to 3, and so on.
If one of the cases is true, then the code inside that case will execute and then it will hit the break statement and exit outside the switch and case.
This means that cases below will not be tested if one of the cases above evaluates to be true.
For example:
int Health = 5;
switch(Health)
{
case 1:
// Health Not EQUAL to 1
break;
case 5:
// Health Equal to 5
// Code here will execute
// Then the workflow will
// exit outside the switch and case
break;
case 6:
// This condition is not tested since
// the case above evaluated to be true
break;
case 8:
// This condition is not tested since
// the case above evaluated to be true
break;
}
To explain how switch and case works, let us re-write the same condition using if else:
int Health = 5;
if(Health == 1)
{
}
else if(Health == 3)
{
}
else if(Health == 4)
{
}
else if(Health == 5)
{
}
This is how we would test for the same condition above using if else statements.
So the purpose of both statements are to test for conditions, and you will sometimes use one over the other.
Every time I say this I automatically get a question “when should I use if and when should I use switch and case?”
And the answer is there is no general rule where to use if where to use switch and case, the feeling for that comes with practice and going through multiple examples.
That is the reason why I cover a lot of topics and create a lot of games in my tutorials, because when you see something in action, then you tend to understand it more.
But for now, what we covered here is enough because this is the basis for everything that will come when we start creating our games.
22 thoughts on “Learn To Code In C++ For Unreal Engine – Conditional Statements”
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.
Também tenho o seu livro marcado para ver coisas novas no seu blog.
1
There are definitely a number of particulars like that to take into consideration. That could be a great level to deliver up. I provide the ideas above as basic inspiration however clearly there are questions like the one you convey up where a very powerful factor shall be working in honest good faith. I don?t know if greatest practices have emerged around issues like that, but I am certain that your job is clearly recognized as a good game. Each boys and girls feel the affect of only a moment’s pleasure, for the remainder of their lives.
The place did you get the file from?
併設されている国際映画見本市(フィルム・ カンヌ国際映画祭(カンヌこくさいえいがさい、仏:Festival International du Film de Cannes)は、1946年にフランス政府が開催して以来、毎年5月(1948年、1950年は中止)にフランス南部コート・ ご契約のクルマが、故障やバッテリー上がり等の車両自体に生じたトラブルにより走行が出来なくなった場合に、30分程度で対応可能な応急対応を行います。
Introducing to you the most prestigious online entertainment address today. Visit now to experience now!
For many purposes, like glass bottles or eyewear, polymer glasses (acrylic glass, polycarbonate or polyethylene terephthalate) are a lighter various to conventional glass.
Thanks for sharing. I read many of your blog posts, cool, your blog is very good.
Thanks for sharing. I read many of your blog posts, cool, your blog is very good.
動画を撮ることによって本当は自分でこんなに音が出て、こんなに口開けてもの食べてるんだ!映画だけで約21万本以上もの作品が見放題な上に、漫画もアニメも盛りだくさん。私も4年以上愛用していますが、生活が本当に便利になりました。平和国ロベッタ史上最年少の15歳という若さで魔法使い最高位「魔女」となった天才少女で、髪の色から『灰の魔女』という魔女名(二つ名)を持つ。 ここでは、企業がメタバースを活用して展示会を開催する2つの方法について解説します。 できるだけ自然な方法で聞いてあげたいですね。虫(蜂とか)や生き物の心配も山に比べたら全然しなくていいので気兼ねなく楽しめると思う。相手の体調心配するかのように聞いてあげます。
Your article helped me a lot, is there any more related content? Thanks!
Hey, I think your blog might be having browser compatibility issues. When I look at your blog site in Opera, it looks fine but when opening in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up! Other then that, terrific blog!
Saved as a favorite, I really like your blog.
Your blog is a great source of positivity and inspiration in a world filled with negativity Thank you for making a difference
漫画版では完全な黒悪魔で、小鬼の力で死者を蘇らせて町を占拠し、真吾たちを脅迫してソロモンの笛を奪い、さらに大口童子との戦いで倒れたメフィスト二世を連れ去ってしまう。漫画版では楽器を鳴らす他に、中国に伝わる術「うそぶき」を使い、死者を蘇らせて町を混乱に陥れた。手に持っている楽器をよく鳴らしている。山一證券株式会社(やまいちしょうけん、英: Yamaichi Securities Co., Ltd.)は、かつて存在した日本の大手証券会社。左側には大の字で寝る松本副隊長。
Your article helped me a lot, is there any more related content? Thanks!
Hello, i read your blog occasionally and i own a similar one and i was just curious if you get a lot of spam comments? If so how do you reduce it, any plugin or anything you can suggest? I get so much lately it’s driving me mad so any assistance is very much appreciated.
pokračujte v pěkné práci, kolegové.|Když máte tolik obsahu a článků, děláte to?