count watcher tells us exactly how many stars are alive, and a cap check keeps the spawner from overcrowding the sky.Learning Goals 3 min
By the end of this lesson you will be able to:
- Explain why there is no built-in reporter for "how many clones are alive?" and why we need a workaround.
- Build a (count) variable (For all sprites) and update it with change [count v] by (1) at clone birth and change [count v] by (-1) just before delete this clone.
- Use the (count) variable to make the spawner stop when it hits a target ((count) > (50)) and to detect "wave cleared" ((count) = (0)).
Warm-Up — where's the "live clones" block? 7 min
Last lesson you fixed the leak. Your endless rain now spawns and deletes in balance. But how many clones are alive on the Stage right now? Twenty? Forty? You can't tell by looking — they overlap, they fade, they fall fast.
Open Scratch and search every palette for a block that reports the number of live clones. Look at Sensing, Control, Variables, Operators. Take 60 seconds.
Reveal the answer
There isn't one. Scratch never built a "how many clones?" reporter. Closest is the Stage's "frame count" debug tooltip, but that's a tool for developers — not a block you can use inside a script. If you want to know how many clones are alive, you have to count them yourself.
The trick is simple: a variable that goes up by one when a clone is born, and goes down by one when a clone dies. That's it. Today's lesson is just three lines of code in three places — but it unlocks a whole class of game features: stop spawning when the screen is full, detect when all enemies are gone, score per active enemy. For the Falling Stars Cloud it's going to cap the sky at 50 stars so the project runs smoothly on any laptop.
New Concept — the count variable pattern 15 min
You've used variables since Level 1 — score, lives, timer. Today you'll use one for something different: tracking the size of a group of clones. The variable doesn't measure anything in the game world — it measures Scratch itself.
Step 1: make the variable
In the Variables palette, click Make a Variable. Name it count. Choose For all sprites — every clone needs to see and change the same shared number. (If you pick "For this sprite only", each clone gets its own private copy and the count is always 0 or 1.)
when flag clicked
set [count v] to (0)
Step 2: count up at clone birth
Inside the when I start as a clone hat, the very first block should be change [count v] by (1). The clone says "I'm alive!" the instant it's born.
when I start as a clone
change [count v] by (1)
go to x: (pick random (-200) to (200)) y: (180)
show
forever
change y by (-3)
end
Step 3: count down just before delete
Wherever delete this clone appears, put change [count v] by (-1) immediately above it. The clone says "I'm dying!" the instant before it vanishes.
forever
change y by (-3)
if <touching [edge v] ?> then
change [count v] by (-1)
delete this clone
end
end
Why "directly above"?
delete this clone has a jagged bottom — but more importantly, it ends the script immediately. If you put the −1 after the delete, Scratch will refuse to snap it there anyway. If you put it somewhere else in the script, it might run too early or not at all. The rule: −1 then delete, in that order, side by side.
Now you have (count) — what can you do with it?
Two everyday uses:
Use 1: cap the spawner
Don't let the screen get overcrowded. Before spawning, check the count:
when flag clicked
set [count v] to (0)
hide
forever
if <(count) < (50)> then
create clone of (myself v)
end
wait (0.2) seconds
end
Use 2: detect "wave cleared"
In a wave-shooter, when every enemy is dead, count drops to 0 — and that's your signal to start the next wave:
when flag clicked
set [count v] to (0)
forever
wait until <(count) = (0)>
broadcast (next wave v)
wait (1) seconds
end
Worked Example — capped raindrop spawner 12 min
Take your endless-rain project from L03-23 and add a counter. Build a system that holds the live clone count between 0 and 30 — no more, no less — and shows the count on the Stage.
Step 1 — Open the rain project
Open HW-L3-23-Endless-Pisang.sb3 (or any project with a working spawner + delete). Click the flag to confirm it still works.
Step 2 — Make the variable
Variables palette → Make a Variable → name count → For all sprites. A small watcher appears in the top-left of the Stage.
Step 3 — Reset at flag
On the original sprite, at the top of the when ⚑ clicked stack (before hide), drop in set [count v] to (0).
Step 4 — Cap the spawner
Wrap the inside of the spawner's forever loop in if <> then. The diamond gets (count) < (30). Inside, the existing create clone of (myself v). Outside the if (still inside the forever): wait (0.2) seconds.
Step 5 — Count up at birth
In the clone hat, immediately under when I start as a clone, drop in change [count v] by (1). Everything else slides down a slot.
Step 6 — Count down before delete
Find your delete this clone (it's inside the if-touching-edge from L03-23). Above it, drop in change [count v] by (-1). The two blocks now sit as a pair.
Step 7 — Test
Click the flag. Watch the count watcher in the top-left. It climbs from 0 toward 30, then plateaus — the spawner stops firing while count = 30, then fires again the instant one clone deletes itself. The number wiggles around 28–30 forever.
Step 8 — Pretty up the watcher
Right-click the count watcher on the Stage → choose large readout. Now you have a big visible "live raindrops: 30" display.
The full assembled stacks
when flag clicked
set [count v] to (0)
hide
forever
if <(count) < (30)> then
create clone of (myself v)
end
wait (0.2) seconds
end
when I start as a clone
change [count v] by (1)
go to x: (pick random (-200) to (200)) y: (180)
show
forever
change y by (-3)
if <touching [edge v] ?> then
change [count v] by (-1)
delete this clone
end
end
What you just built: a self-regulating population system. The same pattern that powers enemy waves in shoot-em-ups, particle limits in fireworks demos, and the "you can't place more units" rule in strategy games.
Try It Yourself — three counter drills 15 min
Goal: Add a counter to any existing clone project (use last week's homework). Don't change the spawn or delete logic — just add the four touches: set [count v] to (0) at flag, +1 in clone hat, −1 before delete. Confirm the watcher rises and falls correctly.
when flag clicked
set [count v] to (0)
when I start as a clone
change [count v] by (1)
go to x: (pick random (-200) to (200)) y: (180)
show
forever
change y by (-3)
if <touching [edge v] ?> then
change [count v] by (-1)
delete this clone
end
end
Think: The four touches are the whole pattern. Memorise them. Every clone-counting project you ever write will have exactly these four touches, in these four places.
Goal: Make a "score per enemy" project. Each enemy clone is worth 10 points per second alive. Use a (score) variable. Every half-second, the Stage should add (count) × 5 to the score. (5 = half-second × 10 points per second.)
when flag clicked
set [count v] to (0)
set [score v] to (0)
forever
wait (0.5) seconds
change [score v] by ((count) * (5))
end
Think: The clone code is unchanged from the Easy task. The score logic lives on the Stage (or any sprite) and just reads the count. That's why "For all sprites" matters — everyone can see the number.
Goal: Wave shooter setup. Spawn 10 enemy clones at the start of each wave. When the player has destroyed all 10 (count back to 0), wait 2 seconds, then spawn 10 more. Use wait until <(count) = (0)>. Bonus: increase the spawn count by 5 each wave (10, 15, 20...) using a (wave) variable.
when flag clicked
set [count v] to (0)
set [wave v] to (1)
forever
repeat ((10) + ((wave) * (5)))
create clone of (myself v)
wait (0.3) seconds
end
wait until <(count) = (0)>
wait (2) seconds
change [wave v] by (1)
end
when I start as a clone
change [count v] by (1)
go to x: (pick random (-200) to (200)) y: (170)
show
repeat until <touching [Bullet v] ?>
change y by (-2)
end
change [count v] by (-1)
delete this clone
Think: Wave 1 spawns 15 enemies, wave 2 spawns 20, wave 3 spawns 25... The wait until is the gatekeeper — the next wave can't start until (count) drops to exactly 0. That's only possible because you maintained the count correctly with +1 and −1.
Mini-Challenge — Iman's drifting counter 5 min
"My count keeps growing forever"
Iman builds a clone-counter for his snowfall project. Snowflakes can leave the screen in two ways — they touch the bottom edge, or they touch the snowman sprite at the bottom (the snowman "catches" them). He writes this clone-hat stack:
when I start as a clone
change [count v] by (1)
go to x: (pick random (-200) to (200)) y: (180)
show
forever
change y by (-3)
if <touching [Snowman v] ?> then
delete this clone
end
if <touching [edge v] ?> then
change [count v] by (-1)
delete this clone
end
end
Iman runs his project. After two minutes, count reads 87, but only about 15 snowflakes are actually visible. Where did the other 72 go — and why is the count wrong?
Reveal one valid solution
Iman has two deletion paths but only one of them decrements the count. When a snowflake touches the snowman, it deletes — but the count never goes down. Over time, the missing −1s pile up. The actual live-clone number is fine (Scratch is happy), but his counter has drifted.
The fix is to add change [count v] by (-1) above the first delete too:
when I start as a clone
change [count v] by (1)
go to x: (pick random (-200) to (200)) y: (180)
show
forever
change y by (-3)
if <touching [Snowman v] ?> then
change [count v] by (-1)
delete this clone
end
if <touching [edge v] ?> then
change [count v] by (-1)
delete this clone
end
end
Rule: every delete this clone in your project must have a change [count v] by (-1) directly above it. No exceptions. If you've got three delete blocks, you need three −1s. Miss one and the count silently drifts.
Recap 3 min
Scratch never gave us a "how many clones are alive?" reporter, so we built one. The pattern has exactly four touches: set [count v] to (0) at the flag, change [count v] by (1) at the top of the clone hat, and change [count v] by (-1) immediately above every delete this clone. Make the variable "For all sprites" so every clone shares it. Once you have (count), you can cap the spawner, detect waves cleared, or score per active enemy. The whole technique is small but it crosses a line — you've gone from controlling individual clones to controlling the population of clones. That's a designer's tool, not just a programmer's.
- Counter variable
- A variable used to track the size of something — here, the number of live clones. Goes up when one is created, down when one is deleted.
- For all sprites
- The Variables option that makes one shared variable visible to every sprite and every clone. Required for clone counters — "For this sprite only" gives each clone its own copy and breaks the count.
- The four touches
- The complete count-clones pattern: (1) set [count v] to (0) at flag, (2) +1 in the clone hat, (3) −1 before delete, (4) a (count) read somewhere that uses the number. Miss any and the system breaks.
- Cap-check
- An if <(count) < (N)> then wrapped around create clone of (myself v) to prevent the spawner from overcrowding the Stage. Stops short of Scratch's 300-clone cap.
- Wave cleared
- The game-design moment when all clones from a wave have been deleted — detectable as (count) = (0). The trigger for "next wave" logic.
Homework 2 min
Counted Kuih Catcher. Build (or extend) a simple game where kuih clones fall from the top and a piring sprite at the bottom catches them with the mouse.
- One sprite for the kuih (any kuih costume — kuih lapis, onde-onde, anything). One sprite for the piring (a plate or bowl) that follows the mouse along the bottom.
- Make two variables (For all sprites): (count) and (caught). Set both to 0 at flag.
- Spawner: spawn one kuih every 0.5 seconds, but only if (count) < (8). Never more than 8 on screen.
- Clone hat: +1 to count at birth. Fall with change y by (-3). Two delete paths: touching piring (count down + caught up + delete) and touching edge (count down + delete only).
- Big watcher on the Stage for both variables.
Save as HW-L3-24-Kuih-Catcher.sb3. Play for one minute and count how many you actually caught. The (caught) number should match.
Bring back next class:
- The
.sb3file. - Your answer to: "If you forgot the −1 above the touching-piring delete, what would the count watcher do over time? Try it on purpose for 30 seconds and write down what you saw."
Heads up for next class: SCR-L03-25 uses everything you've learned in this arc to build and ship the finished Falling Stars Cloud — ghost-fade, random sizes, random speeds, the count cap, and a share link you can send to your friends.