Interpolating between 2 values (not properties)

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

Hi.
How can I interpolate (over time, as in animate) between 2 values in a function that is not a process function?

I can’t use a tween because that requires an object. There is no object - there are simply 2 values which are Vector3’s (from and to).

Neither interpolate_with nor lerp would work either as these must be in process or physics_process and called on every frame to work.

Can I somehow do that by looping though an array of positions between from and to?

You must call the function repeatedly in some fashion to progress over time; be that in process or from a timer, maybe a background thread or coroutine. Perhaps it’d be more appropriate to use a Tween or an animation? Perhaps more information would help; what are you trying to achieve?

spaceyjase | 2021-11-26 09:48

:bust_in_silhouette: Reply From: DaddyMonster

Sorry, I don’t understand. When you say “there is no object”, how is this possible? Godot is OOP, everything is an object. If you’ve got a node in the tree, you’ve got an object instance.

Just add a tween as the child of whatever node your script is attached to and call $Tween.interpolate_method passing self and the target method in your script along with the values you want to interpolate between. Then just pick up the values in the target method and you’re good to go.

$Tween.interpolate_method passing self and the target method in your
script along with the values you want to interpolate between.

Well that’s the thing. I don’t really know how interpolate_method works. I thought this could be the way to go. Docs are somewhat scant on this. I tried doing something similar to what you described but it didn’t work (although I used a dummy spatial as ‘object’; i will try again with ‘self’; maybe I’m getting the syntax wrong on this one too, will try again tonight).

EDIT

Would love to see an example of interpolate_method. Can’t get this to do anything. Found no examples on google…

Macryc | 2021-11-26 16:39