Video Game Design and Coding · PLECK lesson
PLECK lesson 108 · suggested week 22, day 3
Make a two-resource purchase atomic
Objective. Validate the whole transaction before applying effects.
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-L107.
Atomic here means the planting action either completes fully or leaves the prior state unchanged. Check bed not already planted, seed at least one and water at least one. Only after all checks succeed should you subtract both and mark planted. Do not call spend(seed) and then discover spend(water) failed without rollback. Return a Boolean or clear result code so the caller can choose success or failure feedback. Write the three invalid cases before wiring the UI.
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) ``` Scaffold C-PUZZLE — A testable gate rule Setup: Create Node root GateLab and attach this script. The pure gate_open function does not read input or change a scene. A level can call it with its inventory and lever state. Assertions run on start. For the assignment write additional tests before changing the rule. ```gdscript extends Node func gate_open(has_key: bool, lever_on: bool, alarm_on: bool) -> bool: return has_key and lever_on and not alarm_on func _ready() -> void: assert(gate_open(true, true, false)) assert(not gate_open(false, true, false)) assert(not gate_open(true, false, false)) assert(not gate_open(true, true, true)) print("Gate checks passed") ```
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
- Spending more than owned returns false and does not change inventory.
- Unknown IDs and nonpositive amounts are rejected.
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.
A testable gate rule
GDScript (Godot 4, not Scratch, Python, JavaScript or C#). Scaffold C-PUZZLE. Copy starts from this text. The desk does not run it.
Scene and node setup
Create Node root GateLab and attach this script. The pure gate_open function does not read input or change a scene. A level can call it with its inventory and lever state. Assertions run on start. For the assignment write additional tests before changing the rule.
extends Node
func gate_open(has_key: bool, lever_on: bool, alarm_on: bool) -> bool:
return has_key and lever_on and not alarm_on
func _ready() -> void:
assert(gate_open(true, true, false))
assert(not gate_open(false, true, false))
assert(not gate_open(true, false, false))
assert(not gate_open(true, true, true))
print("Gate checks passed")
Checks to run in the editor
- Four supplied assertions pass.
- A complete truth table contains eight input combinations.
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.
Make a two-resource purchase atomic — evidence task
- 1. Implement plant_bed with both-cost validation and a planted flag.
- 2. Test missing seed, missing water, already planted and valid purchase.
Deliverable. Atomic planting function and before/after tables.
Acceptance checks
- VGC-L108-T1. Every failed case preserves both counts and bed state.
- VGC-L108-T2. Success subtracts exactly one of each and marks one bed.
Understanding question. Why are two individually safe spend calls not automatically one safe transaction?
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.
- Godot step by step: nodes, scenes, GDScript, input, signals
Free public documentation, no account. Same class help is available within the desktop editor.
· Opening this link does not submit the lesson. - Saving games: FileAccess and JSON
Free public documentation, no account.
· Opening this link does not submit the lesson.
Name ____________________ Date ____________________