Await Animations in Asynchronous Methods

Just had a thought about the async system Garry implemented in March. Animation calls on weapons or other entities with animated models could be useful methods to be able to use await on. Of course it isn’t hard to just await the amount of time an animation takes to play (assuming there is a function to retrieve that) but it could maybe be a useful feature for scenarios where animation speeds are changed several times during a single animation. Not sure if this is a question or a topic that should be moved to #sb:suggestions-feedback but I think it fits here.

2 Likes

It makes more sense to subscribe to some kind of AnimEnd event.

1 Like

Perhaps. I don’t see a benefit to either one over the other.

1 Like

How would await help if the animation speed isn’t constant?

2 Likes

Because the task of the async function RunAnimation(or whatever it’s called) would finish when the animation completes regardless of time?

3 Likes

@Dreamplay put it pretty well so I didn’t have to:

1 Like

Wouldn’t the task end when SetAnimParam sets an animation like “reload” to true? And not when the animation finishes?

Here’s how the function looks:

SetAnimParam( "reload", true )

I think what @Tripperful is the solution.

Async methods aren’t really suited for animations imo, and won’t be used much in the engine bindings. As others said, an event would be the best

1 Like

Async methods are perfectly suited:

public async Task RunDeathAnimationAsync()
{
    await player.Animator.PlayAsync("death", waitForFinish: true);
    ShowDeathScreen(player);
}

or (if you dont want to block):

public void RunDeathAnimation()
{
    _ = player.Animator.PlayAsync("death", waitForFinish: true)
            .ContinueWith(() => 
            {
               ShowDeathScreen(player);
               return Task.CompletedTask;
            });
}

Events will just make it harder to associate data and feels less natural.

3 Likes

Oh that’s a thing, I guess garry didn’t use it on the reloads since they’re meant to always last for the same duration.

Awaiting the entire duration of an animation doesn’t seem very useful. You would instead want to await certain animation events to play sounds or particle effects from.

I’d imagine your example of forcing the player to watch his own death (in its entirety) to become annoying quick.

This is just an example