Godot 4: Changing the button font color theme override with GDScript doesn't work

44 views Asked by At

I am a beginner in Godot so my code may be not-optimized. I am open for advices.

I want my buttons to fade in when scene loaded. Here is my script.

extends Button

@export_category("Fade Button")
@export var theFadeButton: Button = self
@export var fadeSpeed: float = 0.15
@export var fadeInWhenReady: bool
@export var fadeInLoadWaitSeconds: float = 0
var defaultBackgroundAlpha: float = 1
var defaultTextAlpha: float = 1
var fadingIn: bool = false
var fadingOut: bool = false


# Called when the node enters the scene tree for the first time.
func _ready():
    defaultTextAlpha = theFadeButton.get("theme_override_colors/font_color").a
    defaultBackgroundAlpha = theFadeButton.get("theme_override_styles/normal").bg_color.a
    if fadeInWhenReady:
        fadeIn(fadeInLoadWaitSeconds)


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
    if fadingIn:
        theFadeButton.get("theme_override_styles/normal").bg_color.a += fadeSpeed * delta * (defaultBackgroundAlpha / defaultTextAlpha)
        theFadeButton.get("theme_override_colors/font_color").a += fadeSpeed * delta
        if theFadeButton.get("theme_override_styles/normal").bg_color.a >= defaultBackgroundAlpha:
            theFadeButton.get("theme_override_styles/normal").bg_color.a = defaultBackgroundAlpha
            theFadeButton.get("theme_override_colors/font_color").a = defaultTextAlpha
            fadingIn = false
            
    if fadingOut:
        theFadeButton.get("theme_override_styles/normal").bg_color.a -= fadeSpeed * delta * (defaultBackgroundAlpha / defaultTextAlpha)
        theFadeButton.get("theme_override_colors/font_color").a -= fadeSpeed * delta
        if theFadeButton.get("theme_override_styles/normal").bg_color.a < 0:
            theFadeButton.get("theme_override_styles/normal").bg_color.a = 0
            theFadeButton.get("theme_override_colors/font_color").a = 0
            fadingOut = false

func fadeIn(waitForSeconds: float = 0):
    theFadeButton.get("theme_override_styles/normal").bg_color.a = 0
    theFadeButton.get("theme_override_colors/font_color").a = 0
    if waitForSeconds != 0:
        await get_tree().create_timer(waitForSeconds).timeout
    fadingIn = true

func fadeOut(waitForSeconds: float = 0):
    theFadeButton.get("theme_override_styles/normal").bg_color.a = defaultBackgroundAlpha
    theFadeButton.get("theme_override_colors/font_color").a = defaultTextAlpha
    if waitForSeconds != 0:
        await get_tree().create_timer(waitForSeconds).timeout
    fadingOut = true

I checked the marks for Theme Overrides in the editor and set colors etc. Fading in for background just works fine, but the text is not even changing. Here is the editor screenshots if you think it is related. enter image description here enter image description here

1

There are 1 answers

3
Theraot On

This is technically not an answer to your question. I do not know why the font color theme override might not be working for you.

Use modulate or self_modulate for fade in and fade out animations.

Furthermore, you do not need to do this from code, you can add an AnimationPlayer where you keyframe whichever properties you want (yes, even theme overrides) and then play the animation from code or set it to autoplay.