Errors spawning packed scene at a node

Godot Version 4.2.2

I’m experimenting with trying to spawn and remove environment dynamically using this code.

func _ready():
	var appartment1 = $Appartment1Area
	worldspawn(appartment1)


func worldspawn(area_node: Area3D):
	var appartment = preload("res://Appartment.tscn").instantiate()
	appartment.global_transform.origin = area_node.global_transform.origin
	add_child(appartment)

So far it works technically, seems to render as expected but I do get an error code when using “appartment.global_transform.origin = area_node.global_transform.origin”. Using it spawns the environment where I want it, not using it doesn’t. This is the error code I get with the line:

E 0:00:03:0050 Main.gd:30 @ worldspawn(): Condition “!is_inside_tree()” is true. Returning: Transform3D()
<C++ Source> scene/3d/node_3d.cpp:343 @ get_global_transform()
Main.gd:30 @ worldspawn()
Main.gd:9 @ _ready()

I’ve only recently been trying to learn how to code so I’m pretty amateur, if anyone has any insight on how to resolve or work around this error, I’d appreciate it. Even if it technically works I feel like I’m teaching myself to code like an idiot if I just let errors collect in my debugger.

seems not ready yet, try to add_child.call_deferred(appartment)

“!is_inside_tree()” is true.

means node has to be inside of tree, try to add_child to world, then edit its transform

1 Like

Yup, putting add_child before editing it’s transform solves the issue. Thank you, so much for sparing the time to teach an idiot like me. I feel so embarrassed that I didn’t realize I tried setting the position of an object before I created it, and then got confused about what could possibly be wrong with the code.

I’m not actually sure what you mean by this though, trying to use add_child.call_deferred(appartment) in my code in place of add_child(appartment) seems to be a way of reproducing my original error even when it’s ordered properly. If this was like a technical explanation of what I was doing wrong it’s so far over my head.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.