P1.3 — Types of Data¶
What you'll learn
- That every value has a type — the kind of data it is.
- The four everyday types: whole numbers (
int), decimals (float), text (String), true/false (bool). - Why the type matters: the computer treats text and numbers differently.
- How to turn one type into another when you need to (
str,int,float).
How it applies
- Text and numbers are not interchangeable. The characters
"5"(text) and the number5look alike to you and are unrelated to the computer: one can be glued to other text, the other can be added. Confusing them is behind a huge share of beginner errors and "why won't these combine?" moments. - Whole versus decimal is a real choice. A count of lives is a whole number; a price or a
position is a decimal. Picking the wrong one gives
3lives where you meant3, or loses the.5off a measurement. - True/false is the fuel for decisions. The next chapter's
ifruns onboolvalues. Knowing that "is the player alive?" is a value —trueorfalse— is what makes decisions click. - Data from outside arrives as text. Anything typed by a user or read from a file comes in as a
String; using it as a number means converting it first. Forgetting to convert is a classic bug.
Concepts¶
Every value has a type¶
A type is the kind of data a value is. The computer needs to know the kind, because it handles each kind differently — you can add two numbers, but "adding" two pieces of text means something else, and "adding" text to a number means nothing at all. Four types cover almost everything you will write in this book:
int— an integer: a whole number, no decimal point.0,7,-3. For counting: lives, score, number of items.float— a number with a decimal part.2.5,3.14,0.0. For measuring: price, position, speed. (The name is short for "floating-point"; just read it as "decimal number.")String— text, always written inside quotes."Vex","Game Over","5". For words, names, messages.bool— a true/false value, writtentrueorfalse. For yes/no facts: is the player alive, is the door open. (Short for "boolean.")
var lives := 3 # int — a count
var price := 2.5 # float — a measurement
var name := "Vex" # String — text, in quotes
var is_alive := true # bool — a yes/no fact
When you create a variable with :=, GDScript notices the type of the starting value and remembers it.
You do not have to state the type yourself in this book — just know that the value has one, and which
one.
Why the type matters: "5" is not 5¶
This is the idea to nail down. The text "5" and the number 5 are different values of different
types that happen to look similar on screen:
var number := 5 # int
var text := "5" # String — note the quotes
print(number + 3) # 8 — adding numbers
Adding numbers gives a number. But "adding" the text "5" to the number 3 is not addition at all —
the computer has no rule for gluing a number onto text — and it produces an error. The quotes are the
whole difference: with quotes it is text; without, it is a number. When a value is in quotes, it is a
String, full stop, even if every character inside is a digit.
Combining text with numbers: convert first¶
You often do want to show a number inside a message — "Score: 100". Since you cannot glue a number
directly onto text, you turn the number into text first with str(...):
str(score) produces the String "100", which can be joined to other text with +. The
conversions go both ways:
str(value)→ turn anything into text (aString).int("42")→ turn the text"42"into the number42.float("2.5")→ turn the text"2.5"into the decimal2.5.
Example
The error and its fix, side by side:
var score := 100
print("Score: " + score) # ERROR — can't glue a number onto text
print("Score: " + str(score)) # Score: 100 — convert the number to text first
And the reverse, when text needs to become a number:
var typed := "42" # text (e.g. something a player typed)
print(typed + 8) # ERROR — "42" is text, not a number
print(int(typed) + 8) # 50 — convert the text to a number, then add
The pattern is always the same: match the types before combining them. Text joins with text;
numbers add with numbers; convert across the boundary with str, int, or float.
Walkthrough¶
Use your P1.1 script setup.
- Create one variable of each type: an
int, afloat, aString, and abool.printeach and confirm the values appear. - Make
var n := 5andvar t := "5". Printn + 3(works, gives8). Then tryt + 3and run — read the error. The two5s are different types. - Fix a number-in-text message:
print("You have " + str(n) + " coins."). Confirm it reads cleanly. - Convert the other way: make
var typed := "42", thenprint(int(typed) + 8)and confirm50. Remove theint(...)and watch it error again.
Optional sanity check
Print 3 / 2 and then 3.0 / 2. The first gives 1 (two whole numbers, so the answer is kept
whole and the remainder is dropped); the second gives 1.5 (a decimal is involved, so the decimal
answer is kept). You do not need the full rule yet — just notice that whether values are int or
float changes the result. The next book explains this precisely.
Self-check quiz¶
Q1 — Which type best fits the number of arrows a player is carrying?
A. float, because all numbers are decimals.
B. int, because it is a whole-number count.
C. String, because it could be shown on screen.
D. bool, because the player either has arrows or not.
Reveal answer
B. A count of arrows is a whole number, so int fits. A is wrong — not all numbers are
decimals, and a count is never fractional. C confuses displaying a value with its type (you
can show an int without making it text). D collapses a count into a yes/no, losing the actual
number.
Q2 — What is the difference between 5 and \"5\"?
A. None; they are the same.
B. 5 is a number (int); \"5\" is text (String) — the quotes make it text, so they behave differently.
C. 5 is text and \"5\" is a number.
D. \"5\" is just a slower way to write 5.
Reveal answer
B. Quotes make a value text: "5" is a String, while 5 is a number. They look alike but
are different types — 5 can be added, "5" can be joined to other text. A misses the type
difference (the core point). C reverses it. D treats them as the same value, which they are not.
Q3 — var score := 100. Which line correctly prints Final score: 100?
A. print("Final score: " + score)
B. print("Final score: " + str(score))
C. print("Final score: " + int(score))
D. print(Final score: 100)
Reveal answer
B. You cannot glue a number directly onto text, so convert it with str(score) to get the
String "100", which joins to the message. A errors (number + text). C converts to an int,
which still cannot be glued to text. D is missing quotes around the text and is not valid.
Integration question¶
Q4 — open
A player types their age into a box and the program receives it as age_text. The program then
needs to (a) show the message You are 30 years old. and (b) compute the age ten years from now.
Using types and conversion, explain what type age_text arrives as, what goes wrong if you use it
directly in each task, and what conversions make each task work.
Reveal expected answer
Anything typed by a user arrives as text — age_text is a String (e.g. "30"), even
though it looks like a number. Task (a), the message: joining text to text is fine, so
"You are " + age_text + " years old." actually works as text — but if you instead had the
age as a number and tried "You are " + age_number, that would error, and you would convert
with str(...). The rule: to put a number inside a message, make it text first. Task (b), the
math: using age_text + 10 fails, because "30" is text and you cannot add a number to text;
you must convert it to a number first with int(age_text), giving 30, then int(age_text) +
10 correctly yields 40. The unifying idea: a value's type determines what you can do with
it, data from outside is text by default, and str / int / float are how you move a value
across the text-number boundary so the operation you want (joining vs adding) is valid.