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:
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?
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”:
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:
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.