Unexpected behaviour when using entity:Kill() while iterating through FindInCapsule/FindInRadius
I'm making a familiar (Which, in this situation, is technically a bomb due to complications regarding TryThrow not working on familiars, but that's besides the point but feel it worth mentioning just in case that plays a part in the issue) that when thrown, destroys any projectiles that it collides with. I have been doing this with
for k, v in pairs(Isaac.FindInCapsule(bomb:GetCollisionCapsule(), EntityPartition.BULLET)) do
v:Kill()
end
A note that this is done in a MC_PRE_BOMB_UPDATE callback. POST_BOMB_COLLISION does not fire in this circumstance.
When testing, I found that, inexplicably, enemies would randomly die when hit by my familiar/bomb. After some further experimentation, I found that removing the v:Kill() call stopped this from happening.
Some other things I have tested/noticed:
- Sometimes, entities outside of the collision capsule would be killed instead.
- Doing
print(v.Type)always printed "9" into the console (EntityType.ENTITY_PROJECTILE) - Changing it to
if v:ToProjectile() then v:Kill() endhad no effect and npcs would continue to randomly die as before. - Using FindInRadius also demonstrated strange behaviour.
- Changing it to
Remove()works as expected. - Creating a table, copying all the entities to be killed into that table, and then doing a second iteration through that, also checking
if v:ToProjectile() thendid not stop non-projectiles being killed.
Initially I had thought this was just an issue with FindInCapsule, but further testing has left me stumped.
Your title mentions FindInRadius in addition to FindInCapsule, but I can't seem to find any references to it in the description of the issue ? Did you do some tests with FindInRadius, or is it a typo in the title of the issue ? (I'm asking because I'm more familar with FindInRadius... Extremely questionable implementation, so what you describe would not surprise me that much if it applied to FindInRadius)
oop, I mustve forgotten to mention it, but yes, I tested using FindInRadius as well.