🔢 int vs float in GDScript
When coding in GDScript, numbers are everywhere—from tracking player health to calculating movement speed. But not all numbers are created equal. In this post, I’ll walk you through the two most common types: int and float.
🧱 int: Whole Numbers
An int (short for integer) is a number without a decimal point. It’s perfect for things like counting lives, keeping score, or looping through a list.
var lives = 3
print("Number of lives (int):", lives)
🌊 float: Numbers with Decimals
A float is a number that can include a fractional part. This makes it ideal for smooth movement, percentages, and more precise values.
var pi = 3.14
print("Value of pi (float):", pi)
You can also use negative values, like -2 (int) or -2.75 (float). GDScript handles both with no problem.
🔍 Quick Comparison
| Feature | int | float |
|---|---|---|
| Type | Whole number | Number with decimals |
| Examples | -1, 0, 42 | -1.5, 0.0, 3.14 |
| Use cases | Score, health, count | Speed, time, scaling |
| Memory & speed | Slightly faster | Slightly more flexible |
| Auto-detection | Yes | Yes |
🧪 Mixing Them Together
If you mix an int and a float in a calculation, GDScript usually upgrades the result to a float.
var result = 2 + 3.5
print(result) # Outputs: 5.5 (float)
This is super useful in real game logic where precision matters—like gradually increasing speed or smoothing out animations.
🧠 Final Tips
- Use
intfor clean counts or identifiers. - Use
floatwhen you need smooth or fractional values. - You don’t need to explicitly declare the type—GDScript figures it out for you.
Understanding the difference between int and float may seem small, but it’s foundational. These little building blocks help you write cleaner logic and avoid bugs later.