Video Game Design and Coding · PLECK lesson

PLECK lesson 22 · suggested week 5, day 2

Protect an event from being counted twice

Objective. Use a guard flag before emitting and removing a pickup.

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.

Area2D signals, groups, exactly-once events and a HUD

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-L021.

queue_free schedules removal rather than removing a node halfway through every physics calculation. More than one overlap event may occur before that removal completes. C-PICKUP sets taken to true before emitting collected, so later calls return without awarding another point. This is a small transaction: check eligibility, mark used, announce the reward, then remove the object. Do not connect the same body_entered signal both in the editor and in code; duplicate connections can hide an otherwise correct design.

Worked example

A pickup emits collected(1); a manager receives that event and changes one score variable. The label formats that score rather than keeping a second independent total. Scaffold C-PICKUP — An exactly-once pickup signal Setup: Create Area2D root Coin, CollisionShape2D Circle radius 10. Layer 3 (bit value 4), mask layer 1 only. Monitoring on. Attach script and save Coin.tscn. In the Node > Groups editor add Player root to group player. Put a Coin instance and Player instance in World. Manager connects each Coin.collected to its score handler (see C-MANAGER). Signal wiring here is code-only: do not also connect body_entered in editor. ```gdscript extends Area2D signal collected(points: int) @export var points: int = 1 var taken: bool = false func _ready() -> void: body_entered.connect(_on_body_entered) func _draw() -> void: draw_circle(Vector2.ZERO, 10.0, Color.GOLD) draw_line(Vector2(-5, 0), Vector2(5, 0), Color.BLACK, 2.0) func _on_body_entered(body: Node2D) -> void: if taken or not body.is_in_group("player"): return taken = true collected.emit(points) queue_free() ```

An exactly-once pickup signal

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

Scene and node setup

Create Area2D root Coin, CollisionShape2D Circle radius 10. Layer 3 (bit value 4), mask layer 1 only. Monitoring on. Attach script and save Coin.tscn. In the Node > Groups editor add Player root to group player. Put a Coin instance and Player instance in World. Manager connects each Coin.collected to its score handler (see C-MANAGER). Signal wiring here is code-only: do not also connect body_entered in editor.

extends Area2D
signal collected(points: int)
@export var points: int = 1
var taken: bool = false
func _ready() -> void:
    body_entered.connect(_on_body_entered)
func _draw() -> void:
    draw_circle(Vector2.ZERO, 10.0, Color.GOLD)
    draw_line(Vector2(-5, 0), Vector2(5, 0), Color.BLACK, 2.0)
func _on_body_entered(body: Node2D) -> void:
    if taken or not body.is_in_group("player"):
        return
    taken = true
    collected.emit(points)
    queue_free()

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.

Pocket Orchard starter, as text files. Open the files in Godot on the family computer. This page does not download a zip or run the project.

Protect an event from being counted twice — evidence task

  1. 1. Trace the order of the four pickup operations.
  2. 2. Temporarily print from the collected handler and test approaching the same coin from a corner.

Deliverable. Annotated pickup script and event-count log.

Acceptance checks

Understanding question. Why set taken before emitting collected?

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 one coin and one player in a blank World. Confirm the group and mask before adding a score label. Start with the first assigned task and re-run its acceptance test before adding the remaining feature.

Optional extra. Add a second pickup value using the same signal instead of duplicating the whole scene. Document one changed parameter, your prediction, and the observed effect; optional, not required for completion.

Name ____________________ Date ____________________