Moving platform with parameters?

Godot Version

4.3

Question

I have a platform scene that uses an AnimationPlayer to move up and down.
It works fine where I put it in my level.

But I want to place another platform in another location in my level.
But it this new location there is not the same amount of space.

So my question is:
Can I in code using a @export variable change the position of the last key in the animation?

I am fairly new to Godot.

You can`t change frame of animation by codes, try to use tweens:

@export var move_distance := 50 #this is how much it will move, not the position in y

var start_pos 

func _ready():
   start_pos = global_position.y
   var tween = get_tree().create_tween().set_loops()
   tween.tween_property(self, "position:y", start_pos+move_distance, 3.0)
   tween.tween_property(self, "position:y", start_pos, 3.0)
   tween.play()

I agree with KingGD’s solution, but technically you can change frame of animations by code. The downside is that it will also change for your first platform

Thanks, it worked with the tween.

But somehow the collision no longer works even though that I have a CollisionShape2D on it??

Can you show the scenetree of the platform?

image
The AnimationPlayer is not used but still there.

After create the tween try to use
tween.set_process_mode(Tween.TWEEN_PROCESS_PHYSICS), probably is failing to work because tween by default update in the idle step and physics things should always work on the physics step.

I have this (but with indentation)
func _ready() → void:
# Using tween
start_pos = global_position.y
var tween = get_tree().create_tween().set_loops()
tween.tween_property(self, “position:y”, start_pos+move_distance, speed)
tween.tween_property(self, “position:y”, start_pos, speed)
tween.set_process_mode(Tween.TWEEN_PROCESS_PHYSICS)
tween.play()

But it still does not work.

Can you make the animatablebody2d the scene of the root and attach the script to it?

Yep, thats it.

Thanks again all.