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