# Step 4 - Collect Coins

> Add coin collection to Babylon Bros using physics collision events, a score variable, custom events, and sound in the Flow Graph Editor.

Source: https://doc.babylonjs.com/toolsAndResources/flowGraphEditor/babylonBrosTutorial/coins

## Goal

When the hero collects a coin, increase the score, log it, and play a sound. This introduces custom events, which let you keep "a coin was collected" separate from "update the score".

Open this step in the editor: [#W0H5RG](https://flowgraph.babylonjs.com/#W0H5RG) (scene `P41T6E#2`).

![Coin graph: collision triggers a collect custom event that updates score](https://doc.babylonjs.com/img/tools/flowGraphEditor/tutorial/coins.webp)

Run this step:

- [Playground: Babylon Bros - Coins](https://playground.babylonjs.com/#VM4J12#0) - Tutorial step: move, jump, and collect coins, powered by flow graph #W0H5RG.

## Blocks Used

- `PhysicsCollisionEvent` (per question block / coin)
- `Branch` with an "active?" variable so each coin can only be collected once
- `SendCustomEvent` / `ReceiveCustomEvent` (`collect`)
- `GetVariable`, `Add`, `SetVariable` (score)
- `PlaySound`, `ConsoleLog`

## Step By Step

### 1. Central Collect Handler

Add `ReceiveCustomEvent` named `On Collect`. Wire it into `score + 1` (a `GetVariable` + `Add` + `SetVariable`), a `ConsoleLog` for the score, and `PlaySound` for the coin. This is the single place where the score changes.

### 2. Trigger From Collisions

For each block, add a `PhysicsCollisionEvent` on its body and a `Branch` that tests an `active` variable so it fires once. Set `active` to false, run the bump animation with `PlayAnimation`, then `SendCustomEvent collect`.

### 3. Reuse The Pattern

Build the first block fully, group it (`Ctrl/Cmd+G`), copy and paste it, then retarget it. Copying preserves the configuration and internal wiring; just point the new collision block at `block_2` and `block_3`.

## Editor Features

- Use [execution contexts](https://doc.babylonjs.com/toolsAndResources/flowGraphEditor/executionContexts) to confirm score is per run.
- Use Find (`Ctrl/Cmd+F`) to jump between block clusters as the graph grows past 100 nodes.

## Test And Save

Run, collect coins, watch the score log. Save; match `#W0H5RG`. Continue with [Step 5 - Spring Bounces](https://doc.babylonjs.com/toolsAndResources/flowGraphEditor/babylonBrosTutorial/springs).

## Further reading

- [Step 3 - Add Jumping](https://doc.babylonjs.com/toolsAndResources/flowGraphEditor/babylonBrosTutorial/jump)
- [Step 5 - Spring Bounces](https://doc.babylonjs.com/toolsAndResources/flowGraphEditor/babylonBrosTutorial/springs)
- [Data Access Blocks](https://doc.babylonjs.com/toolsAndResources/flowGraphEditor/dataAccessBlocks)
