You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a Resource of type Deck. Every Deck has an array of cards of type Card, also a Resource. I can create new cards in the Deck Resource from an add_card function which creates a new Card and appends it to the array. I then save the Deck with ResourceSaver. This works as you'd expect.
Now, I also want all cards to hold a reference to the Deck it's in, so in Card, I have a variable called deck, of type Deck, and in the add_card function I assign it with self, since it is in the Deck Resource. When I do this I can no longer save the deck resource with ResourceSaver, the game simply shuts down with a vague message about the Debugger not running anymore. I assume Resource cant handle this type of circle reference, so not sure if this is a bug or if this working as intended.
If this is how it is supposed to work, then I suggest expanding the error message and not simply crashing the app.
Steps to reproduce
Deck Resource
extends Resource
class_name Deck
@export var id : String
@export var cards : Array[Card]
func _init():
id = str(Time.get_unix_time_from_system()).left(-6)
func add_card():
var new_card : Card = Card.new()
new_card.deck = self
cards.append(new_card)
Card Resource
extends Resource
class_name Card
@export var deck : Deck
Main script
extends Control
const SAVE_PATH = "res://projects/"
var deck = Deck.new()
func _ready():
for i in range(2, 6):
deck.add_card()
ResourceSaver.save(deck, SAVE_PATH + deck.id + ".tres")
Minimal reproduction project
The text was updated successfully, but these errors were encountered:
While the crash is absolutely undesirable, and should be fixed, it's not correct to have circular references like this, you need to use weak references to break the cycle (Card should have a weak reference to Deck, using weakref)
My suspicion is that it's caused by infinite recursion, which I'm not sure how to combat here, in this specific case I think you'll be better off handling this by setting the parent reference on adding the card, not serializing it
Edit: Using weakref prevents crash, but I don't think you need to or should store the data in that way, instead I think you should make it so it handles that on addition
Godot version
4.1
System information
MacOS 11.7.4, v4.1.stable.official [9704596], Vulkan forward+
Issue description
I have a Resource of type Deck. Every Deck has an array of cards of type Card, also a Resource. I can create new cards in the Deck Resource from an
add_card
function which creates a new Card and appends it to the array. I then save the Deck with ResourceSaver. This works as you'd expect.Now, I also want all cards to hold a reference to the Deck it's in, so in Card, I have a variable called
deck
, of type Deck, and in theadd_card
function I assign it withself
, since it is in the Deck Resource. When I do this I can no longer save the deck resource with ResourceSaver, the game simply shuts down with a vague message about the Debugger not running anymore. I assume Resource cant handle this type of circle reference, so not sure if this is a bug or if this working as intended.If this is how it is supposed to work, then I suggest expanding the error message and not simply crashing the app.
Steps to reproduce
Deck Resource
Card Resource
Main script
Minimal reproduction project
The text was updated successfully, but these errors were encountered: