0 votes

I currently have a function which creates a Tween in order to fade some music out or in when corresponding buttons (which themselves are created from a script as well) are pressed. I need a way to tell the Tween which way to go (up or down in volume) but I have no way of knowing which way each Tween is already going so I can't tell it when to go up and when to go down.

Is there a way to check this based on some property of the Tween? If I could somehow return the EaseMode of the interpolation it is currently doing that would do what I need but there doesn't seem an obvious way to do that. If this isn't possible, is there another way to do so?

I know the following code doesn't work but is the kind of thing I'd like to do:

if TweenNode.EaseMode == EASE_OUT:
    TweenNode.interpolate property (blah blah, "volume_db", -80, 0.00, etc.)
else:
    TweenNode.interpolate_property(blah blah, "volume_db", 0.00, -80, etc.)
in Engine by (46 points)

1 Answer

0 votes

So, the tweens are controlled by two independent buttons? If that's the case, why not just set a variable in those button callbacks that tell you what you need to know?

by (10,884 points)

I'm not exactly sure what you mean. Each tween created is meant to be controlled by a button, but each of the buttons is also made out of code based on other things. So I can't just attach a script to set a variable based on the button being pressed. I am kind of new at this, so it's possible I have just overlooked something important.

Thanks.

Normally, a button is connected to some code via a signal (like pressed or button_up or button_down). Those signals are, in turn, connected to a callback function that contains the code that runs when the signal is triggered (by interacting with the button).

I was suggesting that you could set a variable in the callback functions to track whatever you need.

If that's now what you're doing, maybe you could provide some more information.

Oh, I see what you mean. Yes, the function that creates the tween and is supposed to use the tween to either fade in or fade out the audio is called by the button. The problem is that if I define the variable in the function, then it just gets overridden, unless I am just not doing something right?
Here is what I have tried:
At the top of the function _OnButtonPressed(): is

var FadingOut

And then

if FadingOut == true:
    FadingOut = false
    print("fading")
else: 
    FadingOut = true
    print("not")

But this always returns false and prints "not" because FadingOut is not true when it is defined at the top of the function, right? And subsequently any attempts to create a simple toggling mechanism with this solution would break when the button is pressed as the variable is reset.

I have other buttons generated by other functions which use code like what I just posted above, but these buttons all edit the same global variable which is defined at ready and then used by all those buttons.

Seems like we're still missing each other. As a baseline, I'm picturing something like the below (for example):

extends CanvasLayer

var fadingIn = false

function _on_ButtonFadeIn_pressed():
    fadingIn = true
    # do fadeIn tween

_function _on_ButtonFadeOut_pressed():
    fadingIn = false
    # do fadeOut tween

Now, why won't that work, or what are you doing differently? Also, with something like the above, I'm not even sure why you'd need the boolean variable...

I can't do exactly that because it's the same button that I would like to have be essentially a toggle between fading in and fading out, and because the amount and name of the buttons and corresponding tweens is dependent on various different conditions of the app, I can't define a variable for all of them at the top of the script. What I would need is to have the function create something like the code you posted, but uniquely, for each button that is generated. And that I have no idea how to do or a better solution to the problem.

I have found a solution. It may not be the most efficient, but it is simple enough. There is a an array which is created on read:

onready var ActiveTweens = []

This array is then called when the function needs to know whether the Tween node is going up or down. When the name of the Tween Node exists in the array, the function switches to fade in and deletes it from the array, and vice-versa much like the standard toggle function described above:

if ActiveTweens.has(TweenNode.name):
    Use interpolate_property to fade in to desired volume
    ActiveTweens.erase(TweenNode.name)

else:
    Use interpolate_property to fade out to -80.00 dB
    ActiveTweens.append(TweenNode.name)

This way, the variables are stored such that each button and tween can access their variables at any time (or the same time), but not in a way that would cause them to "cross streams" and interfere like they would with a single variable. It also will be somewhat useful to be able to print this array for debugging purposes later, now that I think of it. Thanks for the help, you set me on the right path it seems and I figured something out that works.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.