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

Godot Version

4.2.2 stable

Question

I have managed to instantiate the object now I’m trying to get the camera to that object but I have problems:

my code: for instantiation:

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:
			return res.scene.instantiate()
	else:
		printerr(String("wrong index: [{i}] selected Entity does not exist\n{stack}").format({i = index, stack = get_stack()}))

where I call the instantiation:

func _ready():
	GameManager.planetPosition = position
	GameManager.planet = transform
	
	print("planet ready")
	await get_tree().process_frame
	print("planet ready 2")
	#var item = await DataBaseResources.CreateMe(1)
	#GameManager.map.add_child(item)
	var c = await MP.CreateMe(2)
	GameManager.map.add_child(c)
	
	GameManager.camera.add_child(c)
	print("BEFORE")
	var s = c.get_script()
	s.SetCamera()

camera’s script:

extends Camera3D

func _ready():
	GameManager.camera = self

player’s script which does not want to get fiered at the s.SetCamera() but there I don’t get any errors

func _ready():
	DataBase.CreateDB()
	SetCamera()

func SetCamera():
	camera = GameManager.camera
	cameraPivot.add_child(camera)
	camera.position = cameraPivot.position
	print("HERE!!!")
	initialized = true

it works without errors but camera doesn’t want to be attached

I’ve tried other way around too:

camera.add_child(cameraPivot)

try to print GameManager.camera in set camera function

but only if I have in player script: SetCamera()

to print in Player’s script:

print("HERE!!!", GameManager.camera)

result

HERE!!!Camera3D:<Camera3D#30500979970>

Try to do this:

func SetCamera():
	camera = GameManager.camera
	cameraPivot.add_child(camera)
	#camera.position = cameraPivot.position ...do not set it...
	print("HERE!!!")
	initialized = true

nothing it does not attach to the pivot of camera of player’s body

and it stays from where it was positioned at start scene which is logical because position is not set.

Have you tried this?

yes copied and pasted the code

tried and same it does not attach

When you run the game, in the scene tree where all the nodes in the editor, you will see two options, “remote” and “local”, so there, press the “remote” and see what happened, is the camera inside the game?

can’t find remote/local

but I know that camera3D is inside root of map

just check everything in the remote like pos, etc:

I marked it, when you run the game, minimize it and check the editor

holy crap it seems I don’t have it

First run the game, then you can see it in the editor, do not stop the game

yes camera is inside game but not inside the body, … but doesn’t move, weird thanks for this didn’t know that I can inspect like that

1 Like

Ok so I think you forgot to move the camera :sweat_smile:

ok I inspected it closely it didn’t go in the body but it’s inside the game

in both cases add_child

it’s not inside the body

like it doesn’t move

Means camera is not added in game with codes?

weird after trying to add_child

I get

HERE!!!Camera3D:<Camera3D#30500979970>

That means the camera added to scene, so in remote the camera is in? Or the camera position is following the target?

camera is not following the target because it’s not under node of body

the camera in remote is under root of map

What is camera pivot, is it not the body?:

func SetCamera():
	camera = GameManager.camera
	cameraPivot.add_child(camera) #What is it?
	camera.position = cameraPivot.position
	print("HERE!!!")
	initialized = true