Menu
CoddyTech

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 icon

Challenge

Easy

Write 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
  1. Read the name with OS.read_string_from_stdin() and store it in a variable.
  2. Print the greeting.

Try it yourself

extends Node

func _ready():
	# Read the name, then print the greeting
	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