Learn

Chapter 04 Variables & Data Types

#032 Integer vs Float

Seven ammo, split between two players. Seven divided by two gives three, not three and a half.

  • 1:13 video
  • Written lesson
  • Beginner
Video lesson: Integer vs Float, 1:13

Whole numbers or decimals

Pick the wrong type, and the math breaks without any error.

  • An Integer stores whole numbers only: ammo, score, array indexes.
  • A Float stores decimals: health, speed, time in seconds.

Divide an Integer by an Integer, and the remainder is dropped: 7 by 2 is 3.

Plug an Integer into a Float pin, and the editor converts it for you. Going back, you choose: Truncate cuts the decimals, Round goes to the nearest whole number. 2.7 becomes 2, or 3.

Example: sprint stamina

Stamina starts at 1, and each step subtracts 0.3: 0.7, 0.4, 0.1, −0.2.

The Branch checks Stamina equals zero. That never happens, so the sprint never stops. Check less than or equal to zero instead, and the sprint stops right on time.

Floats are approximate

Add 0.1 ten times, and equals 1 still returns false. Compare floats with Nearly Equal, which accepts a small tolerance, or use less than and greater than.

In short

Integers count. Floats measure.

Covered in this lesson

  • Integer
  • Float
  • Truncate
  • Round
  • Nearly Equal
  • Branch

Newsletter

Development updates are coming to your inbox.

The newsletter opens soon. Until then, new lessons, tools and Beach Hunters progress appear first on our channels, linked below.