Changing The Theme Of a Control Node With Code

Godot Version

4.3

Question

I have an inventory system where the slots are panels made with code and they fill a grid that I make in a new scene. I am trying to figure out how to change the theme of the slots with a script so they don’t look like a black box. I can’t find anything when I look it up so I was hoping someone here could help.

You can use Control.add_theme_*_override() methods, e.g. Control.add_theme_stylebox_override() or Control.add_theme_font_override().

Theme resources change the Control’s appearance. If you change the Theme on a Control node, it affects all of its children. To override some of the theme’s parameters, call one of the add_theme_*_override methods, like add_theme_font_override.

What I’ve done for a thing in my project is to create a .TRES resource file out of a custom theme.

Control->Theme-> add a new theme then edit it the way you want it to look. The options are not only in the Inspector but also in a tab in the bottom where the Output is. Once you’re done, right-click the Theme box and select: Save as… and export it.

Hopefully that should help.

i have a theme i just dont know how to apply it using a script

Control.theme = your_theme

If you make a global theme the way I quickly explained above and save it, you can then “force” it for the whole project by doing this:

Project Settings->GUI->Theme->Custom and enter the TRES file you saved.

Ex:

That way, you won’t have to specify a different theme for each control you might use.

HTH