What's the best way to handle resources with connections to game objects and save data?

Godot Version

4.4

Question

This is a bit of a clumsy phrasing for the question, but here’s what I’m after…

I’m trying to figure out the best way to handle custom resources which are both referenced by the save data, used by in game objects and referenced by certain ui elements. The data in these resources is totally static, but other objects and scripts need to know about them in various places in order to process and display data and textures.

I have a common id for each item which acts as a reference to both a particular element in the save data as well as the corresponding resource, but I’m not sure if this is really ideal? Basically the key value in the save data is the same as an ID variable on the resource and that’s how I tell how they connect. I could store the resource as an element in a sub-dictionary, but I don’t want to save it along with the other information.

So what’s a good general practice for how to handle this? Should I have an autoload that contains all of the resources as constants? Should I make them all custom classes? This latter option seems impractical beyond a certain number of items.

I’ve searched for a good while, but I may not know enough to know what I’m missing, so I may not be asking the question in the best way. Any insight on this would be much appreciated.

I think using a number ID for each item is fine. A lot of games do this. But you need to make sure to not change the IDs too much, since then old save data will not work properly.

Another option would be to use the filepaths of the resource files to get the item information, but that is pretty easy to break.

1 Like

That’s good to know! That makes sense about not changing the IDs too much too. You saying that encouraged me to make a spreadsheet as a reference file.

As to the other suggestion about file paths, I don’t know that I understand this well enough yet, but didn’t Godot 4.3 add unique identifiers for all scripts, data and resources? I wonder if that could be useful for that same purpose without as much risk of breaking? Again, I don’t really understand that well enough to say, but it came to mind.

It was 4.4 that added those… but IDK how uids work :cry:

To the docs! :smiley:

Hmm, it looks like UIDs are the same, even if the files were moved. Maybe that would be useful?

Sounds like it could be. I’ll try some experiments with that this week.