Fix: card double-click via remove_child before queue_free + frame dedup guard
Root cause: _rebuild_hand used queue_free() alone, which keeps nodes in the GUI tree (and receiving input events) until end-of-frame. When two state updates arrive in the same frame, stale and live CardWidget instances overlap at the same screen position — both emit card_pressed → _on_hand_card_clicked fires twice per physical click, alternating select/deselect. Fix 1 (root cause): _rebuild_hand now calls remove_child(c) before queue_free(c), so the old widget is immediately detached from the HBoxContainer and can no longer receive GUI input. Fix 2 (defensive): _on_hand_card_clicked deduplicates calls within the same engine frame for the same card instance using Engine.get_process_frames(). Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
@@ -61,6 +61,9 @@ enum Mode {
|
||||
@onready var _go_menu_btn : Button = %GOMainMenuBtn
|
||||
@onready var _hud : CanvasLayer = $HUD
|
||||
|
||||
var count : int = 0
|
||||
var _last_click_frame : int = -1
|
||||
var _last_click_iid : String = ""
|
||||
# ── Affinity colors ───────────────────────────────────────────────────────────
|
||||
|
||||
const AFFINITY_COLORS : Dictionary = {
|
||||
@@ -1038,6 +1041,17 @@ func _hide_ability_panel() -> void:
|
||||
# ── Hand card ArchSpirit routing ──────────────────────────────────────────────
|
||||
|
||||
func _on_hand_card_clicked(iid: String, _panel: PanelContainer) -> void:
|
||||
# Dedup: the same physical click can arrive twice if a stale queue_freed widget
|
||||
# still overlaps the live one (see _rebuild_hand). Ignore the second call in
|
||||
# the same engine frame for the same card instance.
|
||||
var cf := Engine.get_process_frames()
|
||||
if cf == _last_click_frame and _last_click_iid == iid:
|
||||
print("[HandClick] dedup suppressed iid=", iid.substr(0,8))
|
||||
return
|
||||
_last_click_frame = cf
|
||||
_last_click_iid = iid
|
||||
print("Clicked x times: ", count)
|
||||
count += 1
|
||||
if not Game.is_my_turn(): return
|
||||
|
||||
var card := _find_hand_card(iid)
|
||||
@@ -1091,7 +1105,14 @@ func _on_hand_card_clicked(iid: String, _panel: PanelContainer) -> void:
|
||||
# ── Hand display ──────────────────────────────────────────────────────────────
|
||||
|
||||
func _rebuild_hand(hand: Array) -> void:
|
||||
for c in _hand_cards.get_children(): c.queue_free()
|
||||
# Use remove_child + queue_free (not queue_free alone) so stale widgets are
|
||||
# immediately pulled from the GUI tree. queue_free() alone keeps the node in
|
||||
# get_children() and receiving input events until end-of-frame; if two state
|
||||
# updates arrive in the same frame we'd have two overlapping CardWidgets at the
|
||||
# same position → two card_pressed emissions → double _on_hand_card_clicked.
|
||||
for c in _hand_cards.get_children():
|
||||
_hand_cards.remove_child(c)
|
||||
c.queue_free()
|
||||
for card: Dictionary in hand:
|
||||
_hand_cards.add_child(_make_hand_card(card))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user