Input
Part of the Fundamentals section of Coddy's GDScript journey. Lesson 34 of 92.
So far every value came from the code itself. Most programs get values from outside, for example from the person using them. These values are the program's input.
In this course, a program reads input with OS.read_string_from_stdin(). Each call reads one line of input and returns it as a String, without the line break at the end:
var player_name := OS.read_string_from_stdin()
print("Hello, " + player_name)With the input Ada, this prints Hello, Ada.
Each call reads the next line, so a program that needs two lines of input calls it twice:
var first := OS.read_string_from_stdin()
var second := OS.read_string_from_stdin()The result is always a String, even when the line is a number. If the input is 42, the variable holds the text "42", and "42" + "1" is "421". The next lesson turns that text into a number.
Challenge
EasyWrite a program that reads a player's name from the input and greets them.
Print Hello, followed by a space and the name. For example, if the input is Ada, the program prints:
Hello, Ada- Read the name with
OS.read_string_from_stdin()and store it in a variable. - Print the greeting.
Try it yourself
extends Node
func _ready():
# Read the name, then print the greeting
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