Video Game Design and Coding · PLECK lesson

PLECK lesson 107 · suggested week 22, day 2

Use a dictionary as an inventory

Objective. Store quantities by stable item ID.

Godot Engine Standard desktop editor 4.7.2-stable, GDScript (Godot 4), Compatibility renderer. Headless Godot 4.7.2.stable.official.ed1daf0bf passed 106 assertions on the supplied scaffolds, including malformed JSON saves. Audio used the Dummy driver and a synthetic WAV, not a speaker. The visual editor, browser export, and signed-in submission were not tested. The desk still does not run a student's own upload.

About 55 minutes. The due date is chosen by a parent.

Seed Station items, dictionaries, costs and data-driven UI

Suggested pace: Recall and predict 5 min; Read instruction and scaffold 12 min; Build or design the assigned artifact 25 min; Run acceptance tests 8 min; Explain and checkpoint 5 min.

Materials: Godot Standard 4.7.2 desktop editor (GDScript, Compatibility renderer), Local course folder and prior checkpoint, No paid assets or public account required.

Comes after VGC-L106.

A Dictionary maps keys to values. C-INVENTORY maps seed and water to integer counts. The same add_item function can handle either allowed ID, reducing duplicate code. Reject unknown IDs and nonpositive amounts before changing state. Use int when reading a Variant value as a quantity. A display label such as “Water can” may change while the internal water key stays fixed. Start with the standalone assertions before connecting pickups so you know arithmetic is correct.

Worked example

With two tickets and one token, a purchase costing one of each succeeds once. A second request must preserve the remaining ticket if no token is available. Scaffold C-INVENTORY — Dictionary inventory with guarded spending Setup: Attach to Node InventoryLab for standalone testing. In a real level put the same functions on its inventory node and update UI after successful calls. Item IDs are short fixed strings, never a player’s name. A key represents one item type and its integer value is the quantity. ```gdscript extends Node var items: Dictionary = {"seed": 0, "water": 0} func add_item(id: String, amount: int = 1) -> bool: if not items.has(id) or amount <= 0: return false items[id] = int(items[id]) + amount return true func spend(id: String, amount: int = 1) -> bool: if not items.has(id) or amount <= 0 or int(items[id]) < amount: return false items[id] = int(items[id]) - amount return true func _ready() -> void: assert(add_item("seed", 2)) assert(spend("seed", 1)) assert(not spend("seed", 2)) assert(items["seed"] == 1) ```

Dictionary inventory with guarded spending

GDScript (Godot 4, not Scratch, Python, JavaScript or C#). Scaffold C-INVENTORY. Copy starts from this text. The desk does not run it.

Scene and node setup

Attach to Node InventoryLab for standalone testing. In a real level put the same functions on its inventory node and update UI after successful calls. Item IDs are short fixed strings, never a player’s name. A key represents one item type and its integer value is the quantity.

extends Node
var items: Dictionary = {"seed": 0, "water": 0}
func add_item(id: String, amount: int = 1) -> bool:
    if not items.has(id) or amount <= 0:
        return false
    items[id] = int(items[id]) + amount
    return true
func spend(id: String, amount: int = 1) -> bool:
    if not items.has(id) or amount <= 0 or int(items[id]) < amount:
        return false
    items[id] = int(items[id]) - amount
    return true
func _ready() -> void:
    assert(add_item("seed", 2))
    assert(spend("seed", 1))
    assert(not spend("seed", 2))
    assert(items["seed"] == 1)

Checks to run in the editor

Native Godot 4.7.2.stable.official.ed1daf0bf headless compile and representative node-tree/runtime tests passed; see native-qa-results.json. Visual UI, audible output, exports and website submission still need separate checks.

Use a dictionary as an inventory — evidence task

  1. 1. Run C-INVENTORY and add water tests.
  2. 2. Test unknown ID, zero amount and negative amount.

Deliverable. Inventory lab source and six-case table.

Acceptance checks

Understanding question. Why not create a new arbitrary inventory key from every input string?

Include a downloadable project ZIP with project.godot, .tscn, .gd and original asset files when code changes. A private, teacher-accessible download link to that ZIP is equivalent. Screenshots or a playthrough do not replace editable source.

If this is still shaky. Use C-INVENTORY in a blank lab and test add/spend before connecting any button or pickup. Start with the first assigned task and re-run its acceptance test before adding the remaining feature.

Optional extra. Add one new allowed item ID and its tests without changing the transaction algorithm. Document one changed parameter, your prediction, and the observed effect; optional, not required for completion.

Name ____________________ Date ____________________