Search code examples
godotgodot4

Move object with tween back and forth with Godot 4


I had the following code inside an all_tween_completed signal:

func set_tween():
    if move_range == 0:
        return
    move_range *= -1
    var move_tween = get_tree().create_tween()
    move_tween.tween_property(self, "position:x",
                position.x + move_range,
                move_speed) \
                .from(position.x) \
                .set_trans(Tween.TRANS_QUAD) \
                .set_ease(Tween.EASE_IN_OUT)

So that after everything moved I swap sign and move to the other side. However, in Godot 4 there is no such signal. I already changed the code to accommodate for the new syntax but I am not sure how can I reproduce the previous behaviour. Do I need to keep calling and emitting the finished signal from Tween?


Solution

  • How about creating an infinite loop of a two tween sequence? One tween from the start position to end position and the other from the end position to the start position.

    Example Code:

    var start_x := position.x
    var end_x := position.x + move_range
    var tween := create_tween().set_loops()
    tween.tween_property(self, "position:x", end_x, move_speed).from(start_x)
    tween.tween_property(self, "position:x", start_x, move_speed).from(end_x)