Creating an Instance of a scene within a scene at run time

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

I thought I had this figured out but somehow I can’t seem to get it to work as I would expect.

I have a Position3D node where I want to instance a scene (bullet) at

I create a reference to the scene like this: const BULLET = preload("res://Bullet.tscn")

I then instance the scene like this: var bullet_instance = BULLET.instance()

Finally I position the instance like this: bullet_instance.global_transform = global_transform

This code is running on the Position3D node where I want to create the bullets. The code runs, but it doesn’t create any bullets that I can tell. When I look in the remote scene tree at run time no new nodes are being created.

:bust_in_silhouette: Reply From: Jan Heemstra

You need to add the bullet instance as a child of some node, in order to add it to the tree and to render it to the screen. Use the Node.add_child method on the parent node.
Also, I would recommend against adding it to the Position3D, as translating this node afterwards might create some strange effects.

That’s got it. Thank you.

2-Zons | 2019-06-05 00:38