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)) |
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
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
** Debug Process Started ** OpenGL ES 2.0 Renderer: Mesa DRI Intel(R) UHD Graphics 620 (Kabylake GT2) 2 ** Debug Process Stopped **
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
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:
If side
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
** Debug Process Started ** OpenGL ES 2.0 Renderer: Mesa DRI Intel(R) UHD Graphics 620 (Kabylake GT2) 5 ** Debug Process Stopped **
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
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