Call a neighbor
Trace CollectCoin as a guarded sequence of calls into the coin, score, HUD, and audio collaborators.
On this page
Collecting a coin is a handoff. PlayerController.CollectCoin validates the moment, then asks four collaborators to do the work they own.
The problem
Section titled “The problem”The player detects the trigger, but the coin owns collection state, the score counter owns totals, the HUD owns presentation, and the audio service owns sound. Putting all of that work in the player would blur those responsibilities.
When to reach for it
Section titled “When to reach for it”- One interaction coordinates several scene components.
- Each receiver already has a method that names its responsibility.
- The order of calls matters and should be visible on the exec wire.
See it
Section titled “See it”
The current Platformer source is:
public void CollectCoin(Coin coin){ if (coin == null || coin.IsCollected) { return; }
if (_scoreCounter == null || _hud == null) { _logger?.LogWarning( "player controller cannot collect a coin; its score counter or HUD reference is " + "unassigned. wire both on the player to enable coin collection."); return; }
coin.Collect(); // local (trigger-provided) _scoreCounter.AddCoins(CoinsPerPickup); // scene _hud.RefreshCoins(_scoreCounter.Coins, _scoreCounter.Score); // scene _audio?.PlayCoin(); // composed (optional)}-
Open
PlayerController.CollectCoinin Graph View. -
Follow the first guard. A missing or already-collected coin returns before any neighbor call.
-
Follow the second guard. Missing required score or HUD references produce a warning, then return. The audio service is optional, so it is not part of this guard.
-
On the success route, read the first receiver pin: the
coinparameter callsCollecton the trigger-provided instance. -
Continue to
_scoreCounter.AddCoins. Its argument comes from theCoinsPerPickupconstant pill. -
Continue to
_hud.RefreshCoins. Its two arguments read the score counter’s updatedCoinsandScoreproperties. -
Finish at
_audio?.PlayCoin(). The conditional call runs only when the composed audio service exists.
What it writes
Section titled “What it writes”Adding one more optional step after audio, such as a collection diagnostic, extends the end of the sequence:
The new call runs only after required collection, scoring, and HUD work succeeds. It does not need a new guard because the logger call is conditional.
When it goes wrong
Section titled “When it goes wrong”When it goes wrong
| Symptom | Check | Fix |
|---|---|---|
| The HUD shows the previous coin total. | Read whether RefreshCoins runs before AddCoins. | Keep the score update earlier on the exec wire. |
| A missing audio service stops collection. | Inspect the receiver form on PlayCoin. | Keep the audio call conditional so required work does not depend on it. |
| The same coin scores twice. | Follow the coin.IsCollected guard and confirm coin.Collect runs first. | Return for an already-collected coin before calling the score counter. |
- Method calls: choose a receiver and fill its argument pins.
- Get and set: read updated properties for the next call.
- Branch: protect required collaborators with early exits.