Godot Version
4.2.1
Question
Hello. In my game there’s the panel, and the panel has the StyleBoxFlat property. How to change the bg_color in the StyleBoxFlat property via script?
4.2.1
Hello. In my game there’s the panel, and the panel has the StyleBoxFlat property. How to change the bg_color in the StyleBoxFlat property via script?
You need to change the StyleBoxFlat.bg_color
property.
You can get the StyleBoxFlat
by using Control.get_theme_stylebox()
like var stylebox = panel.get_theme_stylebox('panel')
Remember that StyleBoxFlat
is a Resource
and it’s shared between objects that use it and any modification to it will affect them all. You’ll need to make it unique with Resource.duplicate()
and assigning it back with Control.set_theme_stylebox_override()
for example.
Thanks, that helped a lot here!
The official documentation has a pretty neat sample code for this one:
# 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_theme_stylebox("normal").duplicate()
new_stylebox_normal.border_width_top = 3
new_stylebox_normal.border_color = Color(0, 1, 0.5)
$MyButton.add_theme_stylebox_override("normal", new_stylebox_normal)
# Remove the stylebox override.
$MyButton.remove_theme_stylebox_override("normal")