#Tweening a label color in Godot 4

25 messages · Page 1 of 1 (latest)

cobalt hazel
#

I feel this should be incredibly easy, but it seems to be crazy difficult to figure out. I just want to tween a runtime created label in Godot 4 and have the text fade out after a couple of seconds.

The Tween documentation is very poor on explaining the parameters to the tween_property() method, and when you throw in the fact that themes and theme overrides are involved, forming the correct parameters is very convoluted. It took me hours to get the setup and syntax right to figure out how to do the background color, which is not part of a Label, it’s part of a Control. I thought the label color would be easier as at least it’s a label property, but alas I have failed to figure the syntax.

I thought I had it, and had running code, but no fade happened. I’ve seen others ask but not get answers, and both google and ChatGPT have failed as all the results I found were for Godot 3, not 4. Can anyone provide an example of creating a label in code, setting the font color, and then tweening it to fade out after a couple of seconds?

Thanks.

ionic rover
#

There's a lot of things that can affect the color of a Label, and it gets easier once you understand the hierarchy of Theme > Theme Overrides > Label.color > Modulate. Setting any one of these things could work... as long as there's nothing overriding it.
To make things easier with tweening transparency, you could just work with modulate, which affects anything set previously, so to make it transparent, the modulate alpha just needs to be 0.

#

So example code using modulate:

var m: Color = label.modulate
m.a = 0  # Make it completely transparent
get_tree().create_tween().tween_property(label, "modulate", m, 4)
```And this just fades it out over 4 seconds
#

As for the label color, you could also set the modulate property, but it multiplies anything set previously rather than overriding it. If you want to set the color to be something specific, you could just set label.color.

#

As for creating the label in code, if you really need an example for this:

var label := Label.new()  # Like how you'd instantiate any node
label.text = "Hello world"
add_child(label)  # Make sure to add it to the scene tree somewhere
#

If you find yourself setting a bunch of properties of the label in code like a theme, font settings, etc., then I'd suggest you don't. You can instead make a label with the regular GUI editor and Inspector panel, make it into a scene with just the label, and instantiate the scene and add it to the tree.

#

Or alternatively, make an invisible label template (visible property set to false) in your scene, then every time you want to make a label appear, call the duplicate method to create a new label based on the template

cobalt hazel
#

Thanks very much for that. I’ll test it out when I’m back in front of my computer but it’s looks good on paper. I didn’t understand what modulate was before your explanation. Also I was setting a theme override for the color but trying to tween the theme color, which I guess was why my running code didn’t seem to work, if the override was, as it says on the box, overriding what I was tweening? I just couldn’t find out how to set the base theme color in code other than with an override.

ionic rover
#

Yeah, if you were tweening the theme color while the theme override was set, you wouldn't be able to see the theme color transitioning. Tweening the theme override color would work, though (as long as the label.color wasn't overriding it, in which case you'd need to tween the label.color or label.modulate).
Also, tweening works when there's a property to actually tween. If only setter and getter functions are provided, you would need to do a more complicated workaround, so tweening the color or modulate properties are way easier than messing with theme stuff.

Generally, the base theme color would be something you set in the editor, not during runtime. Themes are meant to style a whole set of control types, rather than being specific to just one. Even if you want to change the font colors of a whole group of nodes during runtime, an easier way is to just change the ancestor's modulate property to set their color all at once.

cobalt hazel
#

@ionic rover Finally got to test, all looks good, at least for the basic fade out. Thanks for the help.

cobalt hazel
# ionic rover As for the label color, you *could* also set the modulate property, but it multi...

Well, just when I thought it was all good. The fadeout is fine with the default label color. So I tried your suggestion to set label.color to change the color, and I'm getting a runtime error on this line label.color = Color.YELLOW This was where I ran into a roadblock yesterday trying my other method. Autocomplete after typing "label." only shows references to theme colors when you type in "color", which is what I was failing at previously. I know you said I could use the modulate to set the color, but I really would liket o understand why label.color doesn't work?

ionic rover
#

font_color is a property of the LabelSettings resource that the label contains

#

So you'd set it like label.label_settings.font_color = Color.YELLOW

cobalt hazel
ionic rover
#

What does your code look like?

cobalt hazel
#

except its the null instance error now. label_settigns an object I have to new()?

#

label.label_settings.font_color = Color.YELLOW

ionic rover
#

That means your label has no LabelSettings associated with it

#

In that case, you can instantiate a LabelSettings resource, but at that point, it would be easier to use modulate or make a Label template in the editor, add a LabelSettings in the editor, and just duplicate the template (or instantiate it as a scene if it's a scene)

#

But yes, if you were to do it in code, you could set label.label_settings = LabelSettings.new()

cobalt hazel
#

Yeah OK, quick try and yes this works.

    var label_settings = LabelSettings.new()
    label.label_settings = label_settings
    label.text = "File saved successfully!"
    
    label.label_settings.font_color = Color.YELLOW```
#

All I wanted originally was a message to popup briefly when I save a file. Funny how things you thought would be one or two lines, quite often aren't 🙂

ionic rover
#

Mhm, here I was thinking that you wanted to dynamically generate text like for an inventory system or something 😛
If it were up to me, I'd think it's easier to just make a single transparent label popup in the editor rather than with code, and the only code would just be to fade it into visibility and out

cobalt hazel
#

Yeah I probably should have gone that way. But I'm trying to avoid a lot of hard UI implementation as possible as I'm building a generic code library, so wanted easily changeable solutions