Set the current style via code

Godot Version

4.2.2

Question

Hello everyone,

I’m trying to change the style of a button via code. I’ve made a custom theme, it has styles for when a button is “focused”, “hovered” and so on. I want the button, when pressed, to keep the “pressed” style (until my fading animation finishes and the scene changes).
I know I could set the values of the button’s style manually, overriding the “normal” style to be like the “pressed” one, but it’s a hassle ! I would need to store in variables the color, background and so on, and it would nearly defeat the purpose of the built-in themes.

I tried looking at the button doc and didn’t find a way to tell the button which style it should currently display.
Does someone knows how to access that ?

To clarify things, I’m looking for something like that :
button.style = style.pressed

Thank you very much !

1 Like

Try turning your button into toggle mode, and when the button is pressed set the button’s mouse filter to MOUSE_FILTER_IGNORE.

1 Like

This is a good way to do it, you could also use Tweens

I tried right now, and it doesn’t change the style’s behaviour.
To be sure, the code you’re expecting me to put when the button is pressed would look like : button.MOUSE_FILTER_IGNORE or did I misunderstood something ? (Of course I did put the button as toggle, as you told me)

I don’t see how it would solve things ? I would still have to register the color values and so on to tell the tween what end values it should be aiming for, no ?

button.mouse_filter = Control.MOUSE_FILTER_IGNORE

Still not working. Plus the code didn’t give me an error with my previous syntax ? Anyway, this is the result of your suggestion :

(Note : the GIF doesn’t seems to only work once when you load the page, sorry about that)

Settlement_MenuScreen_ButtonEffects

Your previous code should give a warning something like: “Standalone expression (the line has no effect).” You can change the severity to error in the project settings if you want. Just search for “standalone expression”.

It’s really hard to tell apart the button states to be honest. How is the pressed state supposed to look?

Everything that appears (border and font) are darker, like a richer brown

I mean, I know it doesn’t matter much, everything works fine, the transition is clear with the position animations I’ve put and the black screen fading in… But I’m a bit frustrated. Shouldn’t something like setting the states of the button in code be easily accessible ?
I’m no engine programmer, though, so I can’t complain too loudly :wink:

Did you get the pressed style without toggle enabled?
Could you try removing all custom styles and see whether it works with the default style?

Yes, I had the pressed style without the toggle. All the “normal” styles work well (hover, press and so on).

I just tried, and it doesn’t seem like the “pressed” style stays after having been pressed

The toggle mode seems to work perfectly fine for me :person_shrugging:

image

Works also with the mouse filter ignore:

extends Button

func _ready() -> void:
	toggled.connect(__on_toggled)

func __on_toggled(toggled: bool):
	mouse_filter = Control.MOUSE_FILTER_IGNORE

My brain is fried for the day. I’ll look more into it another time. Though, the more I think of it, the more your solution appears fitting, so I’ll mark it as the answer. Theoretically it should work, and it works for you. I must have something else going on in my project, maybe in my animation or whatever. Thank you anyway for sticking with me !!

1 Like

You can also use some other nodes like Panel, or textureRect, you style these and then you can program the onclick event in a personalized way, that way when you click you wait for the animation to finish and then you make the scene change, or whatever what do you want me to do. I really hope you can fix it :grin: :crossed_fingers:

The problem in that solution is that what it does is maintain a texture when the button is pressed, and what you want is that when you click the button it waits for an animation to occur and then does something

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