Learn To Code In C++ For Unreal Engine – Math Operations With Variables

Table of Contents

Learn To Code In C++ For Unreal Engine – Math Operations With Variables

Reading Time: < 1 minute
Level: Beginner
Version: Visual Studio 2019 Community Edition

Help Others Learn Game Development

In the lecture about variables we learned that we have three types of number variables, two are decimal point variables and one is a whole number variable.

It’s only logical to think that you can perform mathematical operations with those variables, and you are correct.

The Scariest Part Of Programming, Well, Not So Much

The most common question that I get from someone who want’s to learn how to code is “Do I need to be good at math to learn how to code?” And I totally understand where this is coming from.
 
Because the internet is full of forums where people “give” their take on programming and in most of them they mention how you need to know math to learn how to code.
 

The fact is that you only need to know basic operations like addition, subtraction, multiplication and division.

If you can perform those basic math operations on numbers from 1 to 100, you know more than enough math to learn how to code.

Not convinced?

Let’s take a look at some some examples.

Addition

Inside the main function in your script write the following lines of code:

				
					float Health = 50.0f;
float HealValue = 2.0f;

std::cout << "The remaining health is: " << Health + HealValue;
				
			

Run the program and you will see this printed in the console output:

Image 1

And there you go. It’s as simple as adding two numbers together.

We can also create a third variable and store the calculated numbers inside that variable:

				
					float Health = 50.0f;
float HealValue = 2.0f;

float NewHealth = Health + HealValue;

std::cout << "NewHealth Value Is " << NewHealth;
				
			

If we print this to the console you will see the same output from the previous example.

One thing to note is that we are using floats for current calculation, the same way we can use doubles or integers.

We can add the following lines of code:

				
					int a = 2;
int b = 3;

int sum = a + b;

std::cout << "The sum of a and b is: " << sum;
				
			

When you run this application you will see “The sum of a and b is: 5” is printed in the console output.

Subtraction

Moving forward with the basic math operations the next one on the line is subtraction.
 
We can rewrite the code we wrote previously:
 
				
					int a = 2;
int b = 3;

int subtraction = a - b;

std::cout << "The result is: " << subtraction;
				
			

If you run the application you will see that the result that is printed is 1. 

The result of a subtraction can also be negative, which means if we change the values of a and b:

				
					int a = 5;
int b = 10;

int subtraction = a - b;

std::cout << "The result is: " << subtraction;
				
			
When you run the app you will see that the printed result is -5.
 

Multiplication

Moving forward the next operation is multiplication. So instead of subtracting the two numbers from the previous code we can just multiply them:
 
				
					int a = 5;
int b = 10;

int multiplication = a * b;

std::cout << "The result is: " << multiplication;
				
			

When you run the app you will see the result is 50. You can also multiply negative numbers:

				
					int a = -5;
int b = 10;

int multiplication = a * b;

std::cout << "The result is: " << multiplication;
				
			
And when you run the app you will see the result is -50. If you change the b variable to also be a negative number then the result will be a positive number.

Because when you multiply two negative numbers you get a positive result. These are basic math rules taught in elementary school.

Division

The last operation is division. If we change the values of a and b to:

				
					int a = 10;
int b = 5;

int division = a / b;

std::cout << "The result is: " << division;
				
			

When you run the app you will see the result is 2.

The same rules with negative numbers apply for division as well. In fact, the same rules apply to all operations that we saw so far.

One thing that you need to keep in mind when using division is, depending on your needs you will use different types of numbers.

What do I mean by this?

Let’s take a look at this example:

				
					int a = 5;
int b = 2;

int division = a / b;

std::cout << "The result is: " << division;
				
			
If you run this app you will see that the result is 2 and not the expected 2.5.
 
The reason for this is simple, integers are whole numbers and they can’t store decimal point numbers. So depending on your needs you would use floats instead of integers, or you can use casting.
 

Casting? What Is That?

From the previous example we saw that we can’t calculate decimal point numbers with integers. This is a big potential problem depending on the needs of our game.
 
One fix is to use floats to do the calculation, another is using casting. Casting is basically converting one variable type into another one. In this case we would convert an integer to a float.
 
If we rewrite the previous code to:
 
				
					int a = 5;
int b = 2;

float division = (float)a / (float)b;

std::cout << "The result is: " << division;
				
			
If we run the app now we will see the result printed to the console is 2.5.
 
The reason for that is because we used casting by adding (float) in front of the integer variables.
 
This is how casting is performed, by adding in parentheses the type of the variable in which you are casting to, in front of the variable that is being cast.
 
If you try to do this without the (float), like this:
				
					float division = a / b;


				
			

The result would still be 2, because you didn’t specify that you want the variables a and b to be treated as floats, so they will be treated as integers even though the result variable division is a float.

Conclusion

This is the scary math that you need to know to be able to learn how to code, it’s simple as calculating two numbers.
 
Now I am not going to lie to you, because we are going to create games we are going to perform some complex operations like calculating the angle between actors, or their distance.
 
But the good news is that we don’t need to know the formulas for all of this, because everything is already pre-built for us and we just use the already created functionality.
 
So the most complicated math that we will do is the one you saw in this example.

Where To Go From Here

In this post you learned how to perform math operations with variables, and you saw that math in programming is not scary like the majority of people think.
 
To continue your learning journey you can watch our Introduction To Functions Lecture which is the next lecture in this tutorial series.

Leave a Comment