Load a resource embedded in the scene

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

Hi, is there a way to reuse a resource that is embedded in the scene? For instance, I have a Label with a DynamicFont which I created in my scene: HUD.tscn, and in Resource → Path it says “res://Entities/GUI/HUD.tscn::1” so I know the resource is embedded in the scene. When I duplicate the label it seems I can reuse the resource but otherwise I can’t find a way to select the same font (resource). I tried creating a new resource and typing “res://Entities/GUI/HUD.tscn::1” to Path but it gave me an error: " Another resource is loaded from path: res://Entities/GUI/HUD.tscn::1 (possible cyclic resource inclusion)".
I also tried loading the resource by selecting the scene (HUD.tscn) but it was not successful.

:bust_in_silhouette: Reply From: MysteryGM

Hi, is there a way to reuse a resource that is embedded in the scene?

Godot is a instance engine, by default you are reusing resources. When you for example add a label to a scene, it is a instance of a other label class.
What this means is that if you add one or a hundred labels only one was ever loaded.

The preload() function will add a resource at compile time and with testing I confirmed that preloading a object more than once in different scripts will still only load it once.

The instance() command is what you are looking for. For existing classes like labels you can use new().

If you save the label as a scene in the resource folder:

var MyCustom = preload("res://MyCustomLable.tscn")

func _ready():
	var tempHold = MyCustom.instance()
	tempHold.text = "A instance of a custom label"

When you want a new clone of the original label class:

func _ready():
	var tempHold = Label.new()
	tempHold.text = "A instance of the master label class"

Just like in most languages a new object of a class is actually a instance.

Just checked, it was actually mentioned in the Documents that objects are loaded only once. Resources — Godot Engine (3.0) documentation in English

“When a resource is loaded from disk, it is always loaded once. That means”

Hi, thanks for answering but I am not sure if I follow. What I am trying to do is reuse a sub-resource (in my case a DynamicFont) from the inspector like a regular resource (.res, .tres etc.)

gfl | 2018-10-09 13:56

Save the sub resource as a normal resource.

To do this just click on the label that has the font. Then click on the Dynamic font where it is.
The inspector will change to show you are editing the font, where you change the size etc. Right at the top of the inspector is a save icon.
Save the font and re-use it. All sub resources can be saved this way.

Just a note about saving the font like this, it keeps the same size for all instances using the font.

MysteryGM | 2018-10-09 19:33