Trying to understand the use cases for custom Resources

Godot Version

4.4.1

Question

I’ve seen plenty of tutorials about how great custom resources are, and I can definitely see their use, but I’m trying to understand when it’s better to use a custom resource over something like composition with nodes. It seems to me most things you can do with custom resources you could do by attaching a script to a node and using an export variable to access that node’s script in the parent script.

The two things I can see that are different are that you can export resources directly instead of needing an intermediate node and that resources only load once, meaning any changes to a resource will take effect globally. These are clear advantages to using custom resources, but I feel like I might be missing something.

Also, what are the reasons to use nodes instead of resources? The docs say nodes give you functionality and resources give you data, but resources can define methods and signals, so I’m unclear on what the difference is. Is it that resources are unable to call functions but they can define them for nodes to call?

I recommend you check out this topic: I figured out how to create components without nodes (and it is much better!) The author creates a good use case for using Resources instead of Nodes for use in composition.

Ultimately, the choice is yours. As you make your game(s) you will find places where a Resource works better than a Node. For me, I use Resources the way I would use a Struct in C or C++. Basically I use them as ways to store complex data. They have advantages in that there is a built-in way to save them to disk. It’s also a good way to share the information between objects.

I use a Node when I want to know that something exists as part of an object by looking at the node tree. For example, I often hang Timers off other Nodes even though I could just create one in code and put the time in an exported variable. I do the same thing with my StateMachine and State objects. They actually could be much simpler objects, but I find that utilizing them as nodes allows me to immediately see what states an object has without diving into the code.

Ultimately, I don’t think it’s about “missing” anything. Knowing about them allows you to make design choices. For example, I had a bear of a time saving keybindings to disk using the normal FileManager. So I converted that code to save as a resource, and it became very easy to implement.

2 Likes

(Google translator)

Resources fulfill a totally different function than nodes.

A node and its script represent instances of running objects where each of them has attributes with different values ​​and behaves based on them.

Resources, on the other hand, serve to represent structures or types of data that will be shared between the different objects that run in your game.

In Godot many things are resources. A material is a resource. Why? Because a material that represents a red color can be shared among many objects in your game that need it. The material does not change, it is always the same. It is a resource.

Can resources have methods? Yes, they are scripts too, but those methods should simply be to make the resource a little more dynamic or have ways to communicate its information with the rest of the nodes.

For example, the following could be resources:

Damage:
	type: garlic
	Affected enemies: 
		vampire: -100 hp
		werewolf: 0 hp

Damage:
	type: garlic breath
	Affected enemies:
		vampire: -50 hp
		werewolf: 0 hp

Damage:
	type: silver bullet
	Affected enemies:
		vampire: 0 hp
		werewolf: 100 hp

These resources could be edited in the Godot editor, to define the behavior of your game, and they could be assigned to different weapons or magic. When an enemy receives a certain damage you will be able to see, in the resource, whether or not it affects them and to what extent. If you want to increase the damage of garlic breath in your game to everyone at once you just have to touch a resource.

If at any time you consider that you should save something in a json file, you may also need to evaluate whether that information would be better in resources or not. It is serializable data.

Anyway, look at the documentation, I think they explain it much better there than I can.

1 Like