Mobile Game 02 · Give the night guide a name

Museum of Lost Lights · Lesson 02 of 50 · Beginner · About 30 min

The museum’s little lamp needs a name for its night shift badge. Give it one and let the editor change it between rooms.

A gouache painting of an Ocean Gallery with glass jellyfish exhibits, fossils and a deep teal floor.
What you’ll make Use an editable variable, a constant and a string to build a greeting that changes without changing the function.

Download the projects from the course page. In Godot’s Project Manager, choose Import, select project.godot, and use Run Project (F5). Start from checkpoints/01 when the lesson builds on prior work, or copy the checkpoints/02 folder to try this lesson on its own. Read the file names in the steps before editing.

Store the gallery. A constant holds the room name. → Pick the guide. The Inspector edits the exported variable. → Join the pieces. Plus signs join text in order. → Show the badge. The game and the log use it.
Store the gallery → Pick the guide → Join the pieces → Show the badge

Build it step by step

Language and tools in this lesson const, @export, var, String, =, +, Inspector, strings.

  1. Open Lesson 02 and check the root

    In Godot’s Project Manager, import Lesson 02. Open main.tscn, then select its root Control. Find the Inspector on the right and the main.gd file under FileSystem.

    Why it works The root owns the script and its editable properties. The Inspector lets you see those properties before the scene is run.

  2. Choose the piece that should stay fixed

    Read the GALLERY line. Put your finger on the name, the colon, its type and the quoted words. Leave the hall set to Ocean Gallery for the first run.

    const GALLERY: String = "Ocean Gallery"

    Why it works const makes a constant. GALLERY is a name for one value that the script cannot assign again. String means a piece of text. The capital name is a style convention that makes constants easier to find.

  3. Make a name someone can change

    Read the exported variable. Save main.gd, click the root Control again, and change Guide Name in the Inspector to Nori. Save main.tscn and run the scene.

    @export var guide_name: String = "Lumi"

    Why it works The @export instruction exposes this variable in the editor. var means the value can change. The quoted word Lumi is the default for a new node using this script.

  4. Build the sentence in order

    Read the greeting from left to right. Point to each fixed piece inside quotes and the two names between them. Notice the spaces after Hello, and the before the gallery name.

    badge.text = "Hello, " + guide_name + "! Welcome to the " + GALLERY + "."

    Why it works The + between strings joins them in order. Space characters inside quotes count. The phrase uses the current value of guide_name wherever that name appears.

  5. Compare two runs

    Set Guide Name to Lumi in the Inspector and run. Stop and set it to Nori again. Compare both the screen and Output for the second run.

    print(badge.text)

    Why it works The print reads the completed badge.text, so the game view and developer log use the same result.

Watch the values change

  1. Leave the default name guide_name is Lumi
    What you see. The game shows a greeting for Lumi.
  2. Change Guide Name to Nori An Inspector change is stored on this scene
    What you see. The next run reads Nori from guide_name.
  3. Read the log print reads the finished badge text
    What you see. The log and sign show the same words.

Predict before you run What would you read if guide_name were Marina but the gallery stayed unchanged?

Reveal the answer

Hello, Marina! Welcome to the Ocean Gallery. Only the name comes from the editable variable.

Complete checkpoint

With the default badge, the game and Output both show Hello, Lumi! Welcome to the Ocean Gallery.

main.gd

extends Control

const GALLERY: String = "Ocean Gallery"
@export var guide_name: String = "Lumi"

func _ready() -> void:
	var badge := Label.new()
	badge.position = Vector2(52, 54)
	badge.add_theme_font_size_override("font_size", 36)
	badge.text = "Hello, " + guide_name + "! Welcome to the " + GALLERY + "."
	add_child(badge)
	print(badge.text)

GDScript line by line

Follow the line numbers, including the indent at the start of each line. Find the code the game will run first, then look for the function that reacts when you tap or change something.

Read main.gd line by line
  1. extends ControlThe scene root behaves like a Control, the kind of Godot node used for a game’s text, controls and other interface pieces.
  2. const GALLERY: String = "Ocean Gallery"Store Ocean Gallery in a constant named GALLERY. The greeting will read this fixed text every time it runs.
  3. @export var guide_name: String = "Lumi"Set the default guide name to Lumi and let the Inspector store a different value for this particular scene.
  4. func _ready() -> void:Godot automatically calls this setup function after bringing this node and its children into the scene. The code below it prepares the first frame.
  5. var badge := Label.new()Create the badge as a Label node, then keep its reference while you set its position, font and text.
  6. badge.position = Vector2(52, 54)Use two numbers to give this control its position, measured right and down from the root.
  7. badge.add_theme_font_size_override("font_size", 36)Set a larger or smaller font here without changing the typography on the other controls.
  8. badge.text = "Hello, " + guide_name + "! Welcome to the " + GALLERY + "."Join fixed pieces of the greeting with the current guide name and the unchanging gallery name, leaving spaces where words meet.
  9. add_child(badge)Attach the finished badge to the scene so visitors can see it.
  10. print(badge.text)Read badge.text after building it. Using the existing property means the developer log matches the game screen.

Find and fix a bug

If changing the name in the file seems to do nothing, look at the Guide Name property on the root node. An Inspector value saved in the scene takes precedence over the script’s default. Edit it there, or use its revert arrow.

Try a change

Ages 10–12: Try a new lantern name with two words. Ages 13–16: Add another exported String for a night shift message and print a badge that uses both editable values.