Cruise Game 02 · Count the luggage orders

Midnight Deck Dash · Lesson 02 of 50 · Beginner · About 25 min

There are three bags waiting on the promenade. Help Sol turn the ship name and the delivery count into one useful dispatch message.

A straight teak promenade with a cream cabin wall on one side and a ship railing above the moonlit ocean on the other.
What you’ll make Store the ship’s name and update a count after a practice delivery.

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.

Give values names. A constant stores the ship name. A variable counts bags. → Set the sign. The text and number fill the gaps. → Update the count. Subtract one, then check Output.
Give values names → Set the sign → Update the count

Build it step by step

Language and tools in this lesson const, var, int, :=, assignment, format string, %s and %d, -=.

  1. Open the previous scene

    Keep your Lesson 1 project open or import its checkpoint from Project Manager. Double-click main.gd in the FileSystem panel.

    Why it works Your scene already knows how to display a Label. Now the text will depend on stored values.

  2. Give the ship a fixed name

    Add SHIP_NAME between extends Control and _ready. Copy the quotes and the colon-equals sign.

    const SHIP_NAME := "Starlight Voyager"

    Why it works const keeps the name fixed in this script. Godot infers a string from the starting value.

  3. Store the bag count

    Add orders_left on the next line. Include the colon, int, equals sign and starting value.

    var orders_left: int = 3

    Why it works A variable can change. The int hint expects a whole number, which fits counting bags.

  4. Fill in a status message

    Replace sign.text with the format line below. Read the values in the square brackets from left to right.

    	sign.text = "%s | Bags waiting: %d" % [SHIP_NAME, orders_left]

    Why it works %s receives the ship name. %d receives the integer count. The percent sign after the quotes applies the values to the gaps in that order.

  5. Subtract a delivered order

    After add_child(sign), type the deduction and print lines inside _ready. Press F5 and compare the game screen with Output.

    	orders_left -= 1
    	print("After one delivery: ", orders_left)

    Why it works -= subtracts one and stores the result. The screen keeps the earlier text, while print reads the newer count.

  6. Test a different order of instructions

    In a working copy, put the subtraction above the sign assignment, then run again and read the number. Restore the checkpoint after the experiment.

    Why it works Godot follows the statements in order. The finished string on the label does not continue watching the variable that helped create it.

Watch the values change

  1. Load the script SHIP_NAME is Starlight Voyager and orders_left is 3.
    What you see. _ready can read both values.
  2. Assign sign.text The format expression contains the count 3.
    What you see. The label says Bags waiting: 3.
  3. Subtract and print orders_left is 2.
    What you see. The log says After one delivery: 2.

Predict before you run The count becomes 2, but why does the sign still show 3?

Reveal the answer

The Label was given a finished string before the subtraction. To change the sign, the script must assign a new string.

Complete checkpoint

The game sign reports three waiting bags and Output reports two after the practice delivery.

main.gd

extends Control

const SHIP_NAME := "Starlight Voyager"
var orders_left: int = 3

func _ready() -> void:
	var sign := Label.new()
	sign.text = "%s | Bags waiting: %d" % [SHIP_NAME, orders_left]
	sign.position = Vector2(40, 50)
	sign.add_theme_font_size_override("font_size", 32)
	add_child(sign)
	orders_left -= 1
	print("After one delivery: ", orders_left)

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 ControlGives the root of this lesson the positioning and sizing features of a Godot Control, which is the type created by choosing User Interface in the scene editor.
  2. const SHIP_NAME := "Starlight Voyager"Stores the ship’s name as a constant. Every status message can read it from this one place.
  3. var orders_left: int = 3Starts the count at three luggage orders. The int type indicates this count uses whole numbers.
  4. func _ready() -> void:Godot calls this method after the scene is ready. Its indented steps make the dispatch label and any controls before the player can use them. It does not return a value.
  5. var sign := Label.new()Creates a fresh Label node. The variable sign keeps a reference to it, and the colon-equals operator infers the variable’s type from Label.new().
  6. sign.text = "%s | Bags waiting: %d" % [SHIP_NAME, orders_left]Fills the %s gap with the ship’s name and the %d gap with the current count. The new string becomes the Label’s text.
  7. sign.position = Vector2(40, 50)Places the label 40 pixels from the left and 50 down from the top, relative to its parent.
  8. sign.add_theme_font_size_override("font_size", 32)Overrides just the label’s font_size theme setting for a large, readable dispatch sign.
  9. add_child(sign)Adds the Label to this node so it becomes part of the active scene and Godot can display its text.
  10. orders_left -= 1Subtracts one from the previous count and saves the result back into orders_left. At the start of this lesson, three becomes two.
  11. print("After one delivery: ", orders_left)Prints the words followed by the current count. The comma separates the two arguments passed to print.

Find and fix a bug

If the text format fails, count the gaps and the values in the list. There should be two of each, with text first and a whole number second.

Try a change

Start with five bags and take two away. Predict the screen and log separately, then run to check.