Dynamic Difficulty Adjustment

phi golden
4 min readMar 19, 2024

--

Implement algorithms that adjust difficulty dynamically based on player performance during gameplay. Players should have the option to select their preferred difficulty settings. Applying a difficulty curve ranging from -1 to 1 dynamically, where the player’s setting is 1 (easy) and dynamic range (0, 1), can help tailor the gameplay experience. This curve will be broadcasted to all interactable elements in the game, such as fire rate, enemy movements and speed, enemy fire alertness, collectible spawns, ammo availability, wave size, and spawn rate BPM.

At first glance (Video)

We can see multiple things that make the game difficult let’s start with adjusting the easiest of them all, the spawn rate.

A: This bool variable controls the BPM Spawn allowing us to spawn at a Beat.
B: This restricts the spawns between waves _canSpawnTime is set to Time.time when all enemies are killed giving a delay between waves.

Beat measures

Let’s have fun with it and spawn in measures that way we can say 1 spawn for every measure of 4 etc. just need a counter and a difficulty curve set by the game manager.

A: Block of code that runs per beat incrementing current beat count.
B: We test if the current beat is at our measure
C: reset the current tempo measure to repeat.

Results:

Feedback loop

Now let’s look at a spawn rate “Feedback Loop” for our difficulty.
How many enemies passed or were destroyed by the player before the player got hit. (Player hit rate)
how often should the difficulty be adjusted
How often will we spawn multiple at a time

We need to do this for every feature we didn’t add a difficulty curve to, Enemy fire probability, Enemy speed, Enemy aggression, needed collectible spawn probability, Mothership bullet sponge, adjusting Ammo bank ext.

Approach Summary

Just to sum it up, the GameManager.cs controls the Difficulty. We created events related to Enemy Spawns and listened to them in gameManager.cs. We compare the rate every spawn vs player hit in EnemySpawnManager.cs. If it’s greater than the current wave size then we increase the difficulty by 0.1f and if not we decrease the difficulty by 0.1f. we then update EnemySpawnManager.cs’s difficulty on the next wave. The GameManager.cs difficulty works with selected Easy (1) and adjust +- 1 from 0 to a max of 2. Medium (2) down to 1 and max to 3, and hard (3) range (2–4) where 4 is near impossible in the old state. one last thing since we use a +- 1 system on top of our selected difficulty, we created a MathFunctionsHelper.cs script to map multiple spawns per event probability.

We apply it to a simple should spawn on event with the probability to spawn a second or a third based on current difficulty.

1. Always spawns one enemy per event
2. A second chance to spawn an enemy
3. Half the chance to spawn a 3rd

Consider Fist

Nothing easy about setting up a feedback system, Not difficult either but we definitely need to rewrite a lot of code.
If you plan on setting up a difficulty curve, it definitely needs to be planned ahead of time.
Just for the enemy spawn Rate per measure ranging from 1 spawn per 2 measures or 8 beats (easy) to 1 spawn per beat (Hard) also not including multiple spawns for that spawn event was destructive to existing code.

That’s how we adjusted a difficulty curve for Spawning enemies, don’t mind me I’ll be busy doing every single feature that requires a difficulty curve.

Here are some alternative methods to look up about difficulty curves to try and look update

1. Iterative Development: Instead of implementing all aspects of the difficulty curve at once, you could start with a basic implementation and iterate based on play-testing and user feedback. This allows for more flexibility and adaptation as you refine the game’s balance.

2. Data-Driven Design: Rather than hard-coding difficulty parameters into the game code, you could store them in data files (such as JSON or XML) that can be easily modified without changing the code. This approach allows for easier tuning and balancing without needing to recompile the game.

3. Machine Learning: For more complex and dynamic difficulty adjustments, you could explore using machine learning techniques to analyze player behavior and adjust difficulty in real-time based on performance metrics.

Thanks for reading…
.
.
.

--

--