All Questions

Community

Ajinkya steelx

As resources are shared

does that mean they are like a service which can be accessed anytime ? If yes, how different they are from singletons ?

  • Nathan Lovato replied

    A resource is neither like a service nor a singleton: it's an object where all references point to the same data in memory.

    What this does is if you try to load the same resource 3 times, Godot will only load it once from the disk, and other calls to `load()` will instantly return a reference pointed to the already loaded resource.

    Unlike a singleton, which is globally accessible, every reference to a resource is local. It's like any normal object.

    However, as all references to a resource point to a single instance by default, it means that changing the resource from one place affects the other objects that use it.

    This is great for any shared data like using a metal or moss material on multiple 3D objects. You need several 3D objects to use that material, and if you change the material, you want all of them to update. Resources solve that.

    Hope this helps!