How to get UI theme's separation constant in C#? GetThemeConstant is not behaving as expected

Godot Version

v4.2.1.stable.mono.official [b09f793f5]


I’ve written a custom Container class in C# that in general behaves similarly to the built-in BoxContainer. I can see in the engine’s C++ source for the built-in BoxContainer class that it is drawing from a theme’s separation value to add a margin in between its child controls. I want to use the same theme setting and add the same margin in my custom Container… but I’m having trouble working out how to do this. I thought that I would use this.GetThemeConstant("separation") to get this value, but where the separation is clearly non-zero for a similarly configured BoxContainer node, drawing with some space in between the child controls, that GetThemeConstant method is always returning 0 in my own custom class. This is the case whether using the default theme or whether assigning a custom theme to my container with an explicitly set separation value in the editor.

I’ve also tried using h_separation and v_separation, both of which also always produce zero for GetThemeConstant.

Am I not using GetThemeConstant correctly? Is there another API I should be using instead?

If you want to get the value from an specific type like an HBoxContainer you can pass the name as the type parameter in the function:.get_theme_constant("separation", "HBoxContainer") will return the default value from the Theme

1 Like

Yep, this turned out to be what I was missing - I didn’t realize that theme constants were scoped to a class name. Thanks!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.