So I'm working on a small 2d project, in which I procedurally generate a 2d tilemap of caves using cellular automata. It's my first time ever doing this, but I'm proud of the results:

Every time the scene is loaded, the cave system is procedurally generated again. However, an empty cave isn't really fun to explore. My plan was to detect the top edges of the black tiles, and have a percent chance to spawn coins or something.
What I tried to do was detect if a tile was black and had a white tile above it, and return true. Then I would instance something on or above that tile. It boils down to two functions:
`func SpawnLoot():
var temp = loot1.instance()
for i in range(Mwidth):
for j in range(1, Mheight):
if (i < Mwidth and j < Mheight):
if (isUpClear(Vector2(i,j))):
add_child(temp.duplicate())
temp.set_position(map_to_world(Vector2(i,j)) + cell_size/2)
func isUpClear(var pos):
var bottom = get_cellv(Vector2(pos.x,pos.y))
var top = get_cellv(Vector2(pos.x,pos.y-1))
if (bottom == 1 and top == 0):
return true
return false
`
What I expect for the moment is just instancing my green loot block on every black tile with a white tile above it. However, the output is this:

I can't figure out any rhyme or reason as to why it results like this. It's different every time I load the scene, but doesn't seem to result in a way that's related to how the tiles have generated.
Am I misunderstanding something fundamental here? Halp plz :(