Problem with Making Pieces Stick in Tetris

Godot Version

4.2.1.stable

Question

I am currently trying to code a version of Tetris in Godot, but I am having some trouble making the pieces stay when they hit the floor. My logic is that the game board is on the base layer and the falling pieces are on the active layer. When a piece on the active layer touches a block on the base layer, all pieces on the active layer are copied onto the base layer. That way, the game board and inactive pieces are treated the same. This should make it safe to erase the active layer every time a piece moves left or right, because all inactive pieces are on the base layer. That is what I have right now, except only the pieces that are directly touching a base layer piece are copied to the base layer. For example, if the z-shaped block falls on the ground , only the two blocks that are touching the ground get copied. If I then move the next piece, they will stay there, but the other two blocks get erased. How do I fix this? Should I change the way movement erases some of the blocks or is it possible to copy all of the active blocks to the base layer? Here is my code inside the _process() function:
var b = get_used_cells(0)
var a =
for i in b:
var vector2 = Vector2(i.x, i.y)
a.append(vector2)

for vec in get_used_cells(0):
	
	for i in check:
		
		if get_cell_atlas_coords(1, Vector2(vec.x, vec.y+1), false) == Vector2i(check[i],0):
			#set_cell(1, vec, 0, Global.atlas)
			print('2')
			timer = false
			stop = true
			set_cell(1, vec, 0, atlas)
			print(get_used_cells(base_layer).size())
			erase_cell(0, vec)
			#stop = false
			steps = 0
			x = 0
			choice = Global.piece()
			for x in shapes:
				if !shapes.is_empty():
					choice = shapes.pop_front()
				else:
					shapes = shapes_full.duplicate()
		else:
			stop = true
		
			#print(choice)
			#print(get_used_cells(0).size())
			#atlas = Global.color()
		
	#for i in get_used_cells(1):
		#if not get_cell_atlas_coords(1, i, false) == Vector2i(7,0):
			#print(get_used_cells(1).size())			

if Input.is_action_just_pressed('ui_right'):
	#if stop == true:
		#for vec in get_used_cells(0):
			#set_cell(1, vec, 0, atlas)
			#clear_layer(0)
			#stop = false
	#erase_cell(0, Vector2(steps, x))
	#erase_cell(0, Vector2(steps+6, x-1))
	clear_layer(0)
	x+=1
if Input.is_action_just_pressed('ui_left'):
	#if stop == true:
	#for vec in get_used_cells(0):
		#set_cell(0, Vector2(steps+5, x-1), 0, atlas)
		#erase_cell(0, Vector2(steps, x+1))
		#print('1')
			
	#erase_cell(0, Vector2(steps, x))
	clear_layer(0)
	x-=1
if Input.is_action_just_pressed('ui_down'):
	#if stop == true:
		#for vec in get_used_cells(0):
			#set_cell(1, vec, 0, atlas)
			#clear_layer(0)
			#stop = false
	steps+=1

I have seperate functions for each piece that create the piece from block with preset coordinates.

Maybe try to copy the full piece to base layer:
I guess the current problem is only tiles with cooridinate.y == 7 is copied to base layer, but the shape of a piece is not always of the same coord.y, so an additional judgement is required to decide which tiles are copied:

What I would do is when there is tile reaches bottom, call a recursive function to look for tiles with the same atlas: the function gets the atlas of the tile, and check the four position next to it, if the checked tile is of same atlas it is added to an array, and checking is done again on its position. recursive function makes all tiles get returned as an array, then manipulation can be done on this array.

or that’s a little unnecessary, there are much better ways to get all the tiles in a piece. make a variable to record the color for every new piece, and just traverse in the full active layer tilemap(it is slow if we have really large size tilemap) and get the tile of the current color, and add them to an array to further deal with.