Removing an item moves every item after it one place forward, but the loop still steps on to the next position, so the item that moved into the gap is never checked. In our Godot 4.7 test, erase() inside for d in disciples left 2 of 6 exhausted disciples in the list. Loop backwards with remove_at(i), or use filter(): both removed all 6.
The code
remove_where() removes everything the Callable says yes to and gives back what it removed, in the original order, so you can send those disciples home, play an effect for those enemies or show which buffs ran out. It changes the array you pass in.
extends Node
## Remove items from an Array safely: send home disciples with no stamina left,
## clear dead enemies, drop used-up buffs. Never erase from the array you are looping over.
static func remove_where(items: Array, should_remove: Callable) -> Array:
var removed: Array = []
for i in range(items.size() - 1, -1, -1): # backwards: removing does not move what is still to check
if should_remove.call(items[i]):
removed.append(items[i])
items.remove_at(i)
removed.reverse() # back to the original order
return removed
What we measured
10 disciples with stamina 0, 0, 5, 0, 3, 0, 0, 8, 0 and 2: 6 with none left, including two pairs next to each other. Two runs with the same results.
- erase() inside the for loop: 6 kept, D2(0), D3(5), D5(3), D7(0), D8(8) and D10(2). D2 and D7 still had 0 stamina: each came right after another exhausted disciple.
- The script: 4 kept, D3, D5, D8 and D10, and the 6 removed ones returned in their original order.
filter()keeping stamina > 0: the same 4.
Why it happens
A for loop over an Array walks it by position. When D1 is erased from position 0, D2 moves into position 0, and the loop goes on to position 1, which is now D3. D2 is never looked at. Going backwards, removing an item only moves items you have already checked. filter() does not change the array it reads, it builds a new one.
What to change
- Use
filter()when you only need what stays, andremove_where()when you also need what was removed. - The Callable can check anything: stamina, health, a timer, whether a node is still valid.
Progress while the game is closed: idle game offline progress.
A ready-made system: Xianxia Sect Management System for Godot 4 is an idle sect game system: disciples with jobs, 9 buildings, 8 missions, random events, reputation, save/load and offline progress.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Xianxia Sect Management System for Godot 4 is an idle sect game system: disciples with jobs, 9 buildings, 8 missions, random events, reputation, save/load and offline progress.
More how-tos for Xianxia Sect Management System for Godot 4: Big numbers with thousands separators, minus signs and all
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
- Scatter spikes, roots or trees in a circle without overlaps
- Count and take crafting items from every stack
- Play random sounds and events without obvious repeats
- 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