Card Dealing Issue

Latest version, 4 I think.

I am working on a card game and need to make dealer part of it. I’ve found how to draw a card but I can seem to figure out how to make the cards deal a specific number when the deal button is pressed. I need four cards to deal to four players.

Just so you know, you can see the version at the very bottom of the editor window:
image

Like this?

const PLAYER_COUNT: int = 4
const CARD_COUNT: int = 4

var cards: Array[Card] # or whatever a card is represented with

func deal() -> void:
    # iterate through all players
    for i in range(PLAYER_COUNT):
        var player_cards: Array[Card] = []
        # loop 4 times to deal 4 cards
        for j in range(CARD_COUNT):
            # random index from card deck
            var index: int = randi() % cards.size()
            #  removes the card at index from the deck
            var card: Card = cards.pop_at(index)

            player_cards.push_back(card)
        # what do you want to do with the cards when they're dealt?
        # for example:
        var player: Player = players[i]
        player.cards = player_cards
1 Like

Hi! Thanks for you response. I realized I wrote what I’m trying to do but not what my current issue is. I’ve been following a tutorial on how to build a deck of cards. Below is the set up for the deck.

extends Sprite2D

@export var cards : Array[Resource]
@export var cardScene : PackedScene
@export var Hand : Node2D

func draw() → void:
if cards.is_empty():
print(“Deck is empty”)
var data = cards.pop_back()
var card = cardScene.instantiate()

if card.has_method("set_card_data"):
	card.set_card_data(data)
else:
	card.value = data.value
	card.suit = data.suit
	card.points = data.points
	card.texture = data.texture


card.global_position = Hand.global_position
Hand.add_child(card)

pass

Then there is this draw function on its own Script:

extends Node2D

func _on_button_pressed() → void:
$Deck.draw()

pass # Replace with function body.

However, when I test it out and press the DRAW button, I get this error message and I know it’s in reference to the textures I’m using for the cards, I just can’t figure out why. My textures are all PNGs.

Error Message: Invalid access to property or key ‘value’ on a base object of type ‘CompressedTexture2D’.

The error is saying that the variable ‘value’ doesn’t exist in the object you’re trying to access it from; that object is data in this case.

Since none of the properties you’re accessing exist in CompressedTexture2D, I assume you’re using a custom class for the card data? If so, data is not an instance of that class for some reason.

Are you putting the right variables into cards? Because evidently, at the time of the crash there is a basic CompressedTexture2D in there

To be honest, I’m not too sure. I followed the guys tutorial to the T. This is the only issue I’m having. I’ve redone it twice thinking I missed something but my code looks just like his. Unless there’s a change he made without pointing it out. I may try it all over one more time or see if I can find a different way to make the.

It looks like you’ve just got a texture in the cards array… did you drag the full resource or just the texture? It should look like this:

By the way, if you give your card data resources a class name, like this:

class_name CardData
extends Resource

you can use that as a type for your array, that way you can only add card data objects:

# instead of @export var cards: Array[Resource]
@export var cards: Array[CardData]