yield
works inside function where it was called, because this is one of the tools to work with asynchronous code. in this layout you need to add yield(get_tree(), "idle_frame")
after create_children()
too:
func _ready():
create_children()
yield(get_tree(), "idle_frame") # wait for next frame here too
print(get_children()[0].rect_size) #This printing is incorrect
print(expanded_size) #The printing is incorrect
func1() #cannot access expanded size in this method
func2() #cannot access expanded size in this method
here yield
acts as simple shorthand for (basically yield is shorthand for signals handlers):
# ...
get_tree().connect("idle_frame", self, "_on_idle_frame")
func _on_idle_frame():
print(get_children()[0].rect_size) #This printing is correct
expanded_size = get_children()[0].rect_size
if you will use it frequently, consider making wrapper around BoxContainer with custom signal, i.e. size_updated
to reduce amount of lines to write.