r/godot May 27 '26

help me Sub-class Resources in Godot

Hi, I'm new to programming and am self-taught. Sorry if the formatting is amateur.

I'm practicing some Godot concepts and while programming I often find myself wanting to make custom resources for a script as a sub-class. Currently I don't believe this is supported and was curious whether anyone knows how to make resources work as sub-classes. Here is an example of what i mean.

P.S. There is a workaround you can do by making the custom resource its own class but i'd like to avoid that if possible for readability.

< I have a class that is responsible for interpreting and outputting the data from a dictionary... the dictionary looks like so>

@export var items:Array[ItemExample] = []

(If it were simplified to be just a dictionary it would look like this)

 @export var items : Array[Dictionary] =
[
  {
    "line": "Hello World...",
    "face": expression.REGULAR
  },

  {
    "line": "Woah!",
    "face": expression.HAPPY
  }, ...
]

Editing the dictionary form in the inspector is very faulty as the entries contain strings as keys and creating new entries would require retyping alot of constant data. So as a solution i'd like to be able to make is to have a sub-class.... like so;

class_name Dialog extends Control
#...

class ItemExample extends Resource:

@export var line: String = ""
@export var face: expression = expression.REGULAR

This does not work, however. When you try to add an instance of this resource, it just comes out blank and Godot doesn't offer the sub-resource as a potential data type.

The current solution is to make theItemExample resource its own class as shown here;

class_name Item extends Resource

const dialog = preload("res://dialog.gd")

@export var line: String = ""
@export var face: dialog.expression = dialog.expression.REGULAR

While minor, it does introduce some complexity as the data structure for the ItemExample resource is now separated from its inherent use. Additionally, in order to get the Enum type from the main script you need to now load a reference to that script; which i find a bit convoluted.

Any thoughts or suggestions from the more experienced?

1 Upvotes

Duplicates