I have a question about the HUD and something I would like to do with it. During the level, just the level mind is the player gets a set of Jewels, Gems as I have called them, that is, this:
When the player has all three, I want all these items to fade out and give the player an extra “life” (only once per level), think of it like the letters “K”, “O”, “N”, “G”, on Donkey Kong Country, except nowhere near as good. Anyway, I want the whole “group” to fade out.
It’s grouped together in a HBoxContainer, with a Label and 3 TextureRect’s. At first, I thought of accessing the the individual parts of the group and using an alpha parameter and making it fully transparent that way with the _process() function at the appropriate time, but, I cannot see an alpha channel as part of the parameters. So is this doable? Failing that, is there any way of using the color(r, g, b, alpha) on these items and decreasing the alpha that way, again the colour parameter seems to be only available in the Label not the TextureRects and do not know if its accessible programmatically? Is there any way else I can do it, or would I just have to kill it off completely as it’s done, that is just, make it not visible (least preferred option), but, if that’s all that’s available. Then I don’t mind too much.
You should be able to do this by adjusting alpha on the modulate parameter.
If they’re in a common control. You could just adjust modulate in that, since it affects all children.
Also, I’d do it as a tween instead of in process, but either works.
Edit: if you do it as a Tween, you can build everything into it with tween property on the modulate, and then tween callback to give the bonus life at the end. Just be aware if you tween multiple properties and want them to happen simultaneously instead of one after another, you’ll need to use parallel(). But then ensure the callback at the end is not in parallel since you don’t want that to happen until everything is done.
var B_Gem: bool = false
var G_Gem : bool = false
var R_Gem : bool = false
func _physics_process(delta):
if B_Gem == true and G_Gem == true and R_Gem == true :
var B_Gem : bool = false
var G_Gem : bool = false
var R_Gem : bool = false
#Play Animation and Sounds
#Increase Player Health
I had never heard of this modulate property before and here I have two people recommending the same thing so thankyou @keithjones and @StormWHM for bringing this to my attention. And because of I had never heard of it thankyou further for including code showing how it could work .
After posting, I went to bed thinking it was pretty stupid to ask about fading a sprite because that could never work, and now I find it may be indeed possible. I say may because I am still in shock that it could happen.
It’s probably something I am doing wrong, but, tried this out now after playing around with it for a while, the lerp would not work for me. When I tried it on the actual sprite itself it gave an error, but, no biggie I put it on the actual element itself and it removed the error, but, nothing occurred. It was supposed to fade to nothing 1 being fully opaque; 0 being fully transparent but it stayed fully opaque!
Now the following code:
this did work. However as it set it in one hit there is no fade .
I will have to try a tween, but, I don’t like my chances, but, I’ll have a go!
This won’t show much, because it interpolates from 1.0 to 0.0 with the weight 0.02 which is 0.98 ↔ barely visible. In fact, using lerp for a range 0.0 to 1.0 is kind of pointless, since you could just use the weight itself instead (or in this case 1 - weight = 1 - 0.02 = 0.98)
I think in both those lines you’re plugging constant parameters into to lerp. Instead of lerp(1,0,.02), if i recall. you want
self_modulate = lerp(self_modulate, 0, .02)
Which iteratively moves the value toward 0. Except i think instead of .02 you probably want it (the desired decrease in alpha per second) * delta to make it framerate independent and last the exact number of seconds you want.
Also be aware there are two different properties, modulate and self_modulate. The first affects any children, the second does not. I think unless you have a specific reason to use the second, you probably want the first.
Assuming you have your a node called gem that you want to become transparent over 5 seconds, Ithink you can just do
Tween t = create_tween()
t.tween_property(gem, "modulate/a", 0, 5)
If your code is inside the class of the gem, then just “this” instead of “gem” (or does gdscript use “self”? I forget, I use c#.) The string parameter modulate/a is a Node path, and I’m pretty sure you access the member a like that, but I’m not 100% positive offhand. You can then await the signal t.finished or connect it to a function to make your code do whatever you want when it’s done.
Trust me, it is 1000% worth it to invest time in getting comfortable with tweens. The official docs on tweens are quite good, but you can also find tutorials out there, like this one, which I found in the official discord.
I am using tweens in the game for when I change direction - it’s very cool - and also when moving in the y direction, so, I am getting used to them, but, comfortable with them, no, not yet. Before I switched to Godot a few months back had never used a tween and the tutorial I used didn’t really explain the tweens, or even what they were, it was just assumed knowledge.
You could also make an animation with simple functions. Since you can call the function with the animation at the specific frames. This really depends on how you want to go about something, as this could even be done without code and only the animations.
As you could make them bounce and blink a few times, before you make them fade away for good. You can also make each gem fade away at different times, so the animation looks a little more dynamic and cute.
If you’ve ever seen Donkey Kong Country then something like that would be the ultimate goal. At the moment though just getting the alpha modulate working is the only goal I am worried about .
Almost there now .
PS: Now I need to figure out how to stop the lerp continually running, that might need another question.
I have some experience with the animation player, but, not a lot, so, I’ll take a look at it closely in the coming days when I get my current problems sorted.