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
EasyThe 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 coinsDo 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
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsInteger DivisionModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators10Functions
Declare a FunctionArgumentsReturnReturn TypesRecap - Sigma FunctionRecap - Validation FunctionDefault Values5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Ternary OperatorPractice on your own: Online GDScript compiler