0 votes

Given the following scene layout:

\--- MarginContainer
      \--- VBoxContainer
            \--- Control1
            \--- Control2

If MarginContainer sets a margin of say 20px and all children have their size flags set to expand|fill, I would expect that they all receive a 20 pixel margin, but they don't. Only the direct children of MarginContainer do, in this case the VBoxContainer. However, its children have their margins reset to 0.

I find this behavior surprising; I would expect that margins apply transitively to all child nodes of a MarginContainer and don't suddenly stop applying. Shouldn't the VBoxContainer's children expand only to fill their direct parent?

A detail I omitted above is that the VBoxContainer and its children are stored in a separate scene. Does this have any impact on margins applied by its parent when they are in different scenes?

Here are two screenshots of the remote node data that show the problem. First, the parent container with the expected margin and rect position of 20,20:

parent container

And its first child, which suddenly has no margins anymore:

child container

What am I missing here?

Godot version 3.5.1
in Engine by (30 points)

1 Answer

0 votes
Best answer

Nevermind, I figured it out.

The reason the margins are 0 for the children is because they are relative to their direct parent, so this makes sense.

What was throwing me off was a debug overlay I use to draw frames around each node to see its position and size, and it was using get_rect() when it should have used get_global_rect() so it didn't account for margins correctly.

by (30 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.