Picking with randi() % size every time is fair over the long run, but with 4 clips the same one comes twice in a row about a quarter of the time, and players notice. In our Godot 4.7 test it repeated back to back 239 times in 1,000 plays, and one clip went 31 plays without being heard. A shuffle bag plays every clip once per round: 0 repeats, and never more than 6 plays between the same clip.
The code
Fill items with your AudioStreams, flicker patterns or event names and call next() whenever you need one. Seed rng to get the same order every run. Use one bag per kind of thing, so scare sounds and ambient sounds do not share a bag.
extends Node
## Random picks that never repeat back to back and use every item before any comes again:
## scare sounds, light flicker patterns, ambient events. Call next() each time you need one.
var items: Array = []
var rng := RandomNumberGenerator.new()
var _bag: Array = []
var _last = null
func next() -> Variant:
if items.is_empty():
return null
if _bag.is_empty():
_bag = items.duplicate()
for i in range(_bag.size() - 1, 0, -1): # shuffle with our own rng, so a seed repeats the order
var j := rng.randi_range(0, i)
var t = _bag[i]
_bag[i] = _bag[j]
_bag[j] = t
if _bag.size() > 1 and _bag.back() == _last: # the new bag must not start with the last one played
var first = _bag[0]
_bag[0] = _bag[-1]
_bag[-1] = first
_last = _bag.pop_back()
return _last
What we measured
4 clips, 1,000 plays, with a fixed seed. Two runs with the same numbers.
- randi() each time: 239 back-to-back repeats, the longest stretch without one clip 31 plays, and the clips played 254, 238, 248 and 260 times.
- The script: 0 repeats, at most 6 plays between the same clip, and each clip exactly 250 times.
Why it happens
Each randi() pick does not know what came before, so with 4 clips a repeat has a 1 in 4 chance every time. A shuffle bag deals out every item once before reshuffling, and the swap at the start of a new round stops the last clip of one round from opening the next.
What to change
- Put a clip in the list twice to make it twice as common. Its two copies can then come one after the other, so keep that for clips that can take it.
- For chances that are not equal, like rare drops, use weights instead.
Weighted chances: weighted random with rand_weighted(). Fog that works on every renderer: dark horror fog.
Ready-made horror effects: Horror Atmosphere VFX for Godot 4 has 45 presets in Modern and Retro PSX looks, including Ground Fog, Creeping Mist, flickering and dying bulbs, a dying flashlight, glowing eyes and ghosts.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Horror Atmosphere VFX for Godot 4 has 45 ready-made horror effects in Modern and Retro PSX looks, including flickering and dying bulbs, a dying flashlight, Ground Fog, glowing eyes and ghosts.
More how-tos for Horror Atmosphere VFX for Godot 4: Dark horror fog that works in Compatibility · A scare that plays once, and only for the player
More how-tos: Game systems
- Hover a 3D object inside a SubViewport
- A turned building that covers the cells it stands on
- An inventory that never loses an item and never overfills a stack
- A JSON save that loads back exactly what you saved
- Fire that spreads one cell per step, in every direction
- A pooled enemy that comes back as fresh as a new one
- An enemy that stops at the dead player and finds the respawned one
- Poison, burn and bleed that keep ticking when applied again
- XP, qi and gold that keep growing without going negative
- Count slot line wins with wilds in front
- Random directions spread evenly over a sphere or a cone
- Count a lap only when the car really went round
- Make a seed repeat: daily runs, replays and roguelike seeds
- Offline progress that counts the time the game was closed
- A chance roll without the long bad-luck streaks
- Check crafting recipes in any order, with the right counts
- Make HP and mana regen the same at any frame rate
- Damage over time with the right number of ticks
- Spawn enemies where the camera cannot see them
- Keep a chasing horde from stacking into one enemy
- Make the fish bite after the same wait at any frame rate
- Snap buildings to the right grid cell on both sides of the origin
- Make a cooking timer fire once, on time
- Pick from a weighted loot table with the right chances
- Level up as many times as the XP covers
- Walk a patrol guard to each point without overshooting
- Remove items from an Array while checking them, without skipping any
- Scatter spikes, roots or trees in a circle without overlaps
- Count and take crafting items from every stack
- Pick the growth image so the ripe one shows exactly when the crop can be picked
- Turn a day counter into season, day and year without a day 0
- An enemy that dies once, however many hits land
- A fair shuffle you can replay
- A ghost that drives the lap at real speed
- A vision cone that does not flicker at the edge
- Move a group of units without a pile-up
- A sprinkler that waters a round patch
- A slot reel that always lands forward on the result
- A crowd of NPCs that never emote or bob in step
- A song position that moves on every frame
- Nodes your editor tool adds that stay in the scene