How would I go about changing a Panel's background color programmatically? I've been reading the documentation up and down but am obviously missing something.

The short answer is "use the Panel's AddStyleboxOverride()to provide a StyleBoxFlat with its BgColor set to whatever you want it set to". See the docs for Control for a kinda-example: https://docs.godotengine.org/en/stable/classes/class_control.html#class-control-method-add-stylebox-override

In practice, it's somewhat more involved, as you have to first have a StyleBoxFlat to copy (or, I suppose, just create a new() one and set all the properties as appropriate), and then use that to override the name of the StyleBox the Panel would normally use, given your particular Theme.

It really does take a more-than-superficial understanding of Godot's GUI Theme system, as you're not just saying "change this Panel's background colour", but, "instead of using your regular StyleBox, use this one instead".

@Ommi said: The short answer is "use the Panel's AddStyleboxOverride()to provide a StyleBoxFlat with its BgColor set to whatever you want it set to". See the docs for Control for a kinda-example: https://docs.godotengine.org/en/stable/classes/class_control.html#class-control-method-add-stylebox-override

In practice, it's somewhat more involved, as you have to first have a StyleBoxFlat to copy (or, I suppose, just create a new() one and set all the properties as appropriate), and then use that to override the name of the StyleBox the Panel would normally use, given your particular Theme.

It really does take a more-than-superficial understanding of Godot's GUI Theme system, as you're not just saying "change this Panel's background colour", but, "instead of using your regular StyleBox, use this one instead".

Hello and thanks for your reply, this is what I was trying to do but I was not getting anywhere. I tried the following code: StyleBoxFlat new_styleboxflat_normal = new StyleBoxFlat(); new_styleboxflat_normal.BgColor = new Color(1,0,0.5f,0.5f); GetNode<Panel>("panel/panel2").AddStyleboxOverride("custom_styles/panel", new_styleboxflat_normal);

But this did literally nothing. worked around with a few changes. In the end I found what I think is a solution because I found a post from 2016 that used .Set() instead of AddStyleboxOverride() and this actually worked. @Ommi if you know why my original code didn't work then please enlighten me because I would preffer to do it as intended.

For anyone wondering in the future: adding .Set("custom_styles/panel", new_stylebox);

to the node will do the trick. at least it did for me.

9 months later