Hello, newer user here, trying to figure out how to display an inventory system in a roguelike deckbuilder ala Balatro.
The initial premise is that every card is an array of 3 values / var. These are organized as a custom class that I named CarD, because I’m a psycho.
class_name CarD
var base:int
var face:int
var suit:int
var card:Array = [base, face, suit]
These values dictate which of the 4 traditional suits the card is, it’s “power” level Ace - King, and the card’s condition / durability out of 100.
At the moment, I have created a control scene that has several Sprite2D children and a script in the root node that has a couple of functions that use the Sprite2D children to display the appropriate png according to these 3 variables stored in the card array.
oControl_Node
Lbase_sprite_2d
Lface_sprite_2d
if base >= 75:
$base_sprite_2d = load("full_health_sprite.png")
if face == 1 and suit == 1:
$face_sprite_2d = load("ace_of_clubs_sprite.png")
etc
I also have an autoload script which houses a variable that contains 52 arrays which each represent a unique card in a standard playing card deck, created with a function that calls on the custom card class, though I have tried just doing it manually as well.
var playerdeck: Array
func create_startdeck():
for suit in range (1,5):
for face in range(1,14):
var startcard = CarD.new()
startcard.base = 100
startcard.face = face
startcard.suit = suit
playerdeck.append(startcard)
My plan / hope was that I would be able to call on a specific index in the var playerdeck (typically [0], representing the top card, and the one drawn after discards and played hands), apply it to the control node with the sprite scripting, and then display the sprite control scene in a container according to the game need. In the current case, I’d just like to display the deck in it’s entirety in a sort of inventory display using a grid container.
I originally planned to do this by creating a grid container, attaching a script to it, then use the script to parse the playerdeck from the autoload, apply the variables to the sprite scene, then .add_child or somehow populate the grid with that instantiated scene. Repeat for each card in the deck. I know how to pull the CarD arrays from the playerdeck, in this case by index[0] and by .front, but I am unsure how to transfer this information to the sprite control scene I preloaded in an @onready, and finally how to instantiate this scene into the grid container.
@onready var inventory_card = preload("AforementionedControlScene.tscn")
var topcard = Autoload.playerdeck[0]
I have gotten as far as:
$grid_container.add_child(inventory_card)
but the only way it shows anything is if I hard-code in the base, face, suit values into the preloaded control script.
extends Control
var base = 100
var face = 1
var suit = 1
func ready():
if base >= 75:
$base_sprite_2d = load("full_health_sprite.png")
if face == 1 and suit == 1:
$face_sprite_2d = load("ace_of_clubs_sprite.png")
etc
but I get the error:
‘GridContainer’, already has a parent ‘GridContainer’
Which I think means that the .add_child() is an inappropriate function, and so is .add_sibling(). I might be able to sus this one out on my own, but since I’m here…
This is the direction I have been led by most tutorials before they begin to implement their own custom resources which seem to exclusively be static items with set values rather than more fluid, “breakable” ones. Stuff like swords and health potions, with a single unique sprite, and some set values that don’t seem to change. Since the tutorials only cover initial setup, I am unsure if or how they might implement a durability system that impacts the sprite texture, and the item’s very existence within the inventory. This is essential to my gameloop, and so I figured it must be handled immediately.
I have worked a bit with custom resources on this project, but I am having a hard time understanding how to implement them in this scenario, as I don’t know if the Resource class can accommodate images, and I am unsure if I can use them to apply the texturing function efficiently.
I have also seen some work done with Dictionaries, and it is probably a symptom of my inexperience, but I struggle to see the benefit to a Dictionary in this scenario, and their utility honestly confuse me to an extent. Arrays are simple for me, all I have to do is worry about a position, not a bunch of keys .
I am basically hoping to keep the CarD arrays stored in the playerdeck autoload unique, and rather than shallow or deep copying them into other scenes as necessary, doing all of the durability damage and other effects to the one copy of the card in the autoload. I understand this might be the setup for disaster, but it also seemed a lot lighter on the coding side (at the time).
As for displaying the deck as a whole, there is a definite chance that the size of the deck will change due to the durability system. The deck will not always be 52 cards, and it may be more at some point, so I am hesitant to try and create a system with a set number of slots, and instead hoping that a grid container would be able to dynamically accommodate and display them in a set number of columns, in this case 13.
ie (pardon the terrible ascii)
▯ ▯ ▯ ▯ ▯ ▯ ▯ ▯ ▯ ▯ ▯ ▯ ▯
▯ ▯ ▯ ▯ ▯ ▯ ▯ ▯ ▯ ▯ ▯ ▯ ▯
a 26 card deck
▯ ▯ ▯ ▯ ▯ ▯ ▯ ▯ ▯ ▯ ▯ ▯ ▯
_______________▯ ▯
a 15 card deck
_____________▯ ▯ ▯
a 3 card deck
I am still trying to do my due diligence and research as many available tutorials as I can. Unfortunately at this point, I have tried to follow more than a few line for line and been left hanging because they use methods that I do not think are effective in my usecase. Though at this point I would be (somewhat) happy to learn that custom resources are what I should be using, I’m just hoping that I can cut to the quick here and get some learned advice.
I think I could bumble my way through this, writing specific code for every instance, making each possible card sprite and loading them specifically instead of in 3 parts. It just feels like there should be an easier way to strip the front element out of an autoloaded array, use the variables stored within that front element to inform three sprite nodes in a preload, then .add_child / instantiate them into a container.
I appreciate your time and patience in reading this novella and any advice you can offer.
Thank you so much!