Fix unsafe entity iteration.

It was possible to crash the game by creating many entities in a script
callback that iterates over entities, for example.