Placing cards a fixed distance apart works until the hand grows: in our Godot 4.7 test 120 px cards 130 px apart made a hand 900 px wide at 7 cards, 1,290 px at 10 and 2,590 px at 20, in 800 px of room. Sharing the room by the number of gaps fixes big hands but breaks small ones: one card got no usable position at all, because count - 1 is 0 and Godot divided by zero, and two cards were pushed 680 px apart. Using the normal gap while the hand fits, overlapping evenly only when it does not, and centring the result kept every hand we tried, from 0 to 20 cards, within the 800 px.
The code
Save it as hand_layout.gd, load it with const HandLayout := preload("res://hand_layout.gd") and position the cards with var xs := HandLayout.positions(hand.size(), 120.0, 800.0), then card.position.x = centre_x + xs[i] for each card. Call it again whenever a card is drawn or played. Tween the cards to the new x if you want them to slide.
extends RefCounted
## Lays out a hand of cards centred, side by side while they fit, and
## overlapping evenly once they do not, so the hand never runs off the screen.
## var xs := HandLayout.positions(hand.size(), 120.0, 800.0)
## for i in hand.size():
## hand[i].position.x = hand_centre_x + xs[i] # left edge of card i
## Works for 0 and 1 card too.
static func positions(count: int, card_width: float, max_width: float, gap := 10.0) -> PackedFloat32Array:
var step := card_width + gap
if count > 1:
step = minf(step, (max_width - card_width) / (count - 1)) # squeeze only when needed
var total := card_width + step * maxi(count - 1, 0)
var xs := PackedFloat32Array()
for i in count:
xs.append(-total / 2.0 + i * step)
return xs
static func width(count: int, card_width: float, max_width: float, gap := 10.0) -> float:
if count == 0:
return 0.0
var xs := positions(count, card_width, max_width, gap)
return xs[count - 1] + card_width - xs[0]
What we measured
Cards 120 px wide, a 10 px gap and 800 px of room, laid out three ways. Two runs gave the same numbers.
- A fixed 130 px step: 640 px wide at 5 cards, 900 at 7, 1,290 at 10, 2,590 at 20, starting at 0 and running off to the right.
- Room divided by count - 1: one card with no usable position (not a number), two cards 680 px apart, five cards 170 px apart, always 800 px wide even when the hand is small.
- The script: one card centred from -60 to 60, two cards 130 px apart, five cards 640 px wide from -320 to 320. From 7 cards on it squeezes: 113.3 px apart at 7, 75.6 at 10 and 35.8 at 20, always 800 px wide from -400 to 400. An empty hand gave no positions.
Why it happens
A fixed step only knows the gap, not the room, so the hand just keeps growing. Dividing the room by count - 1 always uses all of it, which looks fine for a full hand but pushes two or three cards to the far edges, and for one card it divides by zero. The script uses whichever step is smaller, the normal one or the one that fits, and centres the hand on the total width it ends up with.
What to change
- A fan of cards turned around a point: the same idea for angles, see a centred fan without dividing by zero.
- The shake when a card hits: screen shake. The numbers it deals: floating damage numbers.
What a played card looks like: the Card Game Feel Kit for Godot 4 plays cards with swoops, burns and shatters, with damage numbers and screen shake built in.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Card Game Feel Kit for Godot 4 plays cards with swoops, burns and shatters, with damage numbers and screen shake built in.
More how-tos for Card Game Feel Kit for Godot 4: Screen shake that returns to center
More how-tos: UI and HUD
- A screen fade that finishes while the game is paused or slowed
- Edge markers that stay on the rim and point the true way
- A wrapped Label that is as wide as its text, up to a limit
- Big numbers with thousands separators, minus signs and all
- Show a 0 to 1 ratio on a progress bar, and fill a ring or gauge from the angle you want
- Blink or fade a tinted HUD element without losing its color
- Spin a HUD ring, radar or icon in place instead of around its corner
- Make buttons clickable again under a full-screen fade, transition or post-process ColorRect
- 9-slice window: corners that do not stretch
- Visual novel background: fill the screen, crossfade scenes
- Coins that fly to the HUD counter
- Floating damage numbers in 2D
- An icon over a 3D character's head, without the ghost behind the camera
- Drag a box to select 3D units, in any direction
- Radar blips that turn with the player
- Pick the radial menu slot the mouse is pointing at
- Show a percent that never says 0% or 100% too early
- Type dialogue at the same letters per second on every line and every frame rate
- Place brush dots at the same spacing however fast the mouse moves
- Move a speedometer or gauge needle smoothly without cutting through the bottom
- A countdown that never shows 0 while time is left
- A coin counter that rolls up and lands on the real total