#Scenes added with add_child() exist but don't show

5 messages · Page 1 of 1 (latest)

radiant lodge
#

I want to make a path follower scene that would let me draw a path to automatically spawn a certain scene along it, and then stop. My current code for the PathFollower2D node is as follows:

extends PathFollow2D

@export var step: float = 32.0

var ring_scene = preload('res://code/entities/inanimate/Ring.tscn')

# Called when the node enters the scene tree for the first time.
func _ready():
    print('trying to follow !')
    
    var highest: float = 0.0
    while progress_ratio < 1.0:
        if progress >= highest:
            highest = progress
            var ring_instance: StaticBody2D = ring_scene.instantiate()
            add_child(ring_instance)
            print(get_children())
            ring_instance.position = global_position
            progress += step
        elif progress < highest:
            break
    
    for i in get_children():
        print(i, ' ', i.position)
    print('done !')

func _process(delta):
        # extra debug discard later
    if Input.is_action_just_pressed("down"):
        print(get_children())
        for i in get_children():
            print(i, ' ', i.position)

This seems to technically work, since the print statements up confirm the scenes have been instantiated, added to this node, and are even in the right positions. But in-game, they don't show up at all, and they can't be interacted with in any way, as if they aren't present. I'm pretty much stumped at this point, any help is appreciated.

snow turtle
#

They're in there, but far away, your can see it by printing global_position of such added nodes.

rain ravine
#

When you add a child, it is automatically added at its parents global position. The position variable is always relative to its parent (local) Therefore when you set the position it adds an extra unintentional offset.

#

You are also printing position rather than global position which is misleading you in debugging and making you think the object is in the right place but invisible - when in reality it is probably offset and pushed off the screen.

#

@radiant lodge