How to make a Players Hand in a Card Game

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

I having problem trying to figure out how to make a Player Hand for my card game on Godot, the initial idea was to use a Area2D so when the Node that represents the card is above the area the card would snap to 1 of 3 locations in the players hand. How do i accomplish that? Right now i have the “Draw System” and the “Drag and Drop System” ready.

:bust_in_silhouette: Reply From: Wakatta

While it is tempting to use nodes for everything you should also avoid mixing GUI elements with functionality.

Instead use code to reflect game state changes then visuals to represent those changes.

In your above query you can assign each card a unique code let’s mimic for example Yu-Gi-Oh.

You can then have a list with every card code

var cards = [2345, 9743, 2574, 36737, 5746]

To generate a hand or even a deck you can randomly choose codes from the cards list

 var hand = []
 var hand_size = 3

 for card in hand_size:
     hand[card] = cards[randi() % cards.size()]

You can have each card image named after the codes
Then load each card as needed

func add_hand():
    for card in hand:
        var card_holder = TextureRect.new()
        var card_sprite = load("path/to/image/%s.png" % [card])
        card_holder.set_texture(card_sprite)
        add_child(card_holder)

Everytime you change the hand array show that visually as well

hand.shuffle()
for card_holder in get_children():
    var card_sprite = load("path/to/image/%s.png" % [card])
    card_holder.set_texture(card_sprite)

I don’t quite get it how to do that, i still not 100% familiar with Godot syntax and how everything interacts with each other, i took me a week of studying just to understand some nodes aspects T-T

DeathArcanaXIII | 2022-11-21 21:29

:bust_in_silhouette: Reply From: stormreaver

For my VR Uno game (not finished or released), I manually defined a separate hand class for each of the 108 possible card quantities that could be held. Each hand class consisted of an appropriate number of Position3D nodes that would indicate the position and orientation of each card in the hand (yeah, it was a lot of work). I put them in a separate layer from the ones they scanned for collisions so they wouldn’t fight with each other when overlayed.

Each card has its own collision shape so I can use a short raycast from the controller (I didn’t use hand-tracking) to indicate card selection.

As the number of cards in the hand changes, I queue_free() the current hand class, load the hand class that corresponds with the number of cards now being held by the player, then pass in that list to the hand class.

The end result was an accurate depiction of what was being held.