So I said "The following can be added to the beginning of _ready() in map.gd."
You have: "The following code added before the beginning of _ready() in map.gd."
Spot the difference? It freezes cause you put local-scope code in the file-scoped portion of the code, which is completely undefined behavior. I tried to be quite precise with my description, because there are very few locations you can put this code in the file to get the expected behavior. Perhaps I was too concise, but I am going to sleep now. The code I gave you only works inside a function and not inside a for loop.