Change style parameters in GDscript

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

I want to be able to change the background color of a StyleBoxFlat I put on a specific panel, but any attempt to get that StyleBox just returns a StyleBoxTexture. I tried making a theme for it and changing the StyleBox for the panel there but still the same issue.

The only thing that works is having an export var that I can then use to edit the StyleBox, but I don’t want to have a static variable for each panel I want to change the background color of when it’s so much easier to just pass the panel to a function.

:bust_in_silhouette: Reply From: decepticlown

This is straight outta docs:

void add_stylebox_override(name: String, stylebox: StyleBox)

Overrides the StyleBox with given name in the theme resource the control uses. If stylebox is empty or invalid, the override is cleared and the StyleBox from assigned Theme is used.

Example of modifying a property in a StyleBox by duplicating it:

# The snippet below assumes the child node MyButton has a StyleBoxFlat assigned.
# Resources are shared across instances, so we need to duplicate it
# to avoid modifying the appearance of all other buttons.
var new_stylebox_normal = $MyButton.get_stylebox("normal").duplicate()
new_stylebox_normal.border_width_top = 3
new_stylebox_normal.border_color = Color(0, 1, 0.5)
$MyButton.add_stylebox_override("normal", new_stylebox_normal)

# Remove the stylebox override:
$MyButton.add_stylebox_override("normal", null)

If you don’t have theme or stylebox assigned just create new one :

var box = StyleBoxFlat.new();