Resource recursion for branching and repeating dialog system

Godot Version

4.2.1 stable

Question

Dear community,

I am currently developing a dialog-based 2D Game, in which the player can visit different locations that each have their own dialog tree. I am using custom resources as both the dialog frame and dialog choices. The structure of those trees is as follows:

dialog.gd

extends Resource
class_name Dialog
@export var title: String
@export_multiline var description: String
@export var choices: Array[DialogChoice]

dialog-choice.gd

extends Resource
class_name DialogChoice
@export var title: String
@export var description: String
@export var requiredResources: Array[ChoiceRequirementResource]
@export var nextDialog: Dialog

This approach is working just fine, unless one dialog choice leads back to a previous dialog frame (arrow marked in red). This triggers a “recursion detected” error when attempting to input the resource. I understand why I am getting this error, I would more so like to know if there is an elegant way that would enable this use case. I would really like to stay away from naming a resource property instead of the resource itself, as it might get really annoying to maintain and change things later on.

Thanks and best regards

I’d suggest using a path instead and using WeakRef to handle the storage

So when the parent dialog is created you can have it call a method on the child (so Initial Dialog calls on Dialog 2) and tells it about itself so it can know it, and then store it with WeakRef, something like this, you can then use helper methods to access it from the WeakRef and have it have typing

It depends entirely on how you do things, but that is the way to handle these kinds of circular references :slight_smile:

You also don’t necessarily need to use Resource for this, I think this might be better served by custom Nodes, that will allow you to have a more complex relationship between them (especially as Resource is designed to be reusable, used in many places, without knowing “where” it is, but Node is designed to be used in a hierarchy and relate to where it is in the scene and world, this is how GraphEdit works, it doesn’t use resources, but instead uses Nodes) You can also have the relationship between the dialog options external to them, so instead of having each option know each other you can store the links between them outside, so you have a DialogTree which contains DialogNode and the tree handles the links

1 Like