Check which way a tween is interpolating?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By fader

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.)
:bust_in_silhouette: Reply From: jgodfrey

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?

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.

fader | 2020-05-12 23:46

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.

jgodfrey | 2020-05-12 23:59

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.

fader | 2020-05-13 00:45

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…

jgodfrey | 2020-05-13 01:50

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.

fader | 2020-05-13 02:55

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.

fader | 2020-05-13 07:37