Trying to understand custom resources

Godot Version

`4.3

Question

I’m making a simple card game to teach myself about custom resources but I’m having some trouble getting my head around them.

My plan is to use custom resources to create each card, so I have 52 unique resources, each of them has a name, a value, and a sprite. I have these resources in an array called “deck.”

What I’m having trouble understanding is how exactly I attach those custom resources to actual nodes or objects that I can see and manipulate in game. My idea is to just have one card template and instantiate it however many times, while assigning it a random resource from the deck. But I’m not sure how I would do that or even if that’s the best way to approach it. Any help would be appreciated.

Use a variable to store the script’s resource, if the extended Resource has a class_name you can declare it’s type with that.

var card_info: CardCustomResourceType

If it does not you can declare it’s type through a preload, though I think this doesn’t work well for exports

const CardCustomResourceType = preload("res://card_resource.gd")
var card_info: CardCustomResourceType

I understand that, but what I don’t understand is how I would actually instantiate the card objects with the data from the resource. They are just resources, they’re not attached to a node, so I’m struggling to understand how to create objects with them.

Hard to say without some code. Resources have no control over the SceneTree, so it won’t be them creating the Node objects. I figure you would have a CardManager/Deck node with a array of Resources, when it’s time to draw the Deck would pop a random resource, create a card, and give the new card it’s resource or fill in info based on the resource.

Here is what my code looks like:

I have a “Card” class that looks like this:
image_2024-11-19_094105383

With this, I create the 52 custom resources, one for every card in a standard deck of playing cards and put them in an array:
image_2024-11-19_103729653

Then I have a card template called “card_temp” and it has a reference to the custom resource as an export variable, and the same variables as the card class:

image_2024-11-19_120332015

And this is where I get stuck. I can instantiate the card template but I don’t know how to give it the resource values. There’s something I’m missing here and I don’t know what.

For the sprite you can just transfer it (I would re-type card_info):
:
@export var card_info:Card

func _ready()->void: 
   sprite = card_info.sprite
1 Like

hope you figured it out by now but the way I do it is having something like this:

const CARD_SCENE_PATH = "res://scenes/card.tscn"
var player_deck = ["eye","goblin","mushroom", "skeleton"]
const CardData = preload("res://scripts/card_data.gd")
func _ready() -> void:
	player_deck.shuffle()
func draw_card():
	var card_drawn_name = player_deck[0]
	player_deck.erase(card_drawn_name)
	var new_card = card_scence.instantiate()
    new_card.PROPERTYYOUNEEDTOCHANGE = CardData.property
	$"../Card Manager".add_child(new_card)
	$"../PlayerHand".add_card_to_hand(new_card, CARD_DRAW_SPEED)

the methods of card manager and player hand are to do with dragging the cards and their position on screen not relevant really but reads weird without them to me

(this is with dicts but it functions the same and just the calls are slightly different, I am just yet to implement custom resources as my deck builder is in its early stages so a dict is emulating it for the moment for simplicity)