#Trouble with card resources
173 messages · Page 1 of 1 (latest)
Thanks
extends Node
var card = preload("res://card.tscn")
var cards_per_deck := 52
var deck_of_cards : Array = []
func _ready() -> void:
for i in cards_per_deck:
var card_instance = card.instantiate() # instance each card
card_instance.card_stats.number += 1 # starts at 1 and increments
deck_of_cards.push_back(card_instance) # pushes card into array
add_child(card_instance)
if (card_instance.card_stats.number == 14): # every 14 cards
card_instance.card_stats.number = 1 # starts a new set of cards at '2' again
card_instance.card_stats.suit_number += 1 # starts a new suit
if (deck_of_cards.size() == 52): # if array is filled with cards
for x in cards_per_deck: # for every card
print(deck_of_cards[x].name_label.text + " of " + deck_of_cards[x].suit_label.text) # print array info
extends Control
@export var card_stats : Resource
@onready var name_label = $NameLabel
@onready var suit_label = $SuitLabel
func _ready() -> void:
set_card_labels()
set_card_suits()
func set_card_labels() -> void:
if (card_stats.number == 11):
name_label.text = "Jack"
elif (card_stats.number == 12):
name_label.text = "Queen"
elif (card_stats.number == 13):
name_label.text = "King"
elif (card_stats.number == 14):
name_label.text = "Ace"
else:
name_label.text = str(card_stats.number)
func set_card_suits() -> void:
if (card_stats.suit_number == 1):
suit_label.text = "Hearts"
elif (card_stats.suit_number == 2):
suit_label.text = "Clubs"
elif (card_stats.suit_number == 3):
suit_label.text = "Spades"
else:
suit_label.text = "Diamonds"
card_stats resource
extends Resource
class_name Card_Stats
@export var number := 1
@export var suit_number := 1
Had some extra copy and paste int here
func _ready() -> void:
var number = 2
var suit = 1
for i in cards_per_deck:
var card_instance = card.instantiate() # instance each card
card_instance.card_stats = card_instance.card_stats.duplicate()
card_instance.card_stats.number = number # starts at 1 and increments
card_instance.card_stats.suit_number = suit
deck_of_cards.push_back(card_instance) # pushes card into array
add_child(card_instance)
if number == 14:
suit += 1
number = 2
else:
number += 1
@grand idol lmk if that works
oh wait
there
Nem, I want to be honest here, Not really looking for a solution to the project. Moreso wondering whats goin on with my current one. Why it looks like it is working, and why childing to GridContainer changes things.
I seem to have some big gaps in understanding and am trying to fill those in
sure
well it really should make 0 difference what the instanced nodes parent is
what did you find when u check the remote scene tree at runtime?
what were the values of the cards card_stats resource?
both values on resource were showing as 1 on each card instance
but all text values were correct, both number and suit
Which I don't get
I can see how, if the resource is shared, my line 17 reset the card_stats.number back to 1 at the last for-loop statement. but the card_stat.suit_number never gets reset in code, so it makes me believe they never actually change.
ok so I imagine what was happening was that after each add_child call
that nodes _ready function ran
and at that time, the resource stats were incremented
so your labels were being set with the correct values
I uploaded the project to github if it helps
ya sure you can link it but I dont think it's necessary
https://github.com/SamDevelopsCode/Poker
Yea no need, I get its an extensive thing to look that deep
you were running each cards _ready function with a shared resource that you were incrementing
thats the gist of why it seemed to work
How does suit_number end up as 1 tho?
on the resource. If it incremented correctly, how did it reset
What about the GridContainer mystery lol.
Only in the resource itself.
And incrementing in that for loop Like Ive shown
lol there is no GridContainer mystery
What's your issue? Happy to take a quick look at the project
Oh well therers confusion for me still lol
what changed when you switched to GridContainer as the parent
The gist is this:
I have 4 Nodes:
Main
CardSpawner
GridContainer
and a seperate Card scene not in the scene tree.
I have a CardSpawner script
and a Card script, The Card script uses a simple resource for 2 values. The number of the card, and the suit number of the card
I am trying to create a full deck of 52 cards. It seems to be working when adding Card instances as children to CardSpawner. It breaks when making GridContainer the Parent
The original error that spurred this whole learning adventure.
so you werent getting that error when adding to the spawner?
Correct, yea, it all seemingly works with 'labels' and the such
When adding directly to the CardSpawner
Even though the resource values both show as 1
Same. lol, The print wasn't working
it might just be because your spawner was a node
and not a canvas item
change the node to a node2d
and see if you get the error
Glad I could help
lol
Oh no, I mean it still isn't working. I can get up to the point you did.
I think its a matter of update order for a non canvas item node
so I think you'll get the text error on any node that is a canvas item
like node2D or control or anything that draws to the screen
it would likely give you the same error on any control node or Node2D
What isn't working? I put them all as children on the grid container and your print statement is working
you probably ran the script from the spawner still
i imagine he moved the instancing logic into the gridcontainer script?
My print is still not working, even after adding the children to GridContainer
Min ei
ok lemme download this project lol
Oops mine is haha
can you show your scene tree
Are you switching it around somehow or changing node types
I made grid container child
@onready var grid_container: GridContainer = %GridContainer
Right, my original OG question was why doesn't it work adding them to GridContainer when it is a sibling to CardSpawner
How are you getting reference to sibling?
because its an order issue
Through the $GridContainer?
Ok I get what ya'll are saying to do, swap the positions. trying to understand why that matters in this context to broaden my knowledge
I would just have the container as a child tbh
I usually have plain parent nodes with scripts that have mutiple child node regs
*refs
Mm....Yea I'll have to do some reading once my head clears a bit. Been alot of back and forth trying to understand this. Appreciate you both. I've read that Tree ready order by kidscancode but clearly need another read.
Good luck! 😀
Thanks, you too
One last thing pls!
lol
I can start to see why the order matters. What about the resource value being 1 for every card?
lemme see now that i have the proj
Thanks
I'm using a breakpoint in the loop and never see either of the values change in the Remote tab for the resource
its also seems to only show the all the cards at once, once the loop is completely finished. I thought it would show each individual card when added as a child to GridContainer?
did you just use a print statement to figure that out?
yup
probably that the remote debug inspector doesnt show actual resource values
in this case it seems like it only shows the default resource values
oh i wonder
Ok good to know. And last question for today I swear.
Would you have even used a resource here or just some regular properties on the Card script?
I just wanted to understand resources a little bit but doesn't seem like this is a good use case
Argh lol ok. Just inexperience at this point
ok here's why
you never actually store the resource in the card
you create a floating reference to a resource
So by me dragging in the resource from the FileSystem into the exported Resource slot, that is just a reference, not the actual resource?
but once i add
card_instance.card_stats = Card_Stats.new()
for one you dont have to worry about 1 shared resource because that creates a unique one
but also
notice how the resource is now an actual object
yah it's a lil convoluted
Did I just get lucky that my implementation used the resource in a way that sharing actually makes it work
np
so for unique resources u can just straight up create a new one after you instance a node
card_instance.card_stats = Card_Stats.new()
So thats calling the ClassName.new() to create a new object of the resource?
and you can also look into _init() where if you're familiar with constructors you can pass the values directly into the .new() function
card_instance.card_stats = Card_Stats.new(number, number_suit)
correct
I know of constructors and of things, but understand? No. Clearly not 😂
I've used constructor in Unity using c#, understand what they are, but I think my brain is fried from understanding all this for 2 hours
Need to practise more OOP
haha it would just look like:
extends Resource
class_name Card_Stats
var number := 1
var suit_number := 1
func _init(number : int, suit_number : int):
self.number = number
self.suit_number = suit_number
Right now my OOP is POOP
and then card_instance.card_stats = Card_Stats.new(number, suit_number)
Gotcha. So thats a constructor that when you call .new() it would tell you, "hey, make it like this"
lolz
Alright...well....Im going to go have a good cry, thanks for your help nem
would upvote you if i could
Think I may wanna do a platformer again to learn Godot's "ways" a bit more before I dive into a more data oritented porject, however simple poker may be
Do i close this thread?
I'd recommend just make what you want and overtime you'll learn. That way it feels less like active learning and you dont burn out as easy
yah not sure how to close it
take care!