Welcome back! In our last lesson, you wrote your first line of code and used the print() function to send a "Hello, World!" message to the Godot Output console. This was a fantastic first step, giving you a way to get feedback from your script.
So far, you've only printed a static, unchanging piece of text. But games are all about change: a player's health goes up and down, a score increases, a character's name is displayed. To handle this kind of dynamic information, we need a way to store and manage data in our scripts. This is where variables come in.
Today, you'll learn how to declare variables using the var keyword. We will use them to store different kinds of data—like text and numbers—and then print those stored values to the console. This is a fundamental concept that forms the bedrock of all programming and will be essential for building your game.
What is a Variable?
Think of a variable as a labeled container or a box where you can store a piece of information. You give the box a name (the variable's name) and put something inside it (the variable's value). Later, you can look at what's inside the box, or even replace it with something new.
In game development, variables are everywhere:
player_healthcould store the player's current health.scorecould store the points the player has earned.player_namecould store the name the player chose.
For a quick, clear definition of what a variable is in programming, the following video provides an excellent summary.
Variable Basics (Updated) | Godot GDScript Tutorial | Ep 1.1
Watch the beginning of this video from Godot Tutorials, titled "Variable Basics".
Pay close attention to the definition of a variable as a "container that contains a value" from the start. Then, watch the explanation of how to create a variable in GDScript from this segment. You can stop after the first two examples are shown.
Declaring and Printing Your First Variable
Now that you have the concept, let's put it into practice. Declaring a variable in GDScript is straightforward. You use the var keyword, followed by a name for your variable, an equals sign =, and the value you want to store.
Open your player.gd script. We'll declare our first variable at the top of the script, just below the extends Sprite2D line. Code written here, outside of any function, is accessible to the whole script.
- At the top of your script, add this line:
var greeting = "Hello from a variable!" - Now, find your
_ready()function. Replace your existingprint("Hello, World!")line with this:print(greeting)
Your full script should now look like this:
extends Sprite2D
var greeting = "Hello from a variable!"
# Called when the node enters the scene tree for the first time.
func _ready():
print(greeting)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass
Let's break this down:
var greeting = "...": We created a new variable namedgreetingand stored the text "Hello from a variable!" inside it.print(greeting): We told theprint()function to look inside thegreetingcontainer and output whatever value it finds.
Now, run the scene (F6). Check the Output panel at the bottom of the editor. You should see your new message printed there!

The Output panel in Godot is where messages from the print() function appear. It's an essential tool for seeing what your code is doing.
It's crucial to notice that we wrote print(greeting) without quotation marks. This is because we want the value of the variable, not the literal word "greeting". This is a common point of confusion for new programmers, and the next video explains this distinction very clearly.
Variables and Functions - The Non-Coder's Guide to GDScript 02
Watch this short clip from "The Non-Coder's Guide to GDScript" by ACB_Gamez.
The presenter demonstrates the difference between printing the value of a variable versus printing a literal string of text. Watch the segment from this part of the video.
Storing Different Types of Data
Variables can hold more than just text. They can store numbers, true/false values, and many other types of data. Here are the most common basic types you'll use:
- String: A sequence of text, wrapped in quotes.
var player_name = "Link" - Integer (
int): A whole number.
var health = 100 - Float: A number with a decimal point.
var speed = 80.5 - Boolean (
bool): A value that is eithertrueorfalse.
var has_key = false
The article "Variables and Data Types in GDScript" provides a great reference for these concepts.
Variables and Data Types in GDScript — Godot Engine | Bsmarted
This article from Bsmarted gives a solid overview of variables and the different kinds of data they can hold.
Read the Explanation section to solidify your understanding of the var keyword. Then, review the different Basic and Complex Types. Finally, look at the collect_item function in the Examples section to see how a variable can be used with print().
Making Your print Statements More Informative
Printing just a single value is useful, but it's often better to add some context. You can combine text and variables in your print() statements to create more descriptive messages. This is incredibly helpful for debugging.
You can pass multiple items to the print() function by separating them with commas. Godot will automatically add spaces and convert numbers to text for you.
Let's try this in your script. Add a few more variables to the top of player.gd and modify the _ready() function to print them out.
Your final script for this lesson should look like this:
extends Sprite2D
var player_name = "Hero"
var health = 100
var has_sword = true
# Called when the node enters the scene tree for the first time.
func _ready():
print("Player character initialized.")
print("Name:", player_name)
print("Health:", health)
print("Has sword?", has_sword)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass
Run the scene again. This time, your Output panel should show four distinct, descriptive lines, telling you the state of your variables when the game starts. This practice of logging the state of your game is a skill you will use constantly as you build more complex projects.
Conclusion
You've now taken a significant step forward. By learning about variables, you've moved from writing static instructions to creating systems that can hold and manage dynamic data—the very essence of a game.
Here are the key takeaways from today's lesson:
- Variables are like labeled containers for storing data, declared with the
varkeyword (e.g.,var score = 0). - They are essential for tracking changing information in your game, like health, score, or inventory.
- You can print a variable's value using
print(variable_name), without quotes. - Variables can hold different data types, including text (
String), whole numbers (int), decimal numbers (float), and true/false values (bool). - Combining text and variables in
print()(e.g.,print("Health:", health)) makes your debug output much clearer.
You now have two of the most fundamental tools of programming under your belt: printing output and storing data. In our next lesson, we will make sure your project is properly saved and configured. After that, we'll begin using variables to bring your character to life by controlling its movement with player input.