multiparenting == instancing ?

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

Hello, I am new to godot and find it intriguing due to its great flexibility.

As I have a VRML background, I am trying to figure out what the closest equivalent of the VRML DEF-USE mechanism is. VRML has a scene graph which allows for multiple parents for the same node. Here is Panda3D explanation of this concept:

https://docs.panda3d.org/1.10/python/programming/scene-graph/instancing

A VRML node can be inserted a second time somewhere else in the scene graph by just referencing (“USEing”) its “DEF” name. The second use is not a copy and strictly speaking is not even a pointer to the first use of the node but is materially the same node. The same node then just has two parents and exists in multiple places.

One way to do something like this in Godot seems to be instancing of a scene (which may have only one node). However, after reading the documentation a few times and through answers here at the forum, I am still not quite sure what instancing exactly does. The method documentation to .instance()

just repeats ‘instancing’ without explanation. The doc Introduction

https://docs.godotengine.org/en/latest/getting_started/step_by_step/scripting_continued.html#instancing-scenes

does not use the word ‘copy’ but also not the word ‘reference’ or ‘pointer’.

Digging further, a PackedScene is a Resource which is a reference counted object, so presumably .instance() creates a new, light-weight object which only maintains a pointer back to the scene root node. Is that more or less correct ?

The other option to achieve multiparenting(-like) behavior seems to be reuse of Resources. In fact that seems to be a closer fit since Resources are not loaded more often than once. However, a Resource itself cannot be a Node in the scene graph. For that a .new() call for constructing a new object seems necessary, for each placement in the scene graph. Is that about correct ?

Another way to ask the question is to go back to the Panda3D way of instancing. What would be the closest equivalent in Godot ?

https://github.com/godotengine/godot/blob/26e8fb855febb09583f9c6fe176c1846f39b3be5/scene/resources/packed_scene.cpp#L49

is the method which is used by scene.instance().

From what I can tell it goes through the nodes of the scene and will construct a new node if it had not already been constructed for another instance, or otherwise reference this already constructed node.

Andreas Plesch | 2020-02-11 15:55

:bust_in_silhouette: Reply From: wombatstampede

Instancing is not Godot specific.
There are classes which i.e. can be a node. A Resource basically also is a class (in my understanding) until it is instanced.

So even if you load a resource it is just a resource but no instance.

An instance is an object. It is a Class/Resource put to life and use.

You can apply one instance to multiple parents. And this instance will be the same object.

This can be done by code or in the editor itself… and can be a bit surprising.

Surprises will i.e. happen if you copy a node which has some attributes (i.e. embedded materials) assigned. If you change the material (i.e. albedo color) on one node it will change on the other node as well.

This is because the node instance was duplicated (= new instance) but the instance of the assigned material wasn’t. On such attributes you will find a drop down list in the editor which will also offer “make unique” which will actually create a uniqe instance.

So instances are unique objects. They can be multiparented.

:bust_in_silhouette: Reply From: Sween123

Instancing is different from refferencing.
For example, you have a class “Animal”, An instance of this class means to create a new object that is “Animal”.
Referrencing means that, if you have an intanced object “Animal 1”, refference Animal 1 by using a variable like var reference_animal = Animal_1, they are the same node. It’s the same idea like when referencing dictionaries and arrays.
The thing like “multi-parenting” you said, in Godot, I think it’s not really refferecing, it’s more like extending a class. If it’s not like that and is just exsits in a different scene, I guess it will be just like what I wrote above, creating a variable and put an equal sign, and this variable will be that node. Otherwise, save a scene you created, for example “Monster”, and you instance from this scene you will have multiple different monsters that are alike, but can have different values like HP, MP, attack, position, etc.