Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Tomi |
Hello!
I already almost finished my memory game in Godot, but I can’t delete a node instance from an array without this error:
Invalid get index ‘24’ (on base: ‘Array’).
I have created this card nodes with code and an array contains these:
var c=card.instance()
(...)
cards.append(c)
Then, this piece of code checks whether need to delete two cards or not:
func _process(delta):
if (Input.is_mouse_button_pressed(BUTTON_LEFT)):
var mousex=get_global_mouse_position().x
var mousey=get_global_mouse_position().y
for j in range(0,cards.size()):
if mousex>cards[j].position.x and mousex<cards[j].position.x+cardwidth and mousey>cards[j].position.y and mousey<cards[j].position.y+cardheight:
var k: Sprite=cards[j].get_node("cardsprite")
if card1==null:
card1=cards[j]
k.texture=sprchanger(cards[j].itssprite)
elif card2==null and card1!=cards[j]:
card2=cards[j]
k.texture=sprchanger(cards[j].itssprite)
if card1.itssprite==card2.itssprite:
for m in range(0,cards.size()):
if cards[m]==card1:
card1.queue_free()
card1=null
else:
card2.queue_free()
card2=null
cards.remove(m)
break
else:
card1.get_node("cardsprite").texture=load("res://cardbackground.png")
card2.get_node("cardsprite").texture=load("res://cardbackground.png")
card1=null
card2=null
But something is wrong, because I get that error message at mouse and card position check line when my code uses the queue_free() and remove(m) commands.
Maybe I overcomplicated my code at deleting lines? But then which is the best method to remove a node instance from an array?
I think your cards.remove and break in the for loop don’t make sense. Few comments in code would help when you have code this complex.
wyattb | 2021-05-22 11:33
Hello Wyattb!
Should I use cards.remove after queue_free(), or one of the two is unnecessary?
Tomi | 2021-05-23 08:49