Is there a better way to store card data for my card game?

Godot Version

4.2.2

Question

Bellow is how I would store data for 1 card,

var card_data = {"Name" : "Shield",
   			"Rarity" : "Common",
   			"type" : "Defensive",
   			"EnergyCost" : "1"}

But how would I store the value for all the cards that I want in my game? How I was going to do it was through a massive array of dictionaries like the one above, stored in an autoloaded script e.g.

var cards = [{"Name" : "Shield",
				"Rarity" : "Common",
				"type" : "Defensive",
				"EnergyCost" : "1"},
				
				{"Name" : "Laser",
				"Rarity" : "Common",
				"type" : "Offensive",
				"EnergyCost" : "1"}]

I want this because I will want to be able to pick a random common, defensive, or energy cost card and having a place where all the card values are stored will help with that.

If it helps to know each card also will have its own scene and a script as there are to many different things the cards will need to do for that to be general. Maybe I could store the scripts in one big array, then just store each card values in each card script, instead of having such a massive array? any ideas will be much appreciated.

If you create a script for every card you might aswell store that data in this script. You can create a “Resource” that holds an array or dictionary of all your card scenes. If a scene wants to access a card it can just preload this resource and access this scene through the resource.
In general Dictionaries are the most common way data is stored, since it is easy to read as a programmer and also efficient

2 Likes

Thanks for answering, so you are saying I should do something like this,

extends Resource
class_name cards
@export var card_scenes = []

Then whenever I create a card just add the card scene to this resource? The only problem is that after I have created this resource there is no option to add scenes


Also can you also explain when I would use a resource vs a singleton?

I believe they mean

extends Resource
class_name Card

@export var name: String
@export var rarity: CardRarity
@export var type: CardType
@export var energy_cost: int = 1
@export var image: Texture2D

enum CardRarity {
	COMMON,
	RARE,
}

enum CardType {
	DEFENSIVE,
	OFFENSIVE
}

Then you can fill out cards even in the editor, as files or making an Array of them would be easier

2024-07-19-175109_522x483_scrot

1 Like

I will definitely set it up like that thanks, I was going to say how would I generate a random card as I don’t have an array of all my cards but I remember that groups exists. Meaning I can add all my cards to a group and get an list by calling SceneTree.get_nodes_in_group().

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.