It worked once, but in a wrong place. I changed some container size flags, and now it's been 3 hours since all went wrong(
The problem:
I have NinePatchRect
(1) inside another NinePatchRect
(2) inside GridContainer
. I need to pass outer (2) rect's size to the inner (1) one. Both NinePatchRect
s have textures set at runtime. As I'm working within a container the only way to get starting size would be to set an anchor for (2), otherwise the size would equal to (0, 0)
. And I can actually see the size.x
value change in the editor after assigning the anchors. BUT, once the code is executed, by the time I get to the size.x
value, it becomes 0
. Even though the texture is placed and shown correctly.
NPR (2) has following size flags:
Horizontal
Fill Expand
Vertical
Fill
NPR (1) is set to layout_mode
POSITION
at the position of NPR (2). If I use any anchors, I won't be able to change the size of the rect (which I need to do dynamically).
I actually have a RichTextLabel
(of the same size I could use) nearby within the same GridContainer
, and I can't get its size value as well, as it turns to (0, 0)
.
Question:
How do I get the size of GridContainer
's child?
Edit:
I did find this post on godot forums which seems to be what I need, but the trick with idle frame doesn't work, as I still get size (0, 0)
NPR (1) is set to layout_mode POSITION at the position of NPR (2). If I use any anchors, I won't be able to change the size of the rect (which I need to do dynamically).
You should be able to. Setting the anchor will make the Control
resize automatically, but it won't be setting its size every frame, but only when the container resizes. And, yes, the position properties are hidden in the inspector but you should be able to still write them from code.
Anyway, if you rather the Container
didn't set the size at all, you can use a Control
(a plain old Control
) inside the Container
. So the Container
sets the size of that Control
and inside of that Control
you can add whatever you want with anchors or setting the size form code.
And finally, you can connect the resize
signal of that Control
so the children can react when the container changed it.
How do I get the size of GridContainer's child?
A Control
inside of a Container
will know its own size, the same as if it wasn't in a Container
. Now, I understand that you might not know that size before the Container
sets it. For that you might want to listen (or await) to the resized
signal.
I did find this post on godot forums which seems to be what I need, but the trick with idle frame doesn't work, as I still get size (0, 0)
In the linked page they have this line:
yield(get_tree(),"idle_frame") # yield until the next idle frame
That is Godot 3 code. In Godot 4 you would something like this:
await get_tree().process_frame
In this case we are awaiting the process_frame
signal of the scene tree.