Home Blog Gaming

GDScript Operators: Math, Logic, Comparison

Godot Tutorials > GDScript Operators
Computer programming is based on mathematics and logic. Game development is especially known for being math-heavy. Don't get scared! You do not need to know advanced calculus to be a good gaming programmer. However, you do need a firm grasp of the basics. The next few tutorials will give you a strong logical foundation, enabling your success as a game developer (and a computer programmer).

Math Operators

Math operators are used to perform simple mathimatical calculations. You are probably familiar with most of them.

Mathematical Operators Video




Operator Name Symbol Example
Addition + var total = price + tax
Subtraction - var difference = score1 - score2
Multiplication * var stream_time = time * multiple
Division / var payout = loot / players
Remainder % var leftovers = pizza % people
Square Root sqrt var c = sqrt ((a * a) + (b * b))

Remainder

You are probably familiar with addition, subtraction, multiplication, and division. However, you may not be familiar with the remainder. Think back to 3rd-grade math. The remainder is the amount left over after two numbers are divided.

Let's say you have a pizza party. You ordered a total of 32 slices of pizza. Ten guests show up. You give each guest the same amount of pizza. How many slices are left?

Mouse Hover over the blue text in the description to highlight the code.

extends Node
# number of people
var people = 10
# number of slices
var pizza = 32
# slices left over
var leftovers = pizza % people

# call functions under func_ready()
func _ready():
  # output the amount of left over slices
  print (leftovers)

➼ Number of people
➼ Number of Slices
➼ Leftovers
➼ Output Leftovers


Output

** Debug Process Started **
OpenGL ES 2.0 Renderer: Mesa DRI Intel(R) UHD Graphics 620 (Kabylake GT2)
2
** Debug Process Stopped **

Square Root

The square root is the inverse of square, like addition is the inverse of subtraction. When you square a number (x2) you multiply the number by itself. So 3 2 equals 9. The square root of a number is what number multiplied by itself gives you that number. So the square root of 9 is 3.

Think back to Geometry class. For a right triangle, the length of the hypotenuse squared (the longest side of the triangle which is opposite the right angle) is equal to the sum of the other two sides squared. This is called the Pythagorean theorem: a2 + b 2 = c 2

Right Triangle - Sides a, b, c

If side a has a length of 4 and side b has a length of 3 than side c has a length equal to the square root of 4 2 + 3 2.

extends Node
var a = 4
var b = 3
var c = sqrt ((4*4) + (3*3))

func _ready():
    print (c)

➼ Length of side a
➼ Length of side b
➼ Length of side c
➼ Output the Length of side c


Output

** Debug Process Started **
OpenGL ES 2.0 Renderer: Mesa DRI Intel(R) UHD Graphics 620 (Kabylake GT2)
5
** Debug Process Stopped **

Comparison Operators

Comparison operators are used to compare values.

Operator Name Symbol Example Explained
Less Than < a < b a is less than b
Greater Than > b > c b is greater than c
Less than or equal <= a <= c a is less than or equal to c
Greater than or equal/td> >= b >= c b is greater than or equal to c
Equal == x == y x is equal to y
Not Equal != a != b a is not equal to b

We will look at code examples in our lesson on GDScript if statements


Logic Operators

Logic Operators are used to write logic. We use them to describe when code will execute.

Operator Name Symbol Example Explained
And && color && number both color and number
Or || steak || fish Either steak or fish
Not ! !fish not fish

We will look at code examples in our lesson on GDScript if statements

Homework

Write GDScript code to perform the following calculations
  1. Rhonda played a racing game 3 times. Her scores were: 42,000, 66,000, 99,000. What is Rhonda's average score?
  2. Juan went bought a TV on sale for $99. There is a 9% sales tax. What is the total amount that Juan spent?
  3. Hanah got paid $500 today. She already spent $150 of it on her phone bill. How much money does Hanah have left?
  4. The length of a right triangle's hypotenuse is 900 inches. The other sides are equal in length. What is the length of the other sides?
  5. Katie is drinking a 12 once cup of tea. She has drank 2/3 of the tea. How much tea is left?

Want Commander Candy to Grade your Homework?

Donate




Learn PHP on Linux with Commander Candy
Linux Gaming Blog - Coding Commanders
Palm Beach Techie - Diverisity in Tech
Linux Battle Card RPG - Gänseblümchen
Overland on Linux Preview by Hatnix
Coding Commanders YouTube
Coding Commanders Twitter
Learn HTML on Linux with Commander Candy
Linux Gaming King Hatnix on Twitch
Linux (Ubuntu)+ NodeJS + Postgres
LAMP - Linux, Apache 2, MySQL, PHP
Sexy Linux T-shirt
ZED Linux Review by Hatnix
Linux Gaming Blog YouTube Introduction
Previous Lesson | Next Lesson