Global position not matching view

Godot Version

4.2.1

Question

Hello!

I’m trying to draw a line from the center of one control node to another. The control nodes are the root of a scene called Planet. I instantiate new Planet scenes and add them to the Game scene. They go in VBoxContainers which are in an HBoxContainer (Zones).

Hierarchy looks like this:

HBoxContainer (Zones)
       VBoxContainer1 (Zone1)
            Planet1
            Planet2
      VBoxContainer1 (Zone1)
           Planet1
           Planet2
etc.

When I run everything, the planets are exactly where I expect them to be, but the line points seem to not use the updated position of the HBoxContainer zones. From what I’ve found, Godot needs time to update everything, so I’ve already put the method which draws the line behind a call_deferred.

Any idea what I’m doing wrong? Appreciate any help :slightly_smiling_face:

The code to add the planets is here:

func draw_planets():
    for zone in game_state["zones_to_planets"].keys():
        for planet in game_state["zones_to_planets"][zone]:
            var new_planet_scene = planet_scene.instantiate()
            new_planet_scene.get_node("Planet").setup(planet)
            add_new_planet_to_zone(zone, new_planet_scene)

func add_new_planet_to_zone(zone, new_planet):
    var zone_node_path = "Zones/" + str(zone)
    var zone_node = get_node(zone_node_path)
    zone_node.add_child(new_planet)
    new_planet.get_node("Planet").planet_clicked.connect(_on_planet_clicked)

Drawing the line:

var line = Line2D.new()
line.width = 10
line.default_color = Color(1, 0, 0)  # Red        
line.points = [planet_node1.global_position + planet_node1.size/2, planet_node2.global_position + planet_node2.size/2]

UPDATE:

I stopped drawing the connections immediately after the planets are instantiated and added to the scene, and instead just trigger it during runtime and now everything works fine. It seems like even with call_deferred, the positions aren’t updating in time

The points aren’t in global space, but relative to the line

Thanks for taking the time to respond. I’m not sure what you mean. The points from the planet scene aren’t in global space?

Line2D.points are not global, they have to be relative to the node

Ah I see, good to know. They’re added as children to the root node, so I don’t think that’s it. I updated the original post. The positions of the parent contol nodes aren’t updating in time I’m fairly confident. I’m doing everything in _process which perhaps I shouldn’t be