how to give a randomly picked new texture to a sprite?

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

Beginner here. This was supposed to be easy. I’m deseperate. Please help me.

What I want to do:
When I click on a “draw” button, the game draws one out of 33 cards (a different one at each draw until all 33 are used & then it restarts).

What I was able to do so far:
I could write the part where the cards are preloaded as new textures (examples below),

onready var drawMenu = get_node("Drawacardscene")
var new_texture_card1 = preload("res://Cards/card1.png")
var new_texture_card2 = preload("res://Cards/card2.png")
var new_texture_card3 = preload("res://Cards/card3.png")

and the part where it selects a number randomly (a different one at each draw until all 33 are used & then it restarts).

arrayDraw = initArray()

func initArray():
	var _card = []
	for i in range(33):
		_card.append("_card"+str(i))
	return _card

func _on_draw_pressed():
	var randomDraw = randi() % arrayDraw.size()

#	the line I'm loosing hope to find

	arrayDraw.remove(randomDraw)
	if arrayDraw == null:
		arrayDraw = initArray()

What I can’t seem to do is connect the too things together. I’ve been trying different versions of this (instead of the #comment above):

drawMenu.texture = new_texture_card+str(arrayDraw[randomDraw])

And godot tells me I can’t because I’m “trying to make a sprite out of a string”.

So here it is. Does anyone have any idea what I’m supposed to write instead? Or should I try something else (what)?

I’ve also tried to use the .hide() / .show() option instead of the new_texture one but it seems to lead to the same kind of error which means I’ve probably just missed some obvious thing I’m hoping you guys can point out to me.

Thanks in advance!

:bust_in_silhouette: Reply From: kelaia

enter image description here

extends Control


var possibilities = []
var textures = []

func _ready():
    randomize()
    create_mock_textures(5)
    reset_possibilities()
    refresh_label()

func create_mock_textures(size):
    for i in range(size):
        var image = Image.new()
        image.create(32, 32, false, Image.FORMAT_RGBA8)
        image.fill(Color(randf(), randf(), randf()))
        
        var texture = ImageTexture.new()
        texture.create_from_image(image)
        textures.append(texture)
    
func pick_random():
    var index = randi() % possibilities.size()
    possibilities.remove(index)
    if possibilities.size() == 0:
        $HBoxContainer/Button.disabled = true
    return textures[index]

func reset_possibilities():
    for i in range(textures.size()):
        possibilities.append(i)

func refresh_label():
    $HBoxContainer/Label.text = "Left: " + str(possibilities.size())

func _on_button_pressed():
    $HBoxContainer/TextureRect.texture = pick_random()
    refresh_label()

Hm thanks but that’s not what I asked - I mean, not in the whole article I wrote… I understand the confusion comes from the title, sorry about that.

Jayleeva | 2022-02-03 17:19