Export a dictionary of custom data struct

Godot Version



Is there any way to have a resource exporting an array of “something a bit more complex than standard types”?

Something like this:

extends Resource
class_name TestResource

class State:
	var description:String;

@export var states:Array[State]

but working…

The above code would produce the following error:
Parse Error: Export type can only be built-in, a resource, a node, or an enum.

I can’t believe I have to create more and more resources just to get what’s essentially an Array of Dictionary with pre-set keys…?

The Resource class contains the stuff that the engine needs to serialize/deserialize objects. So yes, if you want to export a custom type then it needs to inherit Resource. It may also needs to be in it’s own file, not a subclass (so that the serialized data can reference it).

I know this is a bit annoying to work with, but it is how it currently works.


extends Resource
class_name TestResourceState

@export var description: String


extends Resource
class_name TestResource

@export var states: Array[TestResourceState]

Yeah, there might be good reasons for it, but I can’t see the advantage.
It just contributes to fragmentation and proliferation of smaller files.

You could assume that a class defined inside a Resource automatically inherits from the resource class, thus not changing this predicate, but still allowing people to keep the codebase compact.

In my case this results in me having to create something like 4 additional files… :confused:

Yeah I understand, I personally wish it was different too. But at least for now this is the way to do it.

Yeah, well… it is what it is.
So, in light of this, what would be the best approach to do the following:

Say the main TestResource there is a character and it comes with an array of TestResourceStates (upset, happy, sad, etc…) which would be used according to how you interact with it.
Whenever the character resource is created, the states are initialized and one might be active, while the others aren’t. Each state might hold quite a lot of data (dialogue, pictures, etc.)

At this point I’m thinking that when the player will save the game state, I shouldn’t store the whole state of the Resource, savegame files might get needlessly bit, holding the entire TestResourceStates data, while the only thing I would need to save really is the index of the currently active state.

Does it make sense?

Why would you want your resource instances to carry that much weight to begin with?
This seems like an ideal place to be using an enum which can be exported in a Resource.

Well… that was my point in fact :slight_smile:

What I’m trying to understand is: if the serialised resource only stores the enum value pointing at the current state, what data structure is better suitable to store such states?
Just a simple class with a giant dictionary, with the enum value used as key to retrieve state changes?