Troubles with Tween.follow_method

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By genete
:warning: Old Version Published before Godot 3 was released.

Hi,
I’m trying to use the Tween.follow_method but doesn’t seem to work at all.
This is the scene I have:

  • World (Node2D)
  • tween (Tween) (script 1)
  • tracker (Node2D (script 2)
    • sprite (Sprite)
  • follower (Node2D)
    • sprite (Sprite)

and the scripts are:
Tracker script:

extends Node2D

var origin = Vector2(200,200)
var speed = -90 # deg/sec
var radius= 100
var time = 0

func _ready():
	set_process(true)
	pass

func _process(delta):
	time = time + delta
	set_pos(origin + radius * Vector2(cos(deg2rad(speed)*time),sin(deg2rad(speed)*time)))
	pass

Tween script:

extends Tween

func _ready():
	var tracker = get_parent().get_node("tracker")
	var follower = get_parent().get_node("follower")
	print("tween ready")
	print(tracker.get_name())
	print(follower.get_name())
	connect("tween_step", self, "on_tween_step")
	follow_method(tracker, "get_pos", tracker.get_pos(), follower, "set_pos", 10, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT, 0)
	start()
	pass
	
func on_tween_step(object, key, elapsed, value):
	print(object, " ", key, " ", elapsed, " ", value)

Neither the follower follows the tracker neither the signal callback on_tween_step is called never.

Anyone has used follow_method before? I’ve seen many examples of follow_property but not for follow_method.

:bust_in_silhouette: Reply From: genete

Found what happened:
Documentation says this:

  • bool follow_method ( Object object, String method, Variant initial_val, Object target, String target_method, float times_in_sec,
    int trans_type, int ease_type, float delay=0 )

Follow method of object and apply the returned value on target_method of target, beginning from initial_val for times_in_secseconds, delay later. Methods are animated by calling them with consequitive values.

Or my English is very bad or the documentation states that returned value of method from the objectObject, would be applied to target_methodof the target Object.

But on contrary, what is happening is that what target_method of target Object returns is applied to the method of the object Object.

Very confusing documentation!

In my example, correct call would be:

follow_method(follower, "set_pos", tracker.get_pos(), tracker, "get_pos", 10, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT, 0)

Indeed, actual code behavior differs from what is stated in the docs. Thank you!

dragora | 2017-03-04 09:29

Hi, I know this thread is old but I have been checking what follow_method does and the only thing I found is this (this thread), so since there is no wider explanation than the DOC, I will give mine here. I hope it is understood because my level of English is not very good.

Let’s say that with that you can call two methods in an interpolated way. That is, every time the interpolation advances, the methods will be called.
The value returned by the first called method will indicate the end of the interpolation, the initial interpolation value is defined in the follow_method parameters.
The value generated by the interpolation is sent to the second method as a parameter until the final value is reached, which is what the first method returns. So this final value can vary as it is defined by the return of the first method.

So to summarize, the interpolation is generating values, these values ​​are passed as a parameter to the second method, the end of the interpolation is defined by the return value of the first method. Both methods are called each time the interpolation advances.

When I say first method I mean the second one defined in follow_method, the second one is defined but it is called first by the engine.

SynthED | 2020-02-13 13:53