0 votes

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]) 
in Engine by (93 points)

1 Answer

+3 votes
Best answer

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

by (21,698 points)
selected by

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

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.