
build a health and damage component and a random drop system with values exposed for tuning, and test the drop rate with a simulation of many trials.
Instruction
Balancing means changing numbers many times. The code should make that easy: every value a designer might tune should be visible in the Inspector, and the game should be testable faster than by playing for an hour. (Randomness follows the Godot documentation’s “Random number generation” page, CC BY 3.0.)
A health component. Instead of writing health code in every enemy and in the player, write it once as a node you can add to anything:
extends Node
signal health_changed(current: int, maximum: int)
signal died
@export_range(1, 1000) var max_health: int = 100
var current: int
func _ready() -> void:
current = max_health
func take_damage(amount: int) -> void:
current = clampi(current - amount, 0, max_health)
health_changed.emit(current, max_health)
if current == 0:
died.emit()@export_range(1, 1000) shows a slider in the Inspector limited to sensible values. clampi keeps health between 0 and the maximum. The component does not know what a health bar or a death animation is; it only emits signals, and anything can listen. A ProgressBar connects to health_changed and sets its max_value and value; the enemy connects to died and plays its defeat.
Random drops. A drop chance of 0.25 means each defeated enemy has a 25% chance to drop an item. randf() returns a random decimal from 0 to 1, so comparing it to the chance gives the right probability:
@export_range(0.0, 1.0, 0.01) var drop_chance: float = 0.25
@export var pity_limit: int = 8
var misses: int = 0
func roll_drop() -> bool:
if misses >= pity_limit - 1 or randf() < drop_chance:
misses = 0
return true
misses += 1
return falseWhy a pity rule? On average, a 25% chance gives one drop every 4 enemies. But averages hide unlucky streaks. The chance of no drop in 10 enemies in a row is 0.7510 ≈ 0.056, so about 1 player in 18 will defeat ten enemies with nothing to show for it, and that player may quit. A pity rule guarantees a drop after a run of misses: with pity_limit 8, no player ever goes more than 8 enemies without a drop. The average barely changes, but the worst case is capped. This is balance for how it feels, not only for what it averages.
Test with a simulation. You cannot playtest 1,000 drops by hand, but the computer can in a fraction of a second. Add a temporary test to a scene:
func _ready() -> void:
var drops := 0
var longest_streak := 0
var streak := 0
for _i in 1000:
if roll_drop():
drops += 1
streak = 0
else:
streak += 1
longest_streak = maxi(longest_streak, streak)
print("Drops: ", drops, " Longest dry streak: ", longest_streak)Run it several times. Without the pity rule, expect about 250 drops per 1,000, and results between roughly 225 and 275 most of the time (the standard deviation is √(1000 × 0.25 × 0.75) ≈ 14). The longest dry streak will often be well above 10. With the pity rule, the drop count rises a little and the longest streak never exceeds 7 misses. Record both in your build log; this is balance evidence.
Separating data from code. Exported values mean designers tune in the Inspector without editing scripts. Professional teams go further and keep tables of values in spreadsheets or data files the game reads, so a designer can rebalance a hundred enemies without touching code.
📖 Read for this lesson
The reading opens over the lesson; close it to come back to exactly where you were. The checks below draw on it.
Build: Health component, drop system and simulation
Question. Does the drop system produce the intended drop rate over many trials, and how does a pity rule change the worst-case experience?
Materials
- Your game project in the Godot 4 desktop editor (4.3 or later)
- Kenney UI pack (CC0) for a health bar, optional
- A free spreadsheet program for recording simulation runs
- Your balance chart draft
- Keep ergonomic habits and 20-20-20 breaks.
- Remove or disable the simulation test code before sharing the build, and back up the project first.
Procedure
- Create the Health component script and add it as a child named Health to the player and to one enemy. Connect health_changed to a ProgressBar and died to a function that removes the enemy.
- Add a way to deal damage (for example, an Area2D hitbox or a key press that calls take_damage) and confirm the bar updates and the enemy is removed at 0.
- Add roll_drop() to the enemy with drop_chance 0.25 and pity_limit set very high (1000) to disable the pity rule. Add the simulation test and run it five times; record drops and longest dry streak.
- Set pity_limit to 8 and run the simulation five more times; record the results.
- Choose a drop chance and pity limit for your own game’s target experience, test them in play, and record the values and why you chose them.
- Remove or comment out the simulation test.
Data
| Run | Pity off: drops / 1000 | Pity off: longest dry streak | Pity 8: drops / 1000 | Pity 8: longest dry streak |
|---|---|---|---|---|
| 1 | ||||
| 2 | ||||
| 3 | ||||
| 4 | ||||
| 5 |
Analysis
- Were your pity-off drop counts within about 225-275? Explain why the results vary between runs even though the code is identical.
- How did the pity rule change the longest dry streak and the total drops? Why does the worst case matter to how the game feels?
- Why is a health component that only emits signals easier to reuse than health code that directly updates a specific health bar?
If you cannot do this build in person: Without Godot, run the simulation with dice: a roll of 1 on a four-sided die (or 1-3 on a d12) is a drop. Roll 100 times, tally drops and the longest dry streak, then repeat with a rule that forces a drop after 7 misses. Repeat the set of 100 over several sittings to reach 500 or 1,000 trials; rolling several dice at once and reading each die as one trial makes it faster.
Formative check
Work through these before moving on. They are not graded — they tell you whether the standard below has landed yet.
With a 25% drop chance, the average is one drop every enemies, and the chance of no drop in 10 enemies in a row is 0.75 to the power 10, about %.
Why does the health component emit health_changed instead of updating the health bar directly?
If a 1,000-trial simulation gives 262 drops at a 25% chance, the code must be wrong.
How confident are you that you can build tunable health and drop systems and test a drop rate by simulation?
Practice
Work these on paper or in your notebook, then open Check your answer. Aim for all of Fluency and Application; try at least one Challenge.
Printable version: this unit’s practice workbook (PDF)
Fluency
Build speed and accuracy with the core skill.
- What does each do in the health component: @export_range(1, 1000), clampi(current – amount, 0, max_health), died.emit()?
Check your answer
Answer: @export_range shows max_health as an Inspector slider limited to 1-1000; clampi keeps health between 0 and the maximum; died.emit() announces the death so any listener can react.The component does not know about health bars or animations; it only emits signals. - A drop chance is 0.2. On average, one drop every how many enemies? What does randf() < drop_chance do?
Check your answer
Answer: Every 5 enemies. randf() returns a random decimal from 0 to 1, so it is below 0.2 about 20% of the time, giving a drop with probability 0.2.1 ÷ 0.2 = 5. - Define a pity rule and explain what it changes and what it barely changes.
Check your answer
Answer: A guarantee of a reward after a set number of failures. It caps the worst unlucky streak while barely changing the average.This is balance for how it feels, not only for what it averages.
Application
Use the skill in context. Show your reasoning.
- Health is 40 with max_health 100. What is current after take_damage(55)? Starting again from 40, what is it after take_damage(-80)?
Check your answer
Answer: 0 (and died is emitted); 100.40 − 55 = −15, clamped to 0. 40 − (−80) = 120, clamped to the maximum of 100. - With a 20% drop chance and no pity rule, what is the probability of no drop in 10 enemies in a row? About 1 player in how many sees that streak?
Check your answer
Answer: About 0.107, roughly 11%: about 1 player in 9.0.810: 0.8² = 0.64, 0.8⁴ = 0.4096, 0.8⁸ ≈ 0.1678, × 0.64 ≈ 0.107. 1 ÷ 0.107 ≈ 9.3. - Code reading: with pity_limit = 5, what is the longest possible dry streak? Explain using the line if misses >= pity_limit – 1 or randf() < drop_chance:.
Check your answer
Answer: 4 misses. After 4 misses in a row, misses is 4, which equals pity_limit − 1, so the next roll is forced to drop.With pity_limit 8 the lesson’s longest streak is 7 for the same reason. - Find the bug. A student deletes misses = 0 from the drop branch. What happens after the first time the pity rule fires?
Check your answer
Answer: misses never resets, so it stays at or above pity_limit − 1 and every roll after that returns true: every enemy drops.Resetting the counter is what lets the pity rule fire only after a fresh run of misses. A simulation run would show it at once (drops near 1,000). - A 1,000-trial simulation at a 20% chance gives 214 drops. The standard deviation is √(1000 × 0.2 × 0.8). Compute it and say whether 214 suggests a bug.
Check your answer
Answer: About 12.6. No: 214 is about one standard deviation above the expected 200, which is normal variation.1000 × 0.2 × 0.8 = 160; √160 ≈ 12.6. Expected drops = 1000 × 0.2 = 200; 214 − 200 = 14.
Challenge
Stretch problems. Expect to think before you write.
- Explain why exposing drop_chance and pity_limit with @export_range is better than typing numbers inside the function, and what professional teams do beyond that.
Check your answer
Answer: Designers can tune the values in the Inspector without editing scripts, within sensible limits, and test many settings quickly. Professional teams keep tables of values in spreadsheets or data files the game reads, so a designer can rebalance a hundred enemies without touching code.Separating data from code makes balancing, which means changing numbers many times, fast and safe.
Review
Keep earlier skills sharp.
- Review (Lesson 3.1): an enemy has 100 health and the weapon deals 25 per hit. How many hits are needed, and is 25 a breakpoint for 100 health?
Check your answer
Answer: 4 hits. Yes: 24 damage would need 5 hits (100 ÷ 24 ≈ 4.2), so 25 is where the count drops to 4.Breakpoints for 100 health include 13, 15, 17, 20 and 25.
Drop Rates: Expected Values, a Health Walkthrough and a Filled Simulation Log
SLS original; expected values computed for this course, run logs invented · SLS original
Why this reading. The lab has you run a drop simulation and record drops and longest dry streak, and it asks how a pity rule changes the result. To check your runs, you need to know what to expect. This sheet works the expected values out by hand, shows a health-component walkthrough with numbers, and gives you a dice route if you cannot run Godot. SLS original.
Health component: take_damage walkthrough (max_health 100)
| Call | current before | current after | Signal |
|---|---|---|---|
| take_damage(30) | 100 | 70 | health_changed(70, 100) |
| take_damage(30) | 70 | 40 | health_changed(40, 100) |
| take_damage(30) | 40 | 10 | health_changed(10, 100) |
| take_damage(30) | 10 | 0 (clamped, not -20) | health_changed(0, 100), then died |
Expected simulation results
- Pity off, chance 0.25: expected drops per 1,000 = 250; standard deviation = square root of (1,000 x 0.25 x 0.75) = 13.7. Most runs fall between about 222 and 278.
- Chance of ten misses in a row = 0.75 to the power 10 = 0.0563, or 5.6%.
- Longest dry streak with pity off: roughly ln(1,000 x 0.25) / ln(1 / 0.75) = 19.2 misses on average, so expect streaks in the teens or twenties.
- Pity limit 8: the code forces a drop when misses reaches 7, so the longest streak is exactly 7. The average cycle length is 1 + 0.75 + 0.75 squared + … up to 0.75 to the power 7 = (1 minus 0.75 to the power 8) / 0.25 = 3.60 enemies per drop, so drops per 1,000 = 1,000 / 3.60 = 278. Expect about 278.
Read the trade: pity raises the drop count from 250 to about 278 (11% more) and cuts the worst streak from the twenties to 7. That is why designers say pity balances how the game feels.
A filled log (SLS original, invented runs)
| Run | Pity off: drops | Pity off: longest streak | Pity 8: drops | Pity 8: longest streak |
|---|---|---|---|---|
| 1 | 244 | 17 | 279 | 7 |
| 2 | 262 | 21 | 275 | 7 |
| 3 | 251 | 14 | 281 | 7 |
Run 2 has 262 drops: 12 above 250, less than one standard deviation (14), so the code is fine. The pity-8 column never exceeds 7. If yours does, check whether misses resets to 0 on a drop.
Dice route
Roll a d4; a 1 is a drop (25%). For pity, force a drop after 7 misses. Ten sets of 50 rolls, done over a few sittings, give 500 trials with the same spread; rolling several d4 at once and reading each die as one trial makes it faster.