MarginContainer margins not inherited by all children

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By mkx

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:

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

What am I missing here?

:bust_in_silhouette: Reply From: mkx

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.