Global position of a node in ready function returns vector zero

Godot Version

4.1.1

Question

I’m trying to create menu with a pointer next to the menu options, highlighting the one in focus. While using ready function all the buttons return their global position as (0,0) and the pointer appears in the corner of the window instead. When i press a key to then choose the next option the buttons return their predicted global position, the problem only occurs in the initial setup.

extends Node2D

func _ready():
	get_viewport().connect("gui_focus_changed", set_cursor)
	$Control/VBoxContainer/HBoxContainer/VBoxContainer/LoadButton.grab_focus()

func set_cursor(focus_owner):
	$Control/Pointer.position.y = focus_owner.global_position.y

This is expected behavior, because the layout update happens at the end of each frame. See here for reference: Children of GridContainer have same position as parent · Issue #30113 · godotengine/godot · GitHub

1 Like

I made it working by using call_deffered, thank you!

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