Should I create preload variables to instance in every scene that utilizes that resource?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Hammer2000

I have a resource preloaded in my main script, and finding no way to access that PackedScene from children nodes, I was wondering if creating a variable to preload a resource in every scene that needs to instance it is a big issue or if there’s some better way to optimize.

For example, I have a Character resource scene, packed in the root node. If I have a sub-node that is character creation, i want to utilize that same packed scene to instance in that child node. Or am I thinking about this all wrong?

:bust_in_silhouette: Reply From: hinasis

Well, there are different ways to do it… The simple way is creating a singleton, the famous “global” node.
Create a GDScript named “”, inside it place the preloaded resource reference.
Then in the project settings go to the “Auto Load” tab and in the “path” select the file you just created (you can search in the load dialog window by clicking in the " … " button), give it a name and click in the “add” button.
This will add a line at the box showing your new singleton.
If done, now this GDScript is loaded when you play the game and can be acceded from every node in your game by typing the name you gave it. Its a good place to set variables you need in many scenes or functions too.

:bust_in_silhouette: Reply From: hilfazer

Yes, go ahead and make a preload variable in every file that needs it. Each resource will only be loaded once no matter how many times you preload() it.

It also makes clear what files given script uses.
Godot 3.1 can warn you if there’s unused variable so in case your script no longer needs some preloaded PackedScene you will get a warning.

Renaming a resource might be a problem since you’ll want to rename all instances of file path. In Godot 3.1 there will be working ‘Find in files’ option that will be able to perform project-wide replace.

Both of those 3.1 functionalities are already available in 3.1 alpha.