World 1-1 The Fundamentals : GDScript intro

Part 11: int vs float in GDScript

A quick, beginner-friendly guide to the difference between int and float in GDScript, with simple examples to help you choose the right one.

🔢 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.

gdscript
1
2
3
4

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.

gdscript
1
2
3
4

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

Featureintfloat
TypeWhole numberNumber with decimals
Examples-1, 0, 42-1.5, 0.0, 3.14
Use casesScore, health, countSpeed, time, scaling
Memory & speedSlightly fasterSlightly more flexible
Auto-detectionYesYes

🧪 Mixing Them Together

If you mix an int and a float in a calculation, GDScript usually upgrades the result to a float.

gdscript
1
2
3
4

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 int for clean counts or identifiers.
  • Use float when 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.

SightlessDev