Tutorial: Coin Yard
In this tutorial you build Coin Yard, a small 3D game: walk around a yard, collect spinning coins, watch your score on a HUD, hear a sound when you pick one up, push some crates around, and use a main menu and a pause screen.
Each step ends with a game that runs. Every file shown here was built into a real game
folder, checked with castc check, and run.
| Step | You learn |
|---|---|
| 1. A level and a spinning coin | game.toml, a screen, a level, object types, a component and a behavior in Cast |
| 2. A player you can walk | Spawning, handles, parts, keyboard input, the character controller |
| 3. Collecting coins and a HUD | Finding objects, destroying them, UI files and data binding |
| 4. Sound and music | One-shot sounds from Cast and an audioSource component |
| 5. Physics crates | Rigid bodies and colliders |
| 6. A main menu and a pause screen | The screen stack, buttons and event handlers |
Before you start
- Build Molten (Getting started).
- Put
build/debug/bin/Debug(Windows) orbuild/debug/bin(Linux) on yourPATH, or type the full path tomolten-gameandcastcin the commands below. - Make an empty folder for the game anywhere outside the Molten repository, for example
coinyard/. All paths in the tutorial are relative to it.
The game uses a few assets from the samples. Copy them in now:
| Copy from the Molten repo | To |
|---|---|
samples/physics/assets/models/cube.gltf |
coinyard/assets/models/cube.gltf |
samples/physics/assets/models/sphere.gltf |
coinyard/assets/models/sphere.gltf |
samples/gridline/assets/audio/power_on.wav |
coinyard/assets/audio/pickup.wav |
samples/gridline/assets/audio/click.wav |
coinyard/assets/audio/click.wav |
samples/gridline/assets/audio/music.wav |
coinyard/assets/audio/music.wav |
(The Gridline sounds are CC0; see samples/gridline/assets/LICENSE.txt.)
Running the game
From the folder that contains coinyard/:
castc check coinyard # check everything without running
molten-game --game coinyard # run it with live reload
Or open the folder in the editor with dev-run path/to/coinyard and press F5.
Keep the game running while you work. Most steps only add files; you can save them one by one and watch the game pick them up.
The finished layout
coinyard/
game.toml
assets/models/ cube.gltf sphere.gltf
assets/audio/ pickup.wav click.wav music.wav
components/ coin.cast coin_game.cast
behaviors/ spin.cast director.cast pickup.cast hud.cast
objects/ ground.object view_camera.object coin.object director.object
player.object player_body.object music.object crate.object
levels/yard/ yard.level chunks/0_0.chunk
screens/ main_menu.screen playing.screen pause.screen
ui/ hud.ui hud.style menu.ui menu.style pause.ui pause.style actions.cast
Start with step 1.