How to instantiate and set the object to specific node child?

@onready var cameraPivot = $body/Node3D

But the camera is not in that :thinking: :open_mouth:

no it is not

is it because it happenes right after instantiate?

Show me the GameManager script

extends Node

var planetPosition = Vector3.ZERO
var planet
var map
var camera

camera script:

extends Camera3D

func _ready():
	GameManager.camera = self

wait, the camera is already inside the tree? or is not added as GameManager.camera = null

no camera is inside the tree and I’m referencing it to the manager

it is added to the manager

manager is singleton
Print is clearly saying that camera is referenced it’s not null

so why you use add child again as it already in the game?

because I want to move camera to the body not to have it in the map root

the body was instantiated not the camera

Ok so make the camera as a new scene, and remove it from the tree, so in codes, add this new camera
also you can create this in only with codes like Camera3D.new()

ok here I’m instantiating them both now:

	var camera = await MP.CreateMe(3)
	
	await get_tree().process_frame
	
	var body = await MP.CreateMe(2)
	GameManager.map.add_child(body)

BUT I get null

HERE!!!<null>

tell me the create me script

extends Node

func _ready():
	for n in container.size():
		dictCon[container[n].entityNum] = container[n]
	initialize = true

var dictCon = {}
var initialize = false

@export var container: Array[Entity]

func CreateMe(index: int):
	if not initialize:
		await get_tree().process_frame
		
	if(dictCon.has(index)):
		var res = dictCon[index]
		if res.scene == null:
			printerr(String("index: [{i}] selected Entity: [{e}] does not have a scene\n{stack}").format({i = index, e = res.entityString, stack = get_stack()}))
		else:
			var instance = res.scene.instantiate()
			get_tree().process_frame
			return instance
	else:
		printerr(String("wrong index: [{i}] selected Entity does not exist\n{stack}").format({i = index, stack = get_stack()}))

:open_mouth: Why so long codes? do like this:

var camera = preload("path/to/your/camera.tscn").instantiate()
camera_pivot.add_child(camera)
camera.position = camera_pivot.position
1 Like

because I want to have them scriptable (resource)

and not adding them with code over and over
just call a number and wala here it is

I’ll try that way now

Ok, now you need to know your previous mistakes:

  1. add_child function will not work because the camera is already in the scene
  2. You can do this by:
var new_camera = camera.duplicate(true)
cameraPivot.add_child(new_camera)

But do not used that, it is not correct I means it is the wrong way…

1 Like

ok how do I remove the camera from scene? and move it back in with add_child?

or simply how to move it elsewhere once when I’ll want to

As I said, delete the camera node, and do like this:

var camera = Camera3D.new() #preload("path/to/your/camera.tscn").instantiate()
#edit here the camera`s properties...
camera_pivot.add_child(camera)
camera.position = camera_pivot.position
1 Like

so if I sum things up

I can’t simply move to other scenes? after it’s made longer time.

or deactivate it and activate it once later and move it

it’s not only camera it’s full entities like 500 of different kinds of entities