Menu
CoddyTech

Output With Variables

Part of the Fundamentals section of Coddy's GDScript journey. Lesson 32 of 92.

To print the value of a variable, pass the variable to print() without quotation marks:

var coins := 25
print(coins)

This prints 25. With quotation marks, print("coins") prints the word coins.

print() accepts several values separated by commas. It prints them one after another on the same line with no space between them, so put the spaces inside the text:

var player_name := "Ada"
var level := 7
print("Player: ", player_name)
print("Level ", level, " reached")

This prints Player: Ada on the first line and Level 7 reached on the second.

prints() works like print() but puts a space between the values: prints("Level", level) prints Level 7.

You can also join text with +. Both sides must be strings, so turn a number into a string with str():

var score := 120
print("Score: " + str(score))

Without str(), "Score: " + score is an error: GDScript cannot add a String and an int.

challenge icon

Challenge

Easy

The program stores a player's name, level and coins in three variables. Use the variables to print exactly these two lines:

Player: Ada
Ada reached level 7 with 250 coins

Do not type the values Ada, 7 and 250 in your print() lines: print the variables player_name, player_level and player_coins.

Try it yourself

extends Node

func _ready():
	var player_name := "Ada"
	var player_level := 7
	var player_coins := 250
	# Print the two lines using the variables
	pass
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals

Practice on your own: Online GDScript compiler