Get Rect Size of a Container Node

:bust_in_silhouette: Reply From: IHate

You can use a CenterContainer as a parent for the Vbox so it’s always centred.

To access the size property of a control node use rect_size.
Hovering over a property on the inspector will give you the proper method

While I can use the CenterContainer to solve my problem, I still am getting (0,0) when calling rect_size. Below is another example. In this example, I added 3 texture buttons to the VBoxContainer using the scene editor. The VBoxContainer size automatically adjusts. When I add a 4 texture button via script, I receive the same rect_size. Why is this happening?

Screenshot

aptek | 2020-10-03 03:13

Sorry for the late reply I didn’t see the post

As I understand it the entire function is happening in the same frame and the size doesn’t update until the new buttons are drawn. so what you can do is wait for the next frame to check the rect_size when the buttons are already drawn.

func _ready():
  for n in 3:
	var tButton = TextureButton.new()
	tButton.texture_normal = load("res://icon.png")
	$VBoxContainer.add_child(tButton)

  yield(get_tree(),"idle_frame") # yield until the next idle frame
  print($VBoxContainer.rect_size)

IHate | 2020-10-06 13:59