Is there a way to 'unload' animations?

I have always had some issues with understanding the fundamentals of playing animations, so excuse me if this is a very strange question. Here is my situation.

In my game, a player can morph into different creatures. When they morph, the physical model of the morph on the server (so all clients can see it) is cloned and parented to the character. Inside of the model is a local script, which has children animations. This script (client) sends the appropriate animations per the humanoid state to a client side animation player (not parented under the morph’s model, rather under the players character).

OK, I have animation tracks, from Animator loaded animations, purely on the client.
When a different morph gets applied, the model with the current morph is deleted, also the local script that handles passing animations to the client animation player. Then a new morph model is parented to the character and it all happens again.

So what happens is, I end up with animation tracks loaded from the Animator, that are not destroyed (or so I assume).

Is there a way to ‘unload’ or ‘remove’ the animations that are loaded onto an Animator, when you no longer need them, as in my case when a morph is removed?

If I store the tracks in the script that is destroyed with the change of morph, will destroyed tracks unload the animations? In other words does the Animator just keep a reference to ‘live’ animation tracks?

I could always use a remove event to have the server (which controls morphing) to send a list to the client of animation key’s and the client find those keys and destroy the tracks, but is that freeing up the Animator on the client if I were to do this? (Because I heard there is a limit)

Sorry for the long post, any help is appreciated. Thanks.

Alos… I don’t want to delete all tracks, considering the morph will not replace all animations, so if I remove all non playing ones, I will be removing the defaults that the default Roblox Animation script is using, also the same with the Animator, the default Roblox script, might not like that.

4 Likes

Yes. Delete the animator, make a new one. (no, there is not an unload function)

Well, I guess the best way would be to use Animator:GetPlayingAnimationTracks() and destroy the ones that have the IsPlaying property to false. Or yes, you can do what GolgiToad suggested.

2 Likes

I guess I should update the post, to state that I don’t want to delete all tracks, considering the morph will not replace all animations, so if I remove all non playing ones, I will be removing the defaults that the default Roblox Animation script is using, also the same with the Animator, the default Roblox script, might not like that.

Then re-add the default Roblox script too? That’s how I do it. I have different races on my game and each race has its own animations. So when a player switches race, I simply delete the animate script of the old race and clone the one of the new race inside the character.

1 Like

The good news is Roblox is considering turning loaded animations into Children. Then you could just delete them.

2 Likes

If now only talk will turn into action. XD

I had not thought of this alternative. Thanks for the workaround.

1 Like

This is how I do it. New animator, load new animations. Be careful if you depend on animation priorities. Loading animations produces a small delay (under 1 second) where the animation Priority is incorrect. I hope this is fixed soon.

I don’t delete the animator because I don’t find it necessary. But that could work too I guess.

I have tracks named based on the humanoid state, so I could end up with multiple tracks named “Running.” It’s a safety precaution. :slight_smile:

1 Like

The solution that I feel fits me best.

I did tests on this all day, and what I realized, is the limit I was talking about seems to only pertain to ‘playing’ animations. You can only have 256 actively playing animations per Animator.

Also, I was able to load 10,000 cloned animations to an Animator, storing the track in a table, and parenting the animation to ‘character’ with no ill effects except for memory usage.

So I feel confident there is no adverse effect with loading LOTS of animation tracks, as long as memory is not being wasted. In my system, each animation that is loaded into the Animator, is kept in a table, that contains the track, along with a reference to the original animation instance. Since my system keeps the original animation instance parented to the morphs model, once the morph is removed the animations have a parent of ‘nil’ so each time a new animation is sent to my animation player, I check the animation player’s list, and if any of the animation instances have no parent, I ‘unload’ (actually delete) the associated track, and set the table entry to nil, freeing the references.

I do appreciate everyone’s help and responses. This is a great community.

19 Likes