7. Operators and arithmetic
func run() -> Array:
var points: int = 10
points += 5
points *= 2
var average: float = float(points) / 4.0
var remainder: int = 11 % 3
return [points, average, remainder, 2 ** 3]| Operator family | Meaning |
|---|---|
| + - * / % ** | Arithmetic, integer remainder, exponentiation |
| = += -= *= /= | Assignment and update |
| == != < <= > >= | Comparison |
| and or not | Boolean logic; and/or short-circuit |
| in, not in | Membership |
| is, as | Type test and object cast |
| & | ^ ~ << >> | Integer bit operations; useful for flags and masks |
Dividing two integers performs integer division. Use a float operand or explicit conversion when you need a fractional result. = assigns; == compares. GDScript has no ++ or --: use += 1 or -= 1.
Parentheses make mixed expressions clear. For a condition involving both and and or, group the part you intend to combine first. Do not use bit operators as substitutes for ordinary Boolean tests.
