It's not working and IDK what to do now: drawing card from deck & adding to hand

Godot Version 4.4

Question

Background:
ok so some background 1st: I’m working on the card/deck portion of this game. The part I need help with is when clicking the deck, a new card is taken from the deck and put in the players hand. I’ve transitioned from having scenes ‘hard coded’ in the main display and am now using globals because they don’t work correctly (as if it works now) when I try to use a UI.
Moved them from root - PlayerHand to root- Hud - PlayerHand so that it sticks when the camera moves.
so everything now is instantiated in the global and main scene handles positioning. Currently they are just being added to root for bug fixes and such and once I can get it work with globals I can add it to the hud

so a short version of global looks like:

@onready var player_hand_ref = preload("res://Scenes/player_hand.tscn")
var player_hand
var hand = []

func _ready() -> void:
	player_hand = player_hand_ref.instantiate()
	hand_container = player_hand.get_node("NinePatchRect/GridContainer")

deck.gd - sets card info then sends card to player hand.

	var card_image_path = str("res://Cards/", new_card.type, "/", card_drawn_name, ".png") 
	new_card.get_node("CardImage").texture = load(card_image_path) 

path name is correct as i have print statements littering my code as i try to fix things so it looks like res://Cards/Unit/Knight.png

player_hand.gd

func add_card_to_hand(card):
	if !Global.hand.has(card):
		
		Global.hand.insert(0, card)
		Global.hand_container.add_child(card)
		cards_in_hand += 1
		update_hand_positions() #just does a queue_sort

card is being passed properly, container is initialized and i’m getting no errors; so code wise, everything is working fine but I am not visually seeing a card none-the-less in the player hand container. Oh and i’m using a container so it can deal with the cards position.

I had everything working at one point before i transitioned into using globals for the use of a hud.

Global.hand is an array, so Global.hand.size() tells you how many things are in it; your cards_in_hand variable is probably redundant.

Is Global.hand_container actually being added to the tree somewhere?

haha, yea that’s true; i’ll probably change that.

so the way i figured it worked is hand_container is set to a node in player_hand scene which looks like

so when adding a child it will add it to the tree correct?

Only if PlayerHand is also in the tree.

ah yes, they are added in main.gd sorry:

func _ready() -> void:
	add_child(Global.player_hand)
	add_child(Global.mana_man)
	add_child(Global.map_man)
	add_child(Global.deck)
	Global.player_hand.position = Vector2(0,759)
	Global.mana_man.position = Vector2(136,250)
	Global.deck.position = Vector2(135,607)

So, the debug printing seems to say everything is working but you aren’t seeing things on the screen? Maybe have a look at the global_position of the cards and see if they’re being drawn offscreen?

so I added a print after update_hand_position() in player_hand.gd and it’s showing 0,0 for all the new cards. I’m so confused because I thought the whole point of having a container was so that it controls the position of things rather than hard coding it.
this whole issue stems from me wanting to use containers because I need a simple UI. I mean realistically the player hand & deck will be in the same position each game but because I was trying to add a hud I needed to do things globally because of UI integration. IDK maybe I’m doing everything wrong lol


I used to have the player_hand and deck in that tree and call them that way; then i looked at some UI tutorial and essentially the camera has to be in a canvas layer right? well I want the deck and hand to not move so also stick them in the HUD. IDK i feel like i’m not calling things correctly or going about this in a complicated manner.

I have camera controls but nothing shows up at 0,0

The grid container is a mystery to me but some of the other containers will force child nodes to minimum size. Check that you have set the minimum size of your cards.

I just wanted to say that the grid container is amazing! Just add children and they are all organised and neat for you automatically (depending on how many columns you have given it). I only used it recently for the first time and it was a revelation!

No it does not. A canvas layer just adds a new layer that can be drawn on independently. A camera controls the viewport and what is visible in the viewport.

Sorry to multiple post.

Your HUD is correct for an independent layer. Add as a direct child a control node to control your containers. Then add a child margin container to give some margins to your card deck. Make the control node fixed centrally to the bottom center. Then add a bottom margin to lift it up a bit on the margin container.

Now add control nodes, perhaps a HBox container, horizontally centered, with each card a child of this. You can then spread them out with the spacing control. Now all your cards will always be at the bottom center of your viewport.

Hope that helps in some way.

For each bar, left bar, right bar, bottom bar, just add three children to the control node (which should be set to cover the whole screen) and position the bars left center, right center, and bottom center. They will now all be fixed in their relevant positions which you can add content to. Probably Vbox containers for the left and right and a Hbox for the bottom one.

Control nodes are powerful, flexible and just work 100%. They get some criticism but I disagree, the level of flexibility and control they offer is astonishing.

I figured it out; after… so so much tweaking and fiddling and doing all kinds of crazy stuff.

So the issue is that the canvas layer does not pass raycasts. There was/is this whole big thing from V3.x that control nodes where not passing raycasts (resolved in v4) and my issue is the same but with the canvas layer. so instead of using raycasts to handle this drag function it needs to be signals; button signals (which lead down another rabbit whole)

In the end because of the position with the tree under the canvas layer; card scene & deck has to be a control node with a button (or just a button?). that button emits the signal it’s clicked and with code it can now be added to the container and dragged around the screen as I want it to be. signals and dragging info can be found on documentation or google.