Nodes VS. Resources

Today I learned to use resources. After I realized how useful they were, I had an “I understand it now,” moment. But shortly after, I completely forgot what the point of them are and why use them over nodes, kinda like forgetting a dream after waking up.

Right now i’m imagining them as scripts but if they were more tangible? Like a node if instead of attaching a script, you “merge” it.
I also think they are useful cuz they don’t need to be in a scene unlike nodes, but I’m not even sure that’s true :stuck_out_tongue:

My brain hurts, help.

Basically, resources are data containers used to store data for Nodes to use.

For example, if you want to persist the health, ammo, and coins values of your player when you change scenes, or when you close ans open the game again. you can have a resource called player_data that stores these details. The stored resource is basically a text file you can edit yourself.

Once the player is loaded in a scene, to persist the data, just load the resource and get your values.

I hope this helps.

3 Likes

However, as Resources cannot hold circular dependencies and thus those information get lost while saving a Resource with the ResourceSaver, you cannot use them to model a data-relationship. In this case, you need nodes.

As an example: You have a car with four tires. The car has values you want to store (position, speed, damage) and the tires have values worth saving, too (wear, temperature). If you also need the information that a car has a specific set of tires and each tire has to know at which car they are currently attached, you would lose at least one relationship information during saving.

I guess you can still restore that circular relationship if when you load the node from the resource. When you know which tire is coming from which car resource, you can reassign the variables when you load.

Yes, you can but

  • As ciruclar dependencies are unsupportet, you would need to load the parent (car) first and propagate the value restoration downwards. If you load the child (tires) first, you have no chance of determining if they have an parent at all - or you could need some additional variables or pattern matching voodoo.
  • It makes the loading more complex eliminating the single most important benefit.
  • Nodes look like a feasible compromise for storing pure data. The “scene tree overhead” must be seen if it is truly causing significant performance penalities.

Having a node based data structure gives you also the benefit of having a runtime visualisation in the Godot editor during development.

But don’t get me wrong, I like the Resource approach but the circular dependency “bug” is a deal breaker for me. I read in one issue ticket that it was stripped from the ResourceSaver because it caused memory leaks. I don’t know which kind of memory leaks, but if it is an uncontrolled recursiveness, I think it should be solvable. Currently you save and load a circular dependency with a null value. Not very clean and well implemented either.

1 Like

What’s the disadvantage of saving as a node rather than resource?

I’m not in ‘save the game’ step yet in my game. But I should start thinking about it at the background. So any input is very valuable for me.

You can only save nodes as a packed scene, resources can be saved much easier and as only one rseource. Still it’s better to save data in other formats than packed scenes or resources directly as they both could load and execute unwanted scripts.

1 Like

@FreakyGoose nailed the answer up there but here’s an easy way to remember how Resources work.

Lets say you’re creating row of 3 buttons for your UI using TextureRect2D nodes. You can add a different texture to each of these buttons by selecting a new Texture resource from the dropdown.
godot_texture_resoruce

Those are resources. This example extends to pretty much every node you’ve ever used in Godot – CollisionShapes and Materials also come to mind. Each node allows you to customize its data by adding a new resource (in this example a texture, shape, or material).

They’re all resources and making your own custom resource affords you the same benefits.

1 Like

There’s no “vs” between nodes and resources. Those two types of objects serve different purposes. They supplement each other. In very simplified terms - a node draws something on screen, a resource loads something from disk.

So if you want to draw an image that’s stored on disk (like godot icon for example), you’ll need both; load the image as a resource and then assign that resource to a node that will display it on the screen.