Cannot get the syntax right for inserting a key to a call method track added to an animation via code

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Sir_Skurpsalot

I cannot figure out the syntax for adding a key to track of an animation via code that calls a function


does not work.

I found this post which has some information:

but the suggestions there for putting it in dictionary format produces errors also.
What is the correct format for this??!

Old question but maybe it will help someone
I need to fist create a track
set the path in my case it is the root on the scene
and then set the method

var idx = anim.get_animation("Idle").add_track(Animation.TYPE_METHOD)
anim.get_animation("Idle").track_insert_key(idx, 0.2, {"method" : "Hide_sword" , "args" : []})

collinviz | 2022-08-22 02:35

:bust_in_silhouette: Reply From: Sir_Skurpsalot

Figured it out, had to first create the track &key via the editor then look at my tscn file in a text editor in order to get clues about what the editor is doing behind the scenes. The post I referenced didn’t spell it out quite enough for me to get it.
The correct syntax for my example is:

animation_player.get_animation("test_animation").track_insert_key(0, 0.2, {"method" : "test_method" , "args" : []})

both “method” and “args” in that dictionary are constants godot needs, not something named/created by you “test_method” is the name of the method you are calling. This is awesome because it answers a previous question I had about finding a way to keep from having to re-do all of my call method tracks every time I update/re-import my player model into godot. I can just do it all it code and it automatically re-adds it all upon starting up.

Or, in C#:

       .TrackInsertKey(0, 0.2F, new Dictionary<string, object>()
               { "method", "methodName" }, { "args", new object[] {} }

mrle | 2020-11-12 00:22

1 Like