The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

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!

Godot version v3.4
in Engine by (12 points)

1 Answer

0 votes

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()
by (255 points)
edited by

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.

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.