Animation - Function track, set argument value with a script

Godot Version



In the animation class documentation, there a method method_track_get_params(). But there no method_track_set_params(). Does that means it is impossible to modify the argument in the methods call during the animation. That means, during the animation, you can only call methods with a static argument that never changed ? Or there a way to set it by some way ?

You can replace them at runtime with Animation.track_set_key_value()

extends Node

@onready var animation_player: AnimationPlayer = $AnimationPlayer

func _ready() -> void:
	# Create an Animation Resource
	var animation =
	animation.length = 1.0
	# Add a method track and set its path to this node
	var method_index = animation.add_track(Animation.TYPE_METHOD)
	animation.track_set_path(method_index, get_path_to(self))

	for i in 3:
		# Insert 3 keyframes which will call test()
		animation.track_insert_key(method_index, i / 3.0, {"method": "test", "args": ["First %s" % i]})

	# Add the animation to the Global animation library
	var library = animation_player.get_animation_library("")
	library.add_animation("new", animation)

	# Play it and await until it finishes"new")
	await animation_player.animation_finished

	# Modify the keyframe values with new arguments
	for key in animation.track_get_key_count(method_index):
		var name = animation.method_track_get_name(method_index, key)
		var args = animation.method_track_get_params(method_index, key)
		args[0] = 'Modified %s' % args[0]
		animation.track_set_key_value(0, key, {"method": name, "args": args})

	# Play it again"new")

func test(s:String) -> void:

But remember that Animation is a Resource and it’s shared by default. Modifying it will modify all instances of that Animation.