Hi,
this should be simple... but I am new to Godot and can't figure it out.
I have a basic Panel under the root Node with no theme assigned. I have added a theme override with a StyleBoxFlat which specifies a custom bg color, let's say yellow.

I'd like to turn the yellow bg to something else at runtime.
In the _ready() func of the root Node I do this:

func testPanel():
	var style:StyleBoxFlat = StyleBoxFlat.new()
	style.bg_color =  Color("#cc0000")

	$Panel.add_theme_stylebox_override("", style)
	print("Main Panel color was changed")

The func() runs, but no joy. Also I have no idea what the "name" parameter in the add_stylebox_override() refers to.
I've googled the heck out of this and didn't find any solution. If anyone has an idea, I'd be grateful 🙂

Thanks!

good afternoon, evening or day depending on the time this message is read, do you mean this option?

HI,
no the Panel I am referring to is a Panel node (see below).
I am trying to access the object via gdscript (which I can do) but the "official" way of changing the color, using add_theme_stylebox_override() does not work for me. Maybe it's an engine bug?

Thanks

Did you look at official documentation ?

The sample given for the method add_theme_stylebox_override can help (can't get something nice when pasting code here, looks like code pasting thing is completely broken or doesn't like GDscript much, I know now why all code post on the forum looks so ugly !) And by the way the string param is the style name.

    JusTiCe8 Thanks! Yes, I have checked the docs but the example is for a Button control, not a Panel. A Panel doesn't have any theme by default and does not have a "normal" state (or theme) as a Button does.

    You can drag and drop a Panel in the Scene and it has no Theme, but you can still define a Theme Override in the Editor, which I guess is fine if you think that "no theme" == "default theme" and then you override it with a custom bg color.

    What is really unclear is how you do the same (i.e. override default bg color) from code.

    7 months later

    This is old, but the OP helped me get to a solution in Godot 4.1 (see code below). Basically I looked at one of my panels through the inspector and saw that under Theme Overrides > Styles there is a Panel property, and if you hover over it, you can see the property name is theme_override_styles/panel. This clued me in on what the first arg in the call to add_theme_stylebox_override should be.

        func example():
            var style:StyleBoxFlat = StyleBoxFlat.new()
            style.bg_color =  Color.GREEN
            style.bg_color.a = 0.5
            add_theme_stylebox_override ("panel", style)
    4 months later