This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

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 isinstancevalid() 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
Godot version 3.3 Stable Official Win64
in Engine by (52 points)
edited by

1 Answer

0 votes

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...

by (52 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.