How to remove an element from an array

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

I have a function that changes the texture of a character.
Choose random texture from an array,

How do I remove the item that was assigned to the character, so that it can no longer be used

var sprites = [
    	"res://Img/black.png",
    	"res://Img/green.png",
    	"res://Img/griss.png",
    	"res://Img/orange.png",
    ]
    
func _color():
	$Sprite.texture = load(sprites[randi() % 4]) 
:bust_in_silhouette: Reply From: jgodfrey

Just remove the same index you assigned… So, change your code to something like (untested):

var sprites = [
        "res://Img/black.png",
        "res://Img/green.png",
        "res://Img/griss.png",
        "res://Img/orange.png",
    ]

func _color():
    var index = randi() % sprites.size()
    $Sprite.texture = load(index) 
    sprites.remove(index)

Note, that you need to use the current size of the array to select the next element, since the size changes with each removal…

Note, I just edited the code I posted above to fix an oversight…

jgodfrey | 2020-11-21 00:22