Setting Control node Anchor Presets via GDScript issue

Godot Version

4.4.1

Question

Hi all, quick question about setting Anchor Presets on Control nodes via GDScript instead of the Godot Editor. Here’s my code, only important line is the red line trying to set the Anchor Preset, everything else works fine:


(I found this from this post: Anchor presets changing with gdscript)

But this flat out does nothing. Here’s a little more context:

The parent Control node “ChampionUI” highlighted in Green, manual testing Label added via the Editor in Orange (set the Anchor Preset to Centered via the Editor drop down box in the Scene, this works as intended), and the Red Label I’m trying to Center generated via the code above.

I know it’s not an inherited positional issue on the Control nodes, as the manual Orange testing label works as intended (literally the only thing I changed on it was the Anchor Preset, nothing else).

Am I doing something wrong with the Control.set_anchors_preset() function?

I think you want to use set_anchors_and_offsets_preset.

If keep_offsets is true, control’s position will also be updated.

You need to also set keep_offsets to true. It is an optional parameter and is false by default so your node’s position isn’t being updated.

Thank you for the suggestion, unfortunately it didn’t work.
I tried these 3 lines of code, also ensuring the “keep_offsets” param was set to true.
I tried using a bunch of different LayoutPresets and LayoutPresetModes as well.

I checked the Remote Inspector while the game was running, selecting the generated node (Red label) which confirmed it’s Layout Mode was set to “Position” where it should be “Anchors”:

Is there perhaps another function or flag to force a Control node into “Anchors” Layout Mode via GDScript? Or is it a bug maybe?

Hmm, maybe it’s a bug.. or that I’m simply unfamiliar with what the editor is doing. I tested on my end and when I set the Anchors Preset to Center in the editor I can see that it adjusts the position to Vector2(-size.x / 2.0, -size.y / 2.0). When I call set_anchors_preset via code it doesn’t make that position adjustment with keep_offsets set to true or false. However, I was able to successfully mimic the editor position adjustment with just this:

set_anchors_and_offsets_preset(Control.LayoutPreset.PRESET_CENTER, Control.LayoutPresetMode.PRESET_MODE_KEEP_SIZE, 0)

Note: In my testing the 2 Labels I used to compare had a Node2D parent, not a Control node parent.

Yeah I confirmed having the generated Label parented to a Node2D instead of a Control does apply the “Layout Mode” of Anchors. So I think that’s definitely a bug that it doesn’t apply the correct Layout Mode to children of Control nodes. Bit annoying considering I have that base Control node as the positioning point for all my champion UI stuff but I guess that’s a work-around for now, just going to have to restructure the scene tree a bit. Thank you for your help.

If anyone else knows a way I can enforce “Layout Mode” to Anchors to children of Control nodes via GDScript, that would be super helpful though.