My node tree looks like this.

My goal is to add a Method_track to the "role_0_animation" ( Which is a AnimationPlayer ) at runtime using a script attached to the root Node, and when the Animation playing, call the method "Node.testTrigger()". No matter how I write it, I always get the following error code:
E 0:00:00:0597 scene.tscn::GDScript_kt58n:75 @ _enter_tree(): Condition "p_key.get_type() != Variant::DICTIONARY" is true. Returning: -1

How should I write the arguments for Animation.track_set_path and Animation.track_insert_key when adding a Method_track at runtime?

I tried:

func _enter_tree():
	var animation:Animation = %role_0_animation.get_animation("intro")
	var track_index = animation.add_track(Animation.TYPE_METHOD)
	animation.track_set_path(track_index, "/root/Node:testTrigger")
	animation.track_insert_key(track_index, 1.0, "")

and

func _enter_tree():
	var animation:Animation = %role_0_animation.get_animation("intro")
	var track_index = animation.add_track(Animation.TYPE_METHOD)
	animation.track_set_path(track_index, "/root/Node")
	animation.track_insert_key(track_index, 1.0, "testTrigger")

and

func _enter_tree():
	var animation:Animation = %role_0_animation.get_animation("intro")
	var track_index = animation.add_track(Animation.TYPE_METHOD)
	animation.track_set_path(track_index, ".")
	animation.track_insert_key(track_index, 1.0, "testTrigger")
  • xyz replied to this.
  • ericlincn track_insert_key() expects a dictionary as the value parameter. The dictionary should contain "method" and "args" keys. The former needs to be a method name string and the latter an array containing the arguments.

    animation.track_insert_key(track_index, 1.0, {"method": "testTrigger", "args": [] } )

    ericlincn track_insert_key() expects a dictionary as the value parameter. The dictionary should contain "method" and "args" keys. The former needs to be a method name string and the latter an array containing the arguments.

    animation.track_insert_key(track_index, 1.0, {"method": "testTrigger", "args": [] } )