Problem with remove and delete an element from array

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Tomi

Hello!

I have a big problem when I try remove an element from an array and delete it.
I have card node instances in this array. When the player clicks on it, a loop checks whether is there a match between two cards. If yes, I would like delete that two cards from the array and the screen too.
But when I remove and delete these, I always get an “Invalid get index ‘position’ (on base: ‘null instance’)” error message. If I use the is_instance_valid() function before the code, it would be good, but in this case some cards became are non clickable, because they are null instances but Godot still draws they.
How to solve this problem?
Here is my main piece of code:

func _process(delta):
	if (Input.is_mouse_button_pressed(BUTTON_LEFT) and can_click==true):
		can_click=false
		var mousex=get_global_mouse_position().x
		var mousey=get_global_mouse_position().y
		var i=0
		for j in cards:
			#if is_instance_valid(j):
			if mousex>j.position.x and mousex<j.position.x+cardwidth and mousey>j.position.y and mousey<j.position.y+cardheight:	
				var k: Sprite=j.get_node("cardsprite")
				if card1==null:
					card1=1
					card1id=j
					k.texture=spritechanger(j.itssprite)
					card1index=i
				elif card2==null and card1id!=j:
					card2=1
					card2id=j
					k.texture=spritechanger(j.itssprite)
					card2index=i
					if card1id.itssprite==card2id.itssprite:
						#cards.remove(card1index)
						#cards.remove(card2index)
						card1id.queue_free()
						card1=null
						card2id.queue_free()
						card2=null
						for m in cards.size():
							if !is_instance_valid(cards[m]):
								cards.remove(m)
					else:
						card1id.get_node("cardsprite").texture=load("res://cardbackground.png")
						card2id.get_node("cardsprite").texture=load("res://cardbackground.png")
						card1=null
						card2=null
			i+=1
	elif !(Input.is_mouse_button_pressed(BUTTON_LEFT)):
		if can_click==false:
			can_click=true
:bust_in_silhouette: Reply From: Tomi

Solved!
It must be care with the order of removing elements from array.
So, if somebody’s needs to remove more than one elements, the best way is always ordering and remove the last element to prevent confusing after redimensioning.
In the code:

elif card2==null and card1id!=j:
					card2=1
					card2id=j
					k.texture=spritechanger(j.itssprite)
                    			card2index=i
					if card1id.itssprite==card2id.itssprite:
						if card1index>card2index:
							cards.remove(card1index)
							card1id.queue_free()
							card1=null
							cards.remove(card2index)
							card2id.queue_free()
							card2=null
						else:
							cards.remove(card2index)
							card2id.queue_free()
							card2=null
							cards.remove(card1index)
							card1id.queue_free()
							card1=null

Sorry for this finally unnecessary question, but maybe at least the answer would be useful other beginners…