How to create a reusable Custom Template class

Godot Version

4.7.2

How to create a “template node” to reuse it

Hi everyone,

I am learning Godot and am currently recreating Poker and was trying to figure out how to create a “Card” class that I can add to the hand node whenever the player draws, but with no success

extends Node2D

func add_card(card: Dictionary):
	#Creo le variabili e il filepath per la texture in base ad esse
	var suit = str(card["Suit"])
	var value = str(card["Value"])
	
	#Creo il filepath per la texture
	var texture_path: String = "res://Sprites/Cards/card_" + suit + "_" + value + ".png"

	#Temp var to save the node infos
	var new_card = Sprite2D.new()
	new_card.texture = load(texture_path)
	
	#Temp var to save the button infos
	var new_card_button = Button.new()
	new_card_button.position = Vector2(-20, -29)
	new_card_button.size = Vector2(40, 58)
	
	self.add_child(new_card)	#self = $"."
	self.get_child(self.get_child_count() - 1).add_child(new_card_button)

How would you go to implement this by using a custom class that I can just call with new data whenever the player draws a new card from the deck?

I have a Card class that consists of both a Sprite2D and a Button, but I can’t access its .new() method when I try

You’d typically make a card scene, consisting of all nodes and scripts that make up the card.

Then instantiate this scene either through code or in the editor and add it to your main scene.

I recommend going through introductory tutorials in Godot docs to get acquainted with engine’s key concepts: